Jacqueline van der Holst

Link.ToMyThoughts()

Creating a hiërarchy of document library folders and subfolders using SharePoint 2010 Client Object Model

Working with the client object model of SharePoint 2010 I wanted to create a way to be able to create a multiple subfolder structure within a document library. Here I describe the way you can do that. At first a procedure was written to create a subfolder under a folder in the documentlibrary:

public void CreateSubFolderForFolder(string subfolderName, string folderName)

{

//This procedure creates a subfolder for a folder in a documentlibrary list

var clientContext = new ClientContext(Uri);

var list = clientContext.Web.Lists.GetById(ListId);

if (list != null)

{

ListItemCreationInformation newFolder = new ListItemCreationInformation();

newFolder.UnderlyingObjectType = FileSystemObjectType.Folder;

//This function gets the complete url to the folder where the subfolder is created for

newFolder.FolderUrl = GetUrl(folderName);

newFolder.LeafName = subfolderName;

ListItem item = list.AddItem(newFolder);

item.Update();

clientContext.ExecuteQuery();

}

}

In order to create a complete layer of subfolders in a  documentlibrary the function above should be placed in a loop. But how can you  pass a hiërarchical structure to such a function. You can do that for instance by supplying the following structure: /Folder1/Subfolder2/Subfolder3. This states that Subfolder2 needs to be created underneath Folder1 and Subfolder3 needs to be created under Subfolder2. When you pass the structure to the function below this is done:

public void CreateFolderSubFolderStructure(string relativeFolderPath)

{

string totalFolderName = String.Empty;

string[] folders = relativeFolderPath.Split(‘/’);

foreach (string folderName in folders)

{

if (!String.IsNullOrEmpty(folderName))

{

//This checks if the folder already exists

Folder folder = GetFolder(folderName);

if (folder == null )

{

if (!String.IsNullOrEmpty(totalFolderName))

{

CreateSubFolderForFolder(folderName, totalFolderName);

}

else

{

//This function creates a folder directly under the root

CreateFolder(folderName);

}

}

totalFolderName += “/” + folderName;

}

}

}

The GetFolder function in the function above checks wether the folder already exists. If it exists than the folder is not created again but

used to create the next folder.  See below:

 

private Folder GetFolder(string folderName)

{

var clientContext = new ClientContext(Uri);

var list = clientContext.Web.Lists.GetById(ListId);

Folder existingFolder = null;

if (list != null)

{

FolderCollection folders = list.RootFolder.Folders;

string folderUrl = GetUrl(folderName);

IEnumerable<Folder>existingFolders= clientContext.LoadQuery(

folders.Include(

folder => folder.ServerRelativeUrl)

);

clientContext.ExecuteQuery();

existingFolder = existingFolders.FirstOrDefault(

folder => folder.ServerRelativeUrl.ToLower() == folderUrl.ToLower());

}

return existingFolder;

}

 

05/09/2011 Posted by | Client Object Model, SharePoint 2010, Uncategorized | Leave a Comment