Create folder in sharepoint site from CRM record via plugin

Hi All,

Here comes once more interesting post related to sharepoint integration with CRM.

Sometime back i was asked to create folder in sharepoint site as soon as the record created in CRM.

So after some struggle i wrote a piece of code to create folder via crm plugin. Here is the code snippet.

But before start, add the below sharepoint dll’s as reference in the CRM plugin.

  1. Microsoft.SharePoint.Client.dll
  2. Microsoft.SharePoint.Client.Runtime.dll

Note: This will only work for On-premise CRM not for CRM online because we don’t have access to CRM server physical directory to copy the above dll’s


using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using SP = Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
using System.Security;

public class CreateSharepointFolder : IPlugin
static SharePointOnlineCredentials creds;
static string SharepointsiteURL = “”;
static string SharepointUsername = “”;
static string Sharepointpassword = “”;

public CreateSharepointFolder(string unsecureConfig, string secureConfig)
if (string.IsNullOrEmpty(unsecureConfig))
throw new InvalidPluginExecutionException(“Unsecure configuration missing.”);
SharepointsiteURL = unsecureConfig;
string[] splitt = secureConfig.Split(new char[] { ‘;’ });
SharepointUsername = secureConfig[0].ToString();
Sharepointpassword = secureConfig[1].ToString();
public void Execute(IServiceProvider serviceProvider)
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

//Create connection to sharepoint site
creds = GetSharepointCredentials();
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)
Entity entity = (Entity)context.InputParameters[“Target”];
if (context.MessageName == “Create” && context.PrimaryEntityName == “new_entity”)
//Entity folder name in sharepoint site
string entityDisplayName = “Test Entity”;
//Folder name for new record
string RecordFolderName= entity.Attributes[“new_name”].ToString() + “_” + entity.Id.ToString().Replace(“{“, “”).Replace(“}”, “”).Replace(“-“, “”).ToUpper();
//Create entity main folder
CreateFolder(SharepointsiteURL, entityDisplayName, context.PrimaryEntityName.ToString(), RecordFolderName, “”, creds);
//Create child folder
CreateFolder(SharepointsiteURL, entityDisplayName, context.PrimaryEntityName.ToString(), RecordFolderName, “Images”, creds);
catch (Exception ex)
throw new InvalidPluginExecutionException(ex.Message.ToString());
public SharePointOnlineCredentials GetSharepointCredentials()
if (creds == null)
SecureString securePassword = new SecureString();
for (int i = 0; i < Sharepointpassword.Length; i++)

creds = new SharePointOnlineCredentials(SharepointUsername, securePassword);
return creds;
public void CreateFolder(string siteUrl, string listName, string folderlogicalName, string newfolderName, string relativePath, SharePointOnlineCredentials cred)
using (ClientContext clientContext = new ClientContext(siteUrl))
clientContext.Credentials = creds;
Web web = clientContext.Web;
SP.List list = web.Lists.GetByTitle(listName);

ListItemCreationInformation newItem = new ListItemCreationInformation();
newItem.UnderlyingObjectType = FileSystemObjectType.Folder;
newItem.FolderUrl = siteUrl + folderlogicalName;

if (!relativePath.Equals(string.Empty))
newItem.FolderUrl += “/” + relativePath;

newItem.LeafName = newfolderName;
SP.ListItem item = list.AddItem(newItem);

Hope it helps you.

Namaste 🙂



10 thoughts on “Create folder in sharepoint site from CRM record via plugin

  1. And for CRM online?? I know I have to use Webservices, but I dont even know where to start :S

    Is there some kind of a ” CRM Sharepoint integration for dummies” ???? lol

    • No there is no available solution for CRM online because there are no sharepoint libraries available in CRM bin folder on cloud. And moreover we can’t copy deploy the sharepoint libraries on cloud along with the plugin library.

      It is same as, we have Microsoft.Xrm.Sdk.dll on CRM bin folder on cloud but we don’t have Microsoft Office related libraries.

    • Yes, you have to register the assembly in GAC & don’t forget to copy paste the assembly in CRM bin folder.

  2. Thinking Enterprise Solutions

  3. Hi, I have the same requirement to create sp folder from crm plugin. But I am facing issues in the dll. Even if I merged the sp and plugin dll. I ma getting the error. Please help me in this. I have wasted 2 days in this.

    • Paste the plugin dll n sp dll in crm bin directory (if u have crm onpremise version) and Register the plugin on disk mode instead of database.
      Or u can try registering sp in GAC and register crm plugin in GAC mode.
      Ultimately goal is to make crm plugin to talk to sp dll.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s