Create a folder in SharePoint from CRM Plugin

Today we got requirement to create a folder automatically in Share Point when a record is created in CRM. Normally SharePoint document location is created when we click on Documents on the left navigation pane for the record.
It can be done very easily by using the plugin but we should register the plugin None mode. It’s not applicable for Online CRM.
Here is the code. 

public void Execute(IServiceProvider serviceProvider)

      IPluginExecutionContext iContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
      IOrganizationServiceFactory iServFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
      IOrganizationService iService = iServFactory.CreateOrganizationService(iContext.UserId);
      ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
      if (iContext.InputParameters.Contains(“Target”) && iContext.InputParameters[“Target”] is Entity && iContext.MessageName.ToLower() == “create”)
           Entity entAccount = (Entity)iContext.InputParameters[“Target”];
           string strSharePointURL = CreateFolderInSharePoint(iService, entAccount.Attributes[“name”].ToString(), entAccount.Id);

public string CreateFolderInSharePoint(IOrganizationService coreCrmService, string strAccountName, Guid guidAccountId)

      string strDocLibraryUrl = http://SharePointURL”;
      string strApplicationFolderUrl = strDocLibraryUrl + “/” + “account”;
      string strFolderUrl = strApplicationFolderUrl + “/” + strAccountName;
      // Create folder and subfolder inside SharePoint site
      CreateFolderStructure(strApplicationFolderUrl, strFolderUrl);
      // create and associate the url of the folder to SharePointDocumentLocation and the crm record.
      CreateAndAssociateSharePointDocumentLocation(coreCrmService, guidAccountId, “account”, strFolderUrl);
      return strFolderUrl;

// Check and create folder.

private bool CreateFolderStructure(string strSharePointDocumentUrl, string strFolderUrl)
     if (CreateFolder(strSharePointDocumentUrl))
          return CreateFolder(strFolderUrl);
     return false;

// Create Folder
private bool CreateFolder(string strFolderUrl)
          WebRequest request = WebRequest.Create(strFolderUrl);
          request.UseDefaultCredentials = true;
          request.Method = “MKCOL”;
          WebResponse response = request.GetResponse();
     catch (Exception ex)
           // throw ex;
     return true;

// Create a Document Location record in CRM by associating Share Point URL.

public static bool CreateAndAssociateSharePointDocumentLocation(IOrganizationServiceiService, GuidguidIncidentId, string strAccount, string strSharePointDocumentUrl)
      Entity entSharePointDocumentLocation = new Entity(“sharepointdocumentlocation”);
      entSharePointDocumentLocation.Attributes[“name”] = “SharePoint Document Location for “ + strAccount;
      entSharePointDocumentLocation.Attributes[“description”] = “SharePoint Document Location created for storing documents related to account”;
      entSharePointDocumentLocation.Attributes[“absoluteurl”] = strSharePointDocumentUrl;
      entSharePointDocumentLocation.Attributes[“regardingobjectid”] = new EntityReference(“account”, guidIncidentId);
      return true;
Hope this helps.

Happy CRM’ing

3 thoughts on “Create a folder in SharePoint from CRM Plugin

  1. mirou March 2, 2017 / 11:08 am

    Hi,The line request.UseDefaultCredentials = true; occurs the following error: Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.My plugin is registered is isolation mode :\”none\” and I have in web.configDo you have an idea please ?


  2. mirou March 2, 2017 / 11:09 am

    I have trust level=\”Full\” in web.config


  3. Unknown May 13, 2017 / 6:12 pm

    I have same issue Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. did you solved this issue?


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s