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


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

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

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

  1. 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

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

Please share your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.