Create Folder in SharePoint using Web Services

Thanks to some other bloggers on the web, especially David Klein, I've been able to quickly build code which allows me to create and/or ensure that a folder in a particular document library does exist. It took a while to get everything working exactly as it should, but in this post I do paste the code as it works.

First you would need to create a web reference to the following webservice:
http://localhost/_vti_bin/Lists.asmx
Or another location if you do have a specific server or location.

Then copy and paste the code below and this should work. The listName should be the name of the document library and the destinationFolder can be any desired folder in SharePoint.


static string FOLDER_EXISTS = "0x8107090d";
static string SUCCESS = "0x00000000";

private void CreateFolder(string listName, string destinationFolder)
{
string[] folders = destinationFolder.Split('/');
if (folders.Length > 0)
{
string path = folders[0];
for (int i = 1; i < folders.Length; i++)
{
CreateFolder(listName, path, folders[i]);
path = path + "/" + folders[i];
}
}
}

/// <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)
{
SharePointListService.Lists listService = new SharePoint_Tests.SharePointListService.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

//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);
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 = listService.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);
}
}

1 comment:

Anonymous said...

Looks a nice post wil ltry the code.Save my day -sacchit