6

Hi is there a way to attach file in specific Custom list item?

Currently i tried to add files on the root folder using below code:

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("MyCustomList");
        ListItem item = docs.GetItemById(1);
        Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Folders.GetByUrl("Attachments").Files.Add(newFile); //Save file on Attachment folder but not creating new attachment folder for ListItem
        context.Load(uploadFile);
        context.ExecuteQuery();
        Console.WriteLine("done");


    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Any help is appreciated! Thanks!

2 Answers 2

5

I already solve my problem below is my code:

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");

Hope this helps other developers too. Regards!

0

This can be done by calling SPservice operation AddAttachment in the csom code.

Reference

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.