I am using following code to attach the image file to the list item using C# CSOM.

using (ClientContext clientContext = new ClientContext(siteUrl))
    List list = clientContext.Web.Lists.GetByTitle(ListName);
    ListItem item = list.GetItemById(1);
    var attachment = new AttachmentCreationInformation();
    attachment.FileName = "fileName.jpg";
    attachment.ContentStream = fs; // My file stream
    Attachment att = item.AttachmentFiles.Add(attachment);

It work fine, but when I try to attach the file with the same name then it throw an error:

The specified name is already in use.The document or folder name was not changed. To change the name to a different value, close this dialog and edit the properties of the document or folder

I want that previous file should be overwrite with new file. I am unable to find any properties.

Any suggestions?

  • 3
    There is no overwrite feature for attachments. Best alternative is to check if similar file exists then delete and upload. Commented Jun 1, 2017 at 9:05

3 Answers 3


It wont work with item.AttachmentFiles.Add method. But you can use SaveBinaryDirect method to overwrite the attachment.

var list = context.Web.Lists.GetByTitle("CustomList");                
var attachFilePath = @"C:\abcd.jpg";
var listItem = list.GetItemById(4);
using (System.IO.FileStream fileStream = new System.IO.FileInfo(attachFilePath).Open(System.IO.FileMode.Open))
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/sites/GautamTestSite/Lists/CustomList/Attachments/" + listItem.Id + "/" + System.IO.Path.GetFileName(attachFilePath), fileStream, true);
  • Thanks for the answer. I'll revert back once I am done with implementing this code. Commented Jun 1, 2017 at 9:33
  • 1
    hey @TrimantraSoftwareSolution, any luck here ? I tried the code and its working for me :) Commented Jun 5, 2017 at 10:30

You can use the following code to add an item or update it if its already exists.

void AddOrUpdateListItemAttachment(ClientContext ctx, ListItem item, string fileName, Stream fileContent)
            var file = item.AttachmentFiles.Where(a => a.FileName.Equals(fileName)).FirstOrDefault();
            if (file != null)

            AttachmentCreationInformation attach = new AttachmentCreationInformation();
            attach.FileName = fileName;
            attach.ContentStream = fileContent;

Make sure when you pass ListItem object to this function it must be loaded along with its property "AttachmentFiles". You can use following code to load item.

ctx.Load(listItem, i => i.AttachmentFiles);


Based on the comment of @Amal Hashim and with reference to following:

Available members are:

enter image description here

We can say that there are no ways to overwrite the attachment using AttachmentCreationInformation.

If anyone has some more reference then are welcomed to provide here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.