SharePoint 2010 - Client Object Model - Add attachment to ListItem

Posted by Thorben on Stack Overflow See other posts from Stack Overflow or by Thorben
Published on 2010-06-03T21:09:51Z Indexed on 2010/06/03 21:14 UTC
Read the original article Hit count: 1469

Hi,

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

© Stack Overflow or respective owner

Related posts about client

Related posts about sharepoint2010