tags:

views:

263

answers:

1

Following up on my previous question here, in which I answered how to create a folder in a document library. I now need to create a folder in a list, however it seems the webDav approach is not working.

Does anyone have a code snippet for creating a folder in a list using web services or webdav?

Thanks

In the following code no error is produced, but the folder doesn't get created:

private void createFolderUsingWebDav(string siteAddress, string listAddress, string folderName)
        {

            string folderAddress = siteAddress + @"/" + listAddress + @"/" + folderName;
            HttpWebResponse response;
            try
            {
                HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(folderAddress);
                request.Credentials = wsLists.Credentials; // CredentialCache.DefaultCredentials;
                request.Method = "MKCOL";
                response = (System.Net.HttpWebResponse)request.GetResponse();
                response.Close();
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw ex;
                }
            }
        }

Under further inspection, the physical folders are being created, I checked this by mapping a drive to sharepoint then navigating to the list folder, but the folders are not visible in the list view. Also folders I create using the web interface on the list are visible in the view, but physically I can see no difference between these and the folder created using web dav

A: 

Found something that works:

XmlDocument doc = new XmlDocument();
            XmlElement batch = doc.CreateElement("Batch");
            string item = "<Method ID=\"1\" Cmd=\"New\">" +
                "<Field Name=\"ID\">New</Field>" +
                "<Field Name=\"FSObjType\">1</Field>" +
                "<Field Name=\"BaseName\">" + folderName + "</Field></Method>";

            batch.SetAttribute("ListVersion", "1");
            batch.InnerXml = item;

            wsLists.UpdateListItems(listAddress, batch);
JL