Share via

How to upload a jpg file to a SharePoint list Image type column Using CSOM with C#?

Brian Mair 20 Reputation points
2023-12-15T04:22:18.2433333+00:00

Anybody recently figured out how to upload to an Image field with CSOM?

Microsoft very recently changed how such images are stored and referenced. They used to be stored in Site Assets under the list GUID, and referenced in the Image field with understandable JSON that I could set.

But currently any new images that are set using the SP interface are stored in the list item's attachment folder and the Image field is set to format that I don't quite understand.

For example, after uploading myphoto.jpg to a column called "Photo", the attachments folder contains the following file:

Reserved_ImageAttachment_[5][Photo][15][myphoto.jpg][1]_[1].jpg

And the image field contains the following string:

{"fileName":"Reserved_ImageAttachment_[5][Photo][15][myphoto][1]_[1].jpg"}

I could upload the image and set the field value if I knew what the numbers between the braces represent, but I'm hoping that there's an even easier way to upload an image file to a Image field.

Microsoft 365 and Office | SharePoint | Development
0 comments No comments
{count} votes

Answer accepted by question author
  1. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2023-12-15T07:07:40.5+00:00

    Hi @Brian Mair,

    The modern "Image" Field is expecting a JSON formatted object. We could create an Image class, serialized it and then sent it that way:

    Entities.Image imgg = new Entities.Image
    {
      fileName = "img.jpg",
      serverUrl = "https://site.sharepoint.com",
      serverRelativeUrl = "/sites/mySite/Shared%20Documents/New Images Upload/img.jpg"
    };
    
    string json = JsonConvert.SerializeObject(imgg, Formatting.Indented);
    
    ListItemCreationInformation itmCreation = new ListItemCreationInformation();
    newItem = myList.AddItem(itmCreation);
    newItem["imageColumninternalName"] = json;
    newItem.Update();
    cc.ExecuteQuery();
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.