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;
listEntry.Update();
clientContext.ExecuteQuery();
var attInfo = new AttachmentCreationInformation();
attInfo.ContentStream = model.File.InputStream;
attInfo.FileName = model.File.FileName;
var attachment = listEntry.AttachmentFiles.Add(attInfo);
clientContext.Load(attachment);
clientContext.ExecuteQuery();
}
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?