I am trying to create a new list item with an attachment. Below is my code:

using (var clientContext = new ClientContext(_sharepointSiteURL))
   clientContext.Credentials = _credentials;
   var list = clientContext.Web.Lists.GetByTitle("ListName");
   var listEntry = list.AddItem(new ListItemCreationInformation());
   listEntry["Title"] = model.Title;
   var attInfo = new AttachmentCreationInformation();
   attInfo.ContentStream = model.File.InputStream;
   attInfo.FileName = model.File.FileName;
   var attachment = listEntry.AttachmentFiles.Add(attInfo);

The list item creation code works fine but the file attachment code throws the following error:

Microsoft.SharePoint.Client.ServerException: Data at the root level is invalid. Line 2, position 1.

on the last clientContext.ExecuteQuery().

Does anyone have any ideas as to what the cause of this is?

  • 1
    I ran your same code (Just updated attInfo.ContentStream from my physical file location) in my environment and it seems working file. Is there event receiver bound to your list? are you uploading xml file as attachment as this exception is related to reading xml file? Aug 11, 2015 at 4:26
  • @KalpeshVaghela I am trying to upload a PDF file but I tried with a.txt file and the same error accurs. I don't think it related to the file. I'm not sure what do you mean by event receiver bound to my list. Can you clarify?
    – Marko
    Aug 11, 2015 at 13:10
  • using Event Receiver, we can execute our custom code on some events like Item Adding/Added, Item Updating/Updated, Item deleting/Deleted. So my question is there any event receiver bound to your list? msdn.microsoft.com/en-us/library/ee231563.aspx Aug 11, 2015 at 13:13
  • @KalpeshVaghela Perhaps I should add that I'm trying to do this from a .NET MVC website. This code in my case sits inside a MVC controller and the user is trying to upload a file to a remote Sharepoint server.
    – Marko
    Aug 11, 2015 at 13:25

1 Answer 1


I was having this same error while uploading a PDF to SharePoint.

Solved it by using the Microsoft.SharePoint.Client.File.SaveBinaryDirect() method instead.

Read how to Upload large files sample app for SharePoint

  • Would you please share your code, and a brief explanation on what was the problem and what steps to take to solve it. It will be very helpful for the future readers.
    – Asad Refai
    Sep 23, 2015 at 9:32

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.