Wednesday, 7 October 2015

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(IOrganizationService iService, Guid guidIncidentId, 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 comments:

  1. 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.config
    Do you have an idea please ?

    ReplyDelete
    Replies
    1. I have trust level="Full" in web.config

      Delete
    2. 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?

      Delete