If you have CRM and SharePoint, one of the easiest things you can do is to link the two together so that you can manage your documents in SharePoint, but access, create, and interact with them in CRM.  There are a few reasons you would want to do this.  One is for a single document location for all related information, another would be cost.  If you are using CRM online, the storage can get expensive and SharePoint may provide a better way to do that.  Document management is a core functionality in SharePoint and it is really good at it, so you might as well leverage that.

The default configuration puts all your CRM documents in one SharePoint site.  You can organize things into libraries and folders, but we already have a SharePoint site for each of our accounts, so we wanted the documents to be in those already existing sites.

The first thing you need to do is log into your CRM instance as an administrator.  Once there, head to the Administration section, then select Document Management from the Settings menu.

image

Before you can connect CRM and SharePoint you will need to install the SharePoint list component.  This will work from on premise to online either way as long as the two systems can communicate.  If you have SharePoint online, this list component is likely already installed and configured.  The Install List Component link will take you to a download page where you can get the WSP for the list component as well as instructions for getting this installed in your SharePoint environment.  It is very straightforward.

Once you get the component installed, click on the Document Management Settings link.

image

For this example, I’m only going to sync the Accounts entity, but you could easily apply this to any other entities you need.  I unchecked all entities except for Account and set the URL to the root site for my SharePoint site.  The documents won’t be stored here, but this connection is required so the two systems know they are connected.

image


You should get a confirmation that your URL is valid and that the list component is installed.  The next thing to do is to customize the Account entity form to add some JavaScript.  This Script will look at the Account name, check to see if there is an entry in the SharePoint Document Locations entity for this account and create it if it doesn’t exist.  This script does not do any checking to see if the SharePoint site actually exists, it just assumes it does.  I created my SharePoint sites just using the first word from the account name for demo purposes, so you may need to adjust how you create the URL to your SharePoint site.

You will need to create two web resources, one for json2 and one for your custom JavaScript that will run on the Account entity form.  See the bottom of this post for the code to put in this account JavaScript.  This will rely on jQuery, but it is already loaded by CRM.  This post has a good explanation of how to create web resources, so I won’t go into that again.

After you have your web resources in place, head back to your Account list.  From the ellipses, click Customize Entity, click the Forms node in the left navigation, then double-click the form with Main in the Form Type column.

image  image  image

Click the Form Properties button in the ribbon and add your web resources to the form.  Note, they need to be in the correct order so dependencies are loaded correctly.  You will need to also add the event in the Event Handlers section.

image




Once you have this set up correctly, click Save, then click Publish from the ribbon.

Head back to an account, that should trigger the JavaScript to create the document link.  From the ribbon, click the Documents button to test your connection.

image

You should see something similar to this below.  You can upload, edit, create or delete documents in SharePoint from here.  There is also a link to jump over to the SharePoint site directly if you need to.

image

You can see and update all of your existing connections in from the Document Management section of settings by clicking on SharePoint Document Locations.

image

And now, finally the JavaScript code to make the magic happen.

   1: var acctName, acctShortName, spURL, acctId
   2: function form_OnLoad(){
   3:     acctName = Xrm.Page.getAttribute("name").getValue()
   4:     acctShortName = acctName.split(" ")[0];
   5:     spURL = "https://yoursite.sharepoint.com/" + acctShortName + "/Shared%20Documents";
   6:     acctId = Xrm.Page.data.entity.getId();
   7:     CheckDocLocation(acctId);
   8: }
   9:  
  10: //Check to see if the document location already exists
  11: function CheckDocLocation(ACTID) {
  12:     //OData URI to get address information from parent account record
  13:     var oDataURI = Xrm.Page.context.getClientUrl()
  14:         + "/XRMServices/2011/OrganizationData.svc/"
  15:         + "SharePointDocumentLocationSet"
  16:         + "?$select=Name"
  17:         + "&$filter=RegardingObjectId/Id eq (guid'" + ACTID + "')";
  18:     //Asynchronous XMLHttpRequest to retrieve account record
  19:     var req = new XMLHttpRequest();
  20:     req.open("GET", encodeURI(oDataURI), true);
  21:     req.setRequestHeader("Accept", "application/json");
  22:     req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  23:     req.onreadystatechange = function () {
  24:         if (this.readyState == 4 /* complete */) {
  25:             req.onreadystatechange = null; //avoids memory leaks
  26:             if (this.status == 200) {
  27:                 //parse the response string as a JSON object into the successCallback method.
  28:                 if(JSON.parse(this.responseText).d.results[0])
  29:                 {
  30:                     //alert("found one" + JSON.parse(this.responseText).d.results[0].Name);
  31:                 } else {
  32:                     //alert("not found.  create link");
  33:                     CreateDocLocation(acctShortName,spURL,acctId,acctName);
  34:                 }
  35:             }
  36:             else {
  37:                 alert("Lookup to SharePoint Doc Location entity failed");
  38:             }
  39:         }
  40:     };
  41:     req.send();
  42: }
  43:  
  44: //Create the document location tied to this account
  45: function CreateDocLocation(ACTSHORTNAME,SITEURL,ACTID,ACTNAME) {
  46:     var serverUrl = Xrm.Page.context.getServerUrl().toString();
  47:     var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
  48:     var ODATA_EntityCollection = "/SharePointDocumentLocationSet";
  49:  
  50:     var objDocLib = new Object();
  51:     // set the name of Account
  52:     objDocLib.Name = "Main SharePoint Account Library"; //ACTSHORTNAME;
  53:     objDocLib.AbsoluteURL = SITEURL;
  54:     objDocLib.RegardingObjectId = { Id: ACTID, LogicalName: "account", Name: ACTNAME };
  55:  
  56:     // Parse the entity object into JSON 
  57:     var jsonEntity = window.JSON.stringify(objDocLib);
  58:  
  59:     $.ajax({
  60:         type: "POST",
  61:         contentType: "application/json; charset=utf-8",
  62:         datatype: "json",
  63:         url: serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection,
  64:         data: jsonEntity,
  65:         beforeSend: function (XMLHttpRequest) {
  66:             XMLHttpRequest.setRequestHeader("Accept", "application/json");
  67:         },
  68:         success: function (response) {
  69:             if (response != null && response.d != null) {
  70:                 //alert("Success!");
  71:             }
  72:         },
  73:         error: function (xmlHttpRequest, textStatus, errorThrown) {
  74:             alert("Document set location fail!  -  Status: " + textStatus + "; ErrorThrown: " + errorThrown);
  75:         }
  76:     });
  77: }

 

For more information about C5 Insight or this blog entry, please Contact Us