Monday 18 January 2010

How to Create Subfolders within Subfolders in SharePoint 2007 via the Lists.asmx Web Service

I did an entry on the deletion of folders via the Lists.asmx web service back in July 2008 which can be found here (http://ddkonline.blogspot.com/2008/07/deleting-folders-in-moss-via-web.html) - but I failed to include how to create folders using the same UpdateListItems() mechanism.

A sample of creating a folder and subfolders within SharePoint 2007 lists can be found below:

/// <summary>
        /// As checked in U2U CAML query builder, this is valid in 
        /// <Batch PreCalc='TRUE' OnError='Continue' RootFolder='/Purchase Order Forms V2'>
        ///<Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>DDkTest2</Field></Method></Batch>
        /// THIS Works for Subfolders
        /// <Batch PreCalc='TRUE' OnError='Continue' RootFolder='/Purchase Order Forms V2/2010_01_January'>
        /// <Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>DDkTest2</Field></Method></Batch>
        /// </summary>
        private void CreateFolder(string listName, string rootSubFolderName, string newFolderName)
        {
            ListsSoapClient client = new ListsSoapClient(); //TODO: Change to use WCF Client Factory

            //Correct invalid characters
            newFolderName = newFolderName.Replace(":", "_");
            string rootFolder = rootSubFolderName.Length > 0 ? string.Format("/{0}/{1}", listName, rootSubFolderName) : listName;
            string xmlCommand = string.Format("<Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>{1}</Field></Method>", rootFolder, newFolderName);
            client.ClientCredentials.UserName.UserName = ConfigHelper.SharePointServiceUserName;
            client.ClientCredentials.UserName.Password = ConfigHelper.SharePointServicePassword;
            XmlDocument doc = new XmlDocument();
            System.Xml.XmlElement batchNode = doc.CreateElement("Batch");
            batchNode.SetAttribute("OnError", "Continue");
            //Insert / to front as it is required by web service.
            if  (!rootFolder.StartsWith("/"))
                rootFolder = string.Format("/{0}",rootFolder);

            batchNode.SetAttribute("RootFolder", rootFolder);
            batchNode.InnerXml = xmlCommand;
            XmlNode resultNode = client.UpdateListItems(listName, batchNode;
        }

1 comment:

santosh said...

Thanks for code. I am getting following error msg while creating subfolder under root folder. Can you please help me with this. Details are below
ErrorCode [0x81020020] - Invalid URL value A URL field contains invalid data. Please check the value and try again

1] I have successfully created Folder "Rootfolder" programatically using lists.asmx under existing document library. 2] Now I want to create sub folder "SubFolder" under this root folder using ur code.
3] Also how to check if folder & subfolder are already existing in the document library? If not exists then i want to create.

Here is the code with slide changes from ur code. It's urgenet hope you will get time to reply. Thanks in advance.


private void CreateSubFolder(string listName, string rootSubFolderName, string newFolderName, ListsService.Lists listWS)
{
//http://ServerName/sites/DevWebApplication/ProjectDocumentLibrary/Rootfolder/SubFolder/

//ListsSoapClient client = new ListsSoapClient(); //TODO: Change to use WCF Client Factory
//Correct invalid characters
newFolderName = newFolderName.Replace(":", "_");
string rootFolder = rootSubFolderName.Length > 0 ? string.Format("/{0}/{1}", listName, rootSubFolderName) : listName;
string xmlCommand = string.Format("New1{1}", rootFolder, newFolderName);

System.Net.NetworkCredential userDefinedCredential =
new System.Net.NetworkCredential("Username", "Password");
listWS.Credentials = userDefinedCredential;

//client.ClientCredentials.UserName.UserName = ConfigHelper.SharePointServiceUserName;
//client.ClientCredentials.UserName.Password = ConfigHelper.SharePointServicePassword;
XmlDocument doc = new XmlDocument();
System.Xml.XmlElement batchNode = doc.CreateElement("Batch");
batchNode.SetAttribute("OnError", "Continue");
//Insert / to front as it is required by web service.
if (!rootFolder.StartsWith("/"))
rootFolder = string.Format("/{0}",rootFolder);

if (!rootFolder.EndsWith("/"))
rootFolder=string.Format("{0}/", rootFolder);

batchNode.SetAttribute("RootFolder", rootFolder);
batchNode.InnerXml = xmlCommand;
XmlNode resultNode = listWS.UpdateListItems(listName, batchNode);
}