How to attach file in specific Custom list item in SharePoint online using Client Object model?
Any help is appreciated! Thanks!
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityHow 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.