SharePoint 2010 - Client Object Model - Add attachment to ListItem

前端 未结 6 918
傲寒
傲寒 2020-12-30 05:43

I have a SharePoint List to which I\'m adding new ListItems using the Client Object Model. Adding ListItems is not a problem and works great.

Now I want to add att

6条回答
  •  伪装坚强ぢ
    2020-12-30 05:53

    I've used and tried this one on my CSOM (SharePoint Client Object Model) application and it works for me

    using (ClientContext context = new ClientContext("http://spsite2010"))
                    {
    
                        context.Credentials = new NetworkCredential("admin", "password");
                        Web oWeb = context.Web;
                        List list = context.Web.Lists.GetByTitle("Tasks");
                        CamlQuery query = new CamlQuery();
                        query.ViewXml = "New Task Created";
                        ListItemCollection listItems = list.GetItems(query);
                        context.Load(listItems);
                        context.ExecuteQuery();
                        FileStream oFileStream = new FileStream(@"C:\\sample.txt", FileMode.Open);
                        string attachmentpath = "/Lists/Tasks/Attachments/" + listItems[listItems.Count - 1].Id + "/sample.txt";
                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, attachmentpath, oFileStream, true);
                    }
    

    Note: Only works if item folder has been created already

提交回复
热议问题