O365 SharePoint 2013 Folder Creation Code Using the Client Object Model

By - January 13, 2014

A lot of organizations today are taking advantage of Microsoft Dynamic CRM’s integration into SharePoint because  SharePoint is by far a much better place to store documents. For example, some of SharePoint’s document management capabilities include: versioning, retention policy, full content indexing and search (think search on file contents, not just title), additional metadata capabilities, direct editing in the browser via Office Web Applications and real-time collaboration. Even though the default integration in Dynamics CRM 2013 includes the global unique identifier (GUID) of the entity in the folder name, there are often times when you need to customize the process. One example might be the need to append additional information to the folder name, add some additional metadata or change the security based upon a field in Dynamics CRM. The below code will get you started creating folders in SharePoint. Keep in mind the below code uses the SharePoint Client Object Model (CSOM).

Before we can do SharePoint development we need to download the client libraries from Microsoft. Run the installation and add a reference to the Microsoft.SharePoint.Client.dll. I had to restart Visual Studio to see the new libraries under the Extensions group. Do note that the assemblies are installed into the GAC.

First we need to create the ClientContext object and pass it into our createSPFolder function. This code could be run from a plugin when an entity is created, or you could make it a workflow custom action. In my code the namespace SP is setup like the following using statement. I do this because there are many similar class names in that namespace that makes calls ambiguous.

using SP = Microsoft.SharePoint.Client;
private SP.ClientContext getClientContext(string url)
{
SP.ClientContext clientContext = new SP.ClientContext(url);
SecureString password = new SecureString();
foreach (char c in _config.Password.ToCharArray())
{
password.AppendChar(c);
}
clientContext.Credentials = new SP.SharePointOnlineCredentials(_config.UserName, password);
return clientContext;
}

Important! – when calling the above function you’ll need to wrap it in a using method because the ClientContext object must be disposed otherwise you’ll have a memory leak.

You’ll notice I’m using my own custom _config object to hold some configurable properties. This makes the code flexible but you’ll need to populate those with your own values/implementation. The url parameter of the getClientContext function is the fully qualified url for the site collection or web wherever you want the client context to be loaded (Example: http://yourcompany.sharepoint.com/sitename).

private void createSPFolder(SP.ClientContext clientContext, string folderName)
{
// create spfolder for the user
SP.List oList = clientContext.Web.Lists.GetByTitle(_config.ListName);
SP.ListItemCreationInformation itemCreateInfo = new SP.ListItemCreationInformation();
itemCreateInfo.UnderlyingObjectType = SP.FileSystemObjectType.Folder;
itemCreateInfo.LeafName = folderName;
SP.ListItem newItem = oList.AddItem(itemCreateInfo);
newItem.Update();
clientContext.ExecuteQuery();
}

In the createSPFolder function we get a list from our _config object (in my scenario it always created in the same list) and then we use the ListItemCreationInformation object and set its properties. Because we’re using the CSOM we need to call the context’s ExecuteQuery function and the folder is created!

The possibilities are endless when you’re in control of how the folders are created in SharePoint. RSM offers services for both Dynamics CRM and SharePoint.  We also have a unique understanding of how these two systems can integrate.  If you’re considering customizing the link between Dynamics CRM and SharePoint, contact our professionals with questions via email at crm@mcgladrey.com or by phone at 855.437.7202.

By: Luke Grindahl – Microsoft SharePoint Server Partner

Receive Posts by Email

Subscribe and receive notifications of new posts by email.