Working with SharePointDocumentLocation in CRM 2011.

We had a requirement to associate a SharePointDocumentLocation with a case record when it is created. We wanted to use our own folder structure for it. Something similar to this

Sharepointdocumentlibraryurl/[CustomerName]/[CaseNumber]

i.e. folder name with Customer Name and a subfolder with the name of Case Number.

Normally SharePoint document location is created when we click on Documents on the left navigation pane for the record. It doesn’t get created or auto associated to a SharePoint folder on create of record.

So the solution was to write a plugin in on create event and then create a folder in SharePoint and then associate it with the created record.

The source code for implementing it (just posting the source code i hope it is self explanatory 🙂 )


// following logic within the Post Create plugin of Case (incident)
	 if (this.Context.MessageName.ToUpper() == "CREATE" && Context.InputParameters.Contains("Target")
                && Context.InputParameters["Target"] is Entity)
            {
                Entity targetIncident = (Entity)Context.InputParameters["Target"];

                //// Create doc folder for case
                Guid incidentId = Context.PrimaryEntityId;
                string ticketNumber = targetIncident["ticketnumber"].ToString();
                Guid customerId = ((EntityReference)targetIncident["customerid"]).Id;

                Contact contact = // code to get the contact's fullname

                SharePointHelper sharepoint = new SharePointHelper();
                sharepoint.CreateFolderForCase(organizationService, contact.FullName, ticketNumber, incidentId);
            }

// Helper functions used

	    public string CreateFolderForCase(OrganizationService coreCrmService, string contacFolderName, string caseFolderName, Guid incidentId)
        {
            string docLibraryUrl = "http://SharePointSiteCollection/incident";
            string applicantFolderUrl = docLibraryUrl + "/" + applicantFolderName;
            string caseUrl = applicantFolderUrl + "/" + caseFolderName;

            //// Create folder and subfolder inside SharePoint site
            this.CreateFolderStructure(applicantFolderUrl, caseUrl);

            //// create and associate the url of the folder to SharePointDocumentLocation and the incident record.
            this.CreateAndAssociateSharePointDocumentLocation(coreCrmService, incidentId, caseFolderName, caseUrl);
            return caseUrl;
        }

		private bool CreateFolderStructure(string applicantFolderUrl, string caseFolderUrl)
        {
            if (this.CreateFolder(applicantFolderUrl))
            {
                return this.CreateFolder(caseFolderUrl);
            }

            return false;
        }

		private bool CreateFolder(string folderUrl)
        {
            try
            {
                WebRequest request = WebRequest.Create(folderUrl);
                request.Credentials = this.credentials;
                request.Method = "MKCOL";
                WebResponse response = request.GetResponse();
                response.Close();
            }
            catch(Exception ex)
            {
                TraceHelper.TraceEvent(ex, "CreateFolder", folderUrl, TraceHelper.AMSApplication.AMSService);
            }
            return true;
        }

		public static bool CreateAndAssociateSharePointDocumentLocation(OrganizationService coreCrmService, Guid incidentId, string caseNumber, string sharePointDocumentUrl)
        {
            SharePointDocumentLocation sharePointDocumentLocation = new SharePointDocumentLocation();
            sharePointDocumentLocation.Name = "SharePoint Document Location for " + caseNumber;
            sharePointDocumentLocation.Description = "SharePoint Document Location created for storing documents related to case";
            sharePointDocumentLocation.AbsoluteURL = sharePointDocumentUrl;
            sharePointDocumentLocation.RegardingObjectId = new EntityReference("incident", incidentId);
			coreCrmService.Create(sharePointDocumentLocation);
            return true;
        }

Please refer these wonderful posts that were extremely helpful.

http://charlesemes.blogspot.com/2011/02/create-sharepoint-document-locations-in.html

http://geek.hubkey.com/2007/10/upload-file-to-sharepoint-document.html


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

6 thoughts on “Working with SharePointDocumentLocation in CRM 2011.”

  1. Hello Nishant

    I have similar requirement and I tried your code with one change, in CreateFolderForCase function I replaced applicantFolderName with contacFolderName.

    When I create new case, plugin completed its execution without throwing any error. Also, i can see a new record created under Sharepoint Document Location with correct Absolute URL.

    But when I try to click on Documents link from within Case left navigation pane I get an message saying “This location doesnt exist on Sharpoint Server. Change the location URL in Crm.” Also, If i go and check my sharpoint site, the folder structure is not created.

    Can you please help me out with this.

    Regards,
    Bhavika Patel

    Like

  2. Hi Nishant,

    This looks like just what I need but with custom entity, i’m not much of a developer and i have some problems to get this to work. Could you list the references used in this?

    Thanks
    Jussi

    Like

Share your thoughts

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 )

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