Retrieve Absolute and Site Collection URL Request in Dynamics CRM

By | October 12, 2016

In Microsoft Dynamics CRM, many emails and notes along with attachment gets created on daily basis, which results into increase in the CRM database storage which is expensive.

So usually, people go with the integration of Dynamics CRM with SharePoint which reduces the CRM database storage.

We receive many such requests where client wants to move the files from emails or notes attachments to SharePoint.

So while integrating i.e. moving file from CRM to SharePoint, we require SharePoint URL.

For getting the SharePoint URL, we generally retrieve the default site and using this default site we need to generate the absolute URL.

Instead of doing this i.e. retrieving default site, CRM has a request “RetrieveAbsoluteAndSiteCollectionUrlRequest” using which we can get the absolute URL.

This request requires the document location id of a specific entity record. So before executing this request, we need to retrieve the Document Location record which is then passed to “RetrieveAbsoluteAndSiteCollectionUrlRequest” request to get the absolute URL.

As you know, Document location only stores the relative URL. See below screenshot.Retrieve absolute

Here we take an example of account entity.

Below is the code to retrieve the Document location record against the account.

private EntityCollection RetrieveLocation(IOrganizationService service, Guid accountId)
        {
            EntityCollection locationColl = null;
            string fetchXML = string.Empty;
            try
            {
                //contruct the fetch query
                fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'>" +
                                      "<entity name='sharepointdocumentlocation'>" +
                                       " <attribute name='sharepointdocumentlocationid' />" +
                                        "<attribute name='relativeurl' />" +
                                        "<filter type='and'>" +
                                         " <condition attribute='regardingobjectid' operator='eq' value=' " + accountId + " ' />" +
                                        "</filter>" +
                                      "</entity>" +
                                    "</fetch>";

                //retrieve the location records
                locationColl = service.RetrieveMultiple(new FetchExpression(fetchXML));
            }
            catch (FaultException ex)
            {
                throw new FaultException(functionName + " Error " + ex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception(functionName + " Error " + ex.Message);
            }
            return locatioColl;
        }

The above function will return the document location collection and this collection can be passed to request to get the absolute URL.

RetrieveAbsoluteAndSiteCollectionUrlRequest retrieveRequest = new RetrieveAbsoluteAndSiteCollectionUrlRequest
                {
                    Target = new EntityReference(SharePointDocumentLocation.EntityLogicalName, locatioColl[0].Id)
                };
                RetrieveAbsoluteAndSiteCollectionUrlResponse retriveResponse = (RetrieveAbsoluteAndSiteCollectionUrlResponse)proxy.Execute(retrieveRequest);

                Console.WriteLine("Absolute URL of document location record is '{0}'.", retriveResponse.AbsoluteUrl.ToString());
                Console.WriteLine("Site Collection URL of document location record is '{0}'.", retriveResponse.SiteCollectionUrl.ToString());

In above code, we have passed the retrieved SharePoint document location record id which will return us the Absolute URL and site collection URL.

Conclusion:

The request “RetrieveAbsoluteAndSiteCollectionUrlRequest” was introduced in 2013 and it can be used to get SharePoint absolute URL.

Now do Accounting without leaving your Dynamics CRM – Try InoLink ( Integrating QuickBooks with Dynamics CRM)!