How to attach file in specific Custom list item in SharePoint online using Client Object model?
Any help is appreciated! Thanks!
using (ClientContext context = new ClientContext("http://SharepointURL/"))
{
try
{
Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(@"C:\test.txt");
newFile.Url = "test.txt";
List docs = web.Lists.GetByTitle("CustomList");
ListItem item = docs.GetItemById(ID);
var attInfo = new AttachmentCreationInformation();
attInfo.FileName = mFile.Name;
attInfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes("streamFile"));
Attachment att = item.AttachmentFiles.Add(attInfo); //Add to File
context.Load(att);
context.ExecuteQuery();
Console.WriteLine("done");
}
catch (Exception ex)
{
throw ex;
}
}
Hope this code helps you to attach file to list item.