C#C
C#3y ago
3 replies
XE SparkyCracked

Windows Form App - SharePoint Image Upload

Hello everyone,

I'm currently working on a Windows Form application using C# and have encountered an issue with uploading images to SharePoint. The objective is to allow users to upload images through the Windows Form, and I want these images to be reflected in a SharePoint table.

Here's a quick overview of my process:
1. Users upload images through the Windows Form app.
2. The app uploads images to SharePoint's siteassets.
3. The corresponding SharePoint table gets updated with image information.

The challenge I'm facing is that while the images successfully upload to SharePoint's siteassets, they don't appear in the SharePoint table.

Bellow is a snippet of code:
    while (FileExistsInFolder(context, documentsLibrary, newFileName))
    {
        fileCount++;
        newFileName = $"{coreFileName} ({fileCount}){fileExtension}";
    }

    // uploading the file
    using (FileStream fs = System.IO.File.OpenRead(filePath))
    {
        FileCreationInformation flciNewFile = new FileCreationInformation();

        flciNewFile.ContentStream = fs;
        flciNewFile.Url = newFileName;
        flciNewFile.Overwrite = false;
        Microsoft.SharePoint.Client.File uploadFile = documentsLibrary.Files.Add(flciNewFile);
        context.Load(uploadFile);
        context.ExecuteQuery();
    }
    image_url = $"https://<website>.sharepoint.com/{context.Web.ServerRelativeUrl}{siteAssetsUrl}/{newFileName}";
    image_url = image_url.Replace(" ", "%20");
    Console.WriteLine("Here is the url:\n" + image_url);
    MessageBox.Show("This is the url (check console to copy):\n" + image_url);
}
else
{
    image_url = string.Empty;
}

//Add new TQ to Register
ListItemCreationInformation newTQInfo = new ListItemCreationInformation();
ListItem newTQ = listObj.AddItem(newTQInfo);
newTQ["RaisedBy"] = Raisedby;
newTQ["Subject_x002f_Description"] = TQSubject.Text;
newTQ["ApplicableDrawing_x002f_ModelNum"] = TQDwgNo.Text;
newTQ["Area_x002f_Equipment"] = TQAreaEquip.Text;
newTQ["ParaMaticSuggestion"] = TQPMRemark.Text;
newTQ["DateRaised"] = dateTimePicker1.Value.ToString();
newTQ["ECRNumber"] = newTQNumber;
newTQ["ContractNumber"] = SelectedProjectItem;

// Attempting to update the attatchement folder
string fileName = "workTester.png";
byte[] fileBytes = System.IO.File.ReadAllBytes(imagePath);
newTQ["OverallImage1"] = fileBytes;
Was this page helpful?