SharePoint 2010 - Client Object Model - Add attachment to ListItem

前端 未结 6 1002
傲寒
傲寒 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:52

    It reflects rather poorly on the Microsoft SharePoint team for not coming forward with an acknowledgement of the issue and a usable suggestion on how to resolve it. Here is how I dealt with it:

    I am using the new SharePoint 2010 managed client that ships with the product. Hence, I already have a SharePoint ClientContext with credentials. The following function adds an attachment to a list item:

    private void SharePoint2010AddAttachment(ClientContext ctx, 
                                         string listName, string itemId, 
                                         string fileName, byte[] fileContent)
    {
        var listsSvc = new sp2010.Lists();
        listsSvc.Credentials = _sharePointCtx.Credentials;
        listsSvc.Url = _sharePointCtx.Web.Context.Url + "_vti_bin/Lists.asmx";
        listsSvc.AddAttachment(listName, itemId, fileName, fileContent);
    }
    

    The only prerequisite for the code above is to add to the project (I used Visual Studio 2008) a _web_reference_ I called sp2010 which is created from the URL of: http:///_vti_bin/Lists.asmx

    Bon Chance...

提交回复
热议问题