Archive

Archive for the ‘Web Service’ Category

Create a Folder in List Attachments folder using client object model or Web Service

October 8, 2010 2 comments

Creating a Folder in List Attachments folder using client object model or Web Service is not allowed

Scenario :

I was working hard to achieve adding an attachment to the list item using client object model.

To achieve this I was tried directly uploading the file to the list item that I newly created. I was using this piece of code to do this

private static void CreatenewListItem()
{
            ClientContext oContext = new ClientContext(“http://SharePointschool.net/“);
            Web oWeb = oContext.Web;
            oContext.Load(oWeb);
            oContext.ExecuteQuery();
            List myList = oWeb.Lists.GetByTitle(“ClientModel”);
            ListItemCreationInformation myCreationInformation = new ListItemCreationInformation();
            ListItem newListItem = myList.AddItem(myCreationInformation);
            newListItem[“Title”] = “new List Item2”;
            newListItem.Update();
            oContext.Load(myList);
            oContext.ExecuteQuery();
            //Adding an attachment to the new item
            string path = “C:\\Users\\Sridhar.Voleti-b\\Documents\\Retreive all list fields of a given list.doc”;
            string Attachmentpath = oWeb.Context.Url + “/lists/” + myList.Title + “/Attachments/” + newListItem.Id + “/”;
             FileStream fileStream = new FileStream(path, FileMode.Open);
            myList.EnableAttachments=true;

            File.SaveBinaryDirect(oContext, Attachmentpath, fileStream, true);//This is where the error occurs as it is saving the file to the attachments folder.

}

This is causing an error:

Cannot invoke HTTP DAV request. There is a pending query.

Could not really understand, what the real issue is!!! Started my search for a solution to this issue. Could not find any information. This exception is already documented in the SDK

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.resourcestrings_members.aspx

But there are no pointers on how to proceed further.

One more issue is we are allowed to create a normal folder in list. But to upload a file as an attachment, the primary requirement is ‘availability of a folder in the list attachments folder.’

For example all attachments are stored in a folder which is created under the attachments folder as shown in the list.

http://SharePointschool.net/Lists/ClientModel/Attachments/

To take this a little further, I have tried creating the folders under the Attachments folder using the Lists.asmx webservice.

But again, creating the folder using the farm administrator previleges also failed, but I was able to create folders normally in the root folder.

There are some posts in forums talking about similar issues about attachments to a list item. but there is no concrete solution or a workaround either.

May be a very small event handler will do, as it uses the standard object model, wherein we can create the required folder using the elevated previleges. As I believe creating the folder in every list is not a major concern, as it is useful in any case.

I’ll surely give it a try :). But lets see how things turn out.