In this post i will explain how we can use the UpdateListItems method of Lists web service to create folder and sub folder inside a document library. I will give you the code snippet and using this you can create folders and sub folders inside document library.
Here is the code snippet
private void CreateFolder(string listName, string rootSubFolderName, string newFolderName)
{
string rootFolder = rootSubFolderName.Length > 0 ? string.Format("/{0}/{1}", listName, rootSubFolderName) : listName;
string xmlCommand = string.Format("<Method ID='1' Cmd='New'><Field Name='FSObjType'>1</Field><Field Name='BaseName'>{1}</Field></Method>", rootFolder, newFolderName);
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 you are having different site collection use rootFolder = string.Format("/sites/sitename/{0}",rootFolder);
if(rootSubFolderName.Length>0)
rootFolder = string.Format("/{0}", rootFolder);
//if sub folder is to be created inside a folder
batchNode.SetAttribute("RootFolder", rootFolder);
batchNode.InnerXml = xmlCommand;
XmlNode resultNode =listsservice.UpdateListItems(listName, batchNode);
if ((resultNode != null) && (resultNode.FirstChild.FirstChild.InnerText == FOLDER_EXISTS) || (resultNode.FirstChild.FirstChild.InnerText == SUCCESS))
{
// success
}
else
{
//failure
throw new Exception("Create new folder failed for: " + newFolderName + ". Error Details: " + resultNode.OuterXml);
}
}
Usage: You can call this function to create folders and sub folders as described below:
1) CreateFolder("Your Document Library Title", "","Folder Name");
This will create the folder inside "Your Document Library Title" with the Name "Folder Name" as no rootSub folder name parameter is specified.
2) CreateFolder("Your Document Library Title", "RootFolder","Folder Name");
This will create the sub folder inside "Your Document Library Title", inside "RootFolder"
In this case hierarchy of the folders will be:Your Document Library Title/RootFolder/Folder Name
Here is the code snippet
private void CreateFolder(string listName, string rootSubFolderName, string newFolderName)
{
string rootFolder = rootSubFolderName.Length > 0 ? string.Format("/{0}/{1}", listName, rootSubFolderName) : listName;
string xmlCommand = string.Format("<Method ID='1' Cmd='New'><Field Name='FSObjType'>1</Field><Field Name='BaseName'>{1}</Field></Method>", rootFolder, newFolderName);
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 you are having different site collection use rootFolder = string.Format("/sites/sitename/{0}",rootFolder);
if(rootSubFolderName.Length>0)
rootFolder = string.Format("/{0}", rootFolder);
//if sub folder is to be created inside a folder
batchNode.SetAttribute("RootFolder", rootFolder);
batchNode.InnerXml = xmlCommand;
XmlNode resultNode =listsservice.UpdateListItems(listName, batchNode);
if ((resultNode != null) && (resultNode.FirstChild.FirstChild.InnerText == FOLDER_EXISTS) || (resultNode.FirstChild.FirstChild.InnerText == SUCCESS))
{
// success
}
else
{
//failure
throw new Exception("Create new folder failed for: " + newFolderName + ". Error Details: " + resultNode.OuterXml);
}
}
Usage: You can call this function to create folders and sub folders as described below:
1) CreateFolder("Your Document Library Title", "","Folder Name");
This will create the folder inside "Your Document Library Title" with the Name "Folder Name" as no rootSub folder name parameter is specified.
2) CreateFolder("Your Document Library Title", "RootFolder","Folder Name");
This will create the sub folder inside "Your Document Library Title", inside "RootFolder"
In this case hierarchy of the folders will be:Your Document Library Title/RootFolder/Folder Name