How to upload attachment in SharePoint list Item?

Hello,

Want upload attachment  in SharePoint 2013 List Item using CSOM. Created one windows applcation so user can enter the url of site ,list name and ID of list Item in Textbox so to do this so user can browse and upload the file on particular ID. tried the below code:

ClientContext context = new ClientContext("http://sparees"); using (FileStream fs = new FileStream(@"C:\ABC.pptx", FileMode.Open)) { Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/documents/ABC.pptx", fs, true);

}


August 20th, 2015 10:14am


try example from the following forum thread: Attach file in CustomList Item using Sharepoint.Client object.
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 11:00am

Hello,

I tried the above sample code its not working.Its giving me some error:

Could not find file 'c:\users\jaayush\documents\visual studio 2012\Projects\TestProject\TestProject\bin\Debug\streamFile'.

private void button1_Click(object sender, EventArgs e)
        {
            ClientContext ctx = new ClientContext("https://temp/sites/TEST/");

            Web web = ctx.Web;

            FileCreationInformation newFile = new FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes(@"C:\test.txt");
            newFile.Url = "test.txt";
            List docs = web.Lists.GetByTitle("Student");
            ListItem item = docs.GetItemById(176);

            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

            ctx.Load(att);
            ctx.ExecuteQuery();
            Console.WriteLine("done");
Any help ?

August 20th, 2015 10:52pm

Hello,

I tried the above sample code its not working.Its giving me some error:

Could not find file 'c:\users\jaayush\documents\visual studio 2012\Projects\TestProject\TestProject\bin\Debug\streamFile'.

private void button1_Click(object sender, EventArgs e)
        {
            ClientContext ctx = new ClientContext("https://temp/sites/TEST/");

            Web web = ctx.Web;

            FileCreationInformation newFile = new FileCreationInformation();
            newFile.Content = System.IO.File.ReadAllBytes(@"C:\test.txt");
            newFile.Url = "test.txt";
            List docs = web.Lists.GetByTitle("Student");
            ListItem item = docs.GetItemById(176);

            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

            ctx.Load(att);
            ctx.ExecuteQuery();
            Console.WriteLine("done");
Any help ?

Hi,

 I hope you have just copied and executed the code without modifying. You have to pass your file location path instead of 'streamFile'

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 2:37am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics