0

How to attach file in specific Custom list item in SharePoint online using Client Object model?

Any help is appreciated! Thanks!

1 Answer 1

1
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.

6
  • Using Jsom upload document in sharepoint online Commented Apr 22, 2016 at 12:38
  • In document library or in SharePoint online list item? Commented Apr 22, 2016 at 12:39
  • Upload Document in Custom list sharepoint online listitem Commented Apr 22, 2016 at 12:40
  • it is too much confusing. Please elaborate it. Commented Apr 22, 2016 at 12:42
  • If you need JSOM code to upload attachment to list item than please follow this..sharepoint.stackexchange.com/questions/136577/… Commented Apr 22, 2016 at 12:42

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.