<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jacqueline van der Holst &#187; Uncategorized</title>
	<atom:link href="http://www.jacquelinevanderholst.nl/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jacquelinevanderholst.nl</link>
	<description>Link.ToMyThoughts()</description>
	<lastBuildDate>Tue, 20 Dec 2011 19:32:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating a hiërarchy of document library folders and subfolders using SharePoint 2010 Client Object Model</title>
		<link>http://www.jacquelinevanderholst.nl/2011/09/05/creating-a-hierarchy-of-document-library-folders-and-subfolders-using-sharepoint-2010-client-object-model/</link>
		<comments>http://www.jacquelinevanderholst.nl/2011/09/05/creating-a-hierarchy-of-document-library-folders-and-subfolders-using-sharepoint-2010-client-object-model/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 14:45:00 +0000</pubDate>
		<dc:creator>Jacqueline</dc:creator>
				<category><![CDATA[Client Object Model]]></category>
		<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.jacquelinevanderholst.nl/?p=13</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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:</p>
<p><span style="color: blue;">public void</span> CreateSubFolderForFolder(<span style="color: blue;">string</span> subfolderName, <span style="color: blue;">string</span> folderName)</p>
<p>{</p>
<p style="padding-left: 30px;"><span style="color: green;">//This procedure creates a subfolder for a folder in a documentlibrary list</span></p>
<p style="padding-left: 30px;"><span style="color: blue;">var</span> clientContext = <span style="color: blue;">new</span> <span style="color: lightblue;">ClientContext</span>(Uri);</p>
<p style="padding-left: 30px;"><span style="color: blue;">var</span> list = clientContext.Web.Lists.GetById(ListId);</p>
<p style="padding-left: 30px;"><span style="color: blue;">if</span> (list != <span style="color: blue;">null</span>)</p>
<p>{</p>
<p style="padding-left: 60px;"><span style="color: lightblue;">ListItemCreationInformation</span> newFolder = <span style="color: blue;">new</span> <span style="color: lightblue;">ListItemCreationInformation</span>();</p>
<p style="padding-left: 60px;">newFolder.UnderlyingObjectType = <span style="color: lightblue;">FileSystemObjectType</span>.Folder;</p>
<p style="padding-left: 60px;"><span style="color: green;">//This function gets the complete url to the folder where the subfolder is created for</span></p>
<p style="padding-left: 60px;">newFolder.FolderUrl = GetUrl(folderName);</p>
<p style="padding-left: 60px;">newFolder.LeafName = subfolderName;</p>
<p style="padding-left: 60px;"><span style="color: lightblue;">ListItem</span> item = list.AddItem(newFolder);</p>
<p style="padding-left: 60px;">item.Update();</p>
<p style="padding-left: 60px;">clientContext.ExecuteQuery();</p>
<p style="padding-left: 30px;">}</p>
<p style="padding-left: 0px;">}</p>
<p>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:</p>
<p><span style="color: blue;">public void</span> CreateFolderSubFolderStructure(<span style="color: blue;">string</span> relativeFolderPath)</p>
<p>{</p>
<p style="padding-left: 30px;"><span style="color: blue;">string</span> totalFolderName = <span style="color: lightblue;">String</span>.Empty;</p>
<p style="padding-left: 30px;"><span style="color: blue;">string</span>[] folders = relativeFolderPath.Split(&#8216;/&#8217;);</p>
<p style="padding-left: 30px;"><span style="color: blue;">foreach</span> (<span style="color: blue;">string</span> folderName in folders)</p>
<p style="padding-left: 30px;">{</p>
<p style="padding-left: 60px;"><span style="color: blue;">if</span> (!<span style="color: lightblue;">String</span>.IsNullOrEmpty(folderName))</p>
<p style="padding-left: 60px;">{</p>
<p style="padding-left: 60px;"><span style="color: green;">//This checks if the folder already exists</span></p>
<p style="padding-left: 60px;"><span style="color: lightblue;">Folder</span> folder = GetFolder(folderName);</p>
<p style="padding-left: 90px;"><span style="color: blue;">if</span> (folder == <span style="color: blue;">null</span> )</p>
<p style="padding-left: 90px;">{</p>
<p style="padding-left: 120px;"><span style="color: blue;">if</span> (!<span style="color: lightblue;">String</span>.IsNullOrEmpty(totalFolderName))</p>
<p style="padding-left: 120px;">{</p>
<p style="padding-left: 150px;">CreateSubFolderForFolder(folderName, totalFolderName);</p>
<p style="padding-left: 120px;">}</p>
<p style="padding-left: 120px;"><span style="color: blue;">else</span></p>
<p style="padding-left: 120px;">{</p>
<p style="padding-left: 150px;"><span style="color: green;">//This function creates a folder directly under the root</span></p>
<p style="padding-left: 150px;">CreateFolder(folderName);</p>
<p style="padding-left: 120px;">}</p>
<p style="padding-left: 90px;">}</p>
<p style="padding-left: 90px;">totalFolderName += &#8220;/&#8221; + folderName;</p>
<p style="padding-left: 60px;">}</p>
<p style="padding-left: 30px;">}</p>
<p>}</p>
<p>The GetFolder function in the function above checks wether the folder already exists. If it exists than the folder is not created again but</p>
<p>used to create the next folder.  See below:</p>
<p>&nbsp;</p>
<p><span style="color: blue;">private</span> <span style="color: lightblue;">Folder</span> GetFolder(<span style="color: blue;">string</span> folderName)</p>
<p>{</p>
<p style="padding-left: 30px;"><span style="color: blue;">var</span> clientContext = <span style="color: blue;">new</span> <span style="color: lightblue;">ClientContext</span>(Uri);</p>
<p style="padding-left: 30px;"><span style="color: blue;">var</span> list = clientContext.Web.Lists.GetById(ListId);</p>
<p style="padding-left: 30px;"><span style="color: lightblue;">Folder</span> existingFolder = <span style="color: blue;">null</span>;</p>
<p style="padding-left: 30px;"><span style="color: blue;">if</span> (list != null)</p>
<p style="padding-left: 30px;">{</p>
<p style="padding-left: 60px;"><span style="color: lightblue;">FolderCollection</span> folders = list.RootFolder.Folders;</p>
<p style="padding-left: 60px;"><span style="color: blue;">string</span> folderUrl = GetUrl(folderName);</p>
<p style="padding-left: 60px;"><span style="color: lightblue;">IEnumerable</span>&lt;<span style="color: lightblue;">Folder</span>&gt;existingFolders= clientContext.LoadQuery(</p>
<p style="padding-left: 90px;">folders.Include(</p>
<p style="padding-left: 120px;">folder =&gt; folder.ServerRelativeUrl)</p>
<p style="padding-left: 90px;">);</p>
<p style="padding-left: 60px;">clientContext.ExecuteQuery();</p>
<p style="padding-left: 60px;">existingFolder = existingFolders.FirstOrDefault(</p>
<p style="padding-left: 90px;">folder =&gt; folder.ServerRelativeUrl.ToLower() == folderUrl.ToLower());</p>
<p style="padding-left: 30px;">}</p>
<p style="padding-left: 30px;"><span style="color: blue;">return</span> existingFolder;</p>
<p>}</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jacquelinevanderholst.nl/2011/09/05/creating-a-hierarchy-of-document-library-folders-and-subfolders-using-sharepoint-2010-client-object-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

