Create a folder in SharePoint from CRM Plugin

Hi,
 
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)
{
     try
     {
          WebRequest request = WebRequest.Create(strFolderUrl);
          request.UseDefaultCredentials = true;
          request.Method = “MKCOL”;
          WebResponse response = request.GetResponse();
          response.Close();
     }
     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);
      iService.Create(entSharePointDocumentLocation);
      return true;
}
 
Hope this helps.
 

Happy CRM’ing
Gopinath

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=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.My plugin is registered is isolation mode :\”none\” and I have in web.configDo you have an idea please ?

    Like

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

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

    Like

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

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

    Like

Leave a Reply to Unknown Cancel reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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