Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint 2010 - Client Object Model - Add attachment to ListItem

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

I'm using the SaveBinaryDirect in the following manner:

File.SaveBinaryDirect(clientCtx, url.AbsolutePath + "/Attachments/31/" + fileName, inputStream, true);

It works without any problem as long as the item that I'm trying to add the attachment to, already has an attachment that was added through the SharePoint site and not using the Client Object Model.

When I try to add an attachment to a item that doesnt have any attachments yet, I get the following errors (both happen but not with the same files - but those two messages appear consistently):

The remote server returned an error: (409) Conflict
The remote server returned an error: (404) Not Found

I figured that maybe I need to create the attachment folder first for this item. When I try the following code:

clientCtx.Load(ticketList.RootFolder.Folders);
clientCtx.ExecuteQuery();
clientCtx.Load(ticketList.RootFolder.Folders[1]);             // 1 -> Attachment folder
clientCtx.Load(ticketList.RootFolder.Folders[1].Folders);
clientCtx.ExecuteQuery();
Folder folder = ticketList.RootFolder.Folders[1].Folders.Add("33");
clientCtx.ExecuteQuery();

I receive an error message saying:

Cannot create folder "Lists/Ticket System/Attachment/33"

I have full administrator rights for the SharePoint site/list.

Any ideas what I could be doing wrong?

Thanks, Thorben

like image 387
Thorben Avatar asked Jun 03 '10 21:06

Thorben


People also ask

Can you add an attachment to a SharePoint list?

You can also add an attachment to a list item—upload an image, or attach a file (such as a PDF, a photo, or a video from your device or from OneDrive or SharePoint). Open the list where you want to add an item. Tap New. Under New Item, enter or select the data you want to add.

How do I add an attachment to SharePoint?

It is the Edit form that will have the upload functionality, but you'll likely want to add attachments to the View form (a.k.a. Detail form) as well. In the properties pane, click on the Data field to open the data panel. In the list of fields, find the Attachment field and enable it.


1 Answers

I struggled for a long time with this problem too, so I thought I'd post a complete code sample showing how to successfully create a list item and add an attachment.

I am using the Client Object API to create the list item, and the SOAP web service to add the attachment. This is because, as noted in other places on the web, the Client Object API can only be used to add attachments to an item where the item's upload directory already exists (eg. if the item already has an attachment). Else it fails with a 409 error or something. The SOAP web service copes with this OK though.

Note that another thing I had to overcome was that even though I added the SOAP reference using the following URL:

https://my.sharepoint.installation/personal/test/_vti_bin/lists.asmx

The URL that VS actually added to the app.config was:

https://my.sharepoint.installation/_vti_bin/lists.asmx

I had to manually change the app.config back to the correct URL, else I would get the error:

List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user. 0x82000006

Here is the code:

    void CreateWithAttachment()
    {
        const string listName = "MyListName";
        // set up our credentials
        var credentials = new NetworkCredential("username", "password", "domain");

        // create a soap client
        var soapClient = new ListsService.Lists();
        soapClient.Credentials = credentials;

        // create a client context
        var clientContext = new Microsoft.SharePoint.Client.ClientContext("https://my.sharepoint.installation/personal/test");
        clientContext.Credentials = credentials;

        // create a list item
        var list = clientContext.Web.Lists.GetByTitle(listName);
        var itemCreateInfo = new ListItemCreationInformation();
        var newItem = list.AddItem(itemCreateInfo);

        // set its properties
        newItem["Title"] = "Created from Client API";
        newItem["Status"] = "New";
        newItem["_Comments"] = "here are some comments!!";

        // commit it
        newItem.Update();
        clientContext.ExecuteQuery();

        // load back the created item so its ID field is available for use below
        clientContext.Load(newItem);
        clientContext.ExecuteQuery();

        // use the soap client to add the attachment
        const string path = @"c:\temp\test.txt";
        soapClient.AddAttachment(listName, newItem["ID"].ToString(), Path.GetFileName(path),
                                  System.IO.File.ReadAllBytes(path));
    }

Hope this helps someone.

like image 113
Mike Chamberlain Avatar answered Oct 07 '22 23:10

Mike Chamberlain