Monday, June 8, 2015

Get Sharepoint absolute path in MS CRM using Javascript

The purpose of this code is used to get the sharepoint folder location of the particular case and save the URL in the particular field. For that, here i using rest call to retrieve the document location and absolute URL of the sharepoint.

Before adding this code you need to add REST.js in the case form properties. And add the below code in form. Then call GetAbsoluteURLFromCase() method in the onload of the page.

var caseId = "";
var relativeURL = "";
var finalUrl = "";
var absoluteUrl = "";

function GetAbsoluteUrlFromCase() {
caseId = Xrm.Page.data.entity.getId();
if(caseId == "")
    return;
    try {
        var options = "$select=AbsoluteURL,RelativeUrl,ParentSiteOrLocation&$filter=RegardingObjectId/Id eq (guid'" + caseId + "')";
        SDK.REST.retrieveMultipleRecords("SharePointDocumentLocation", options, retrieveDocumentLocationCallBack, errorHandler, DocumentLocationRetrieveComplete);
    }
    catch (error) {
        errorHandler(error);
    }
}

function retrieveDocumentLocationCallBack(retrievedDocumentLocation) {
    if (retrievedDocumentLocation.length > 0) {
        for (var i = 0; i < retrievedDocumentLocation.length; i++) {
            var documentLocation = retrievedDocumentLocation[i];
            var parentLocation = documentLocation.ParentSiteOrLocation;

            if (parentLocation.Id != "") {
                finalUrl = documentLocation.RelativeUrl + "/" + finalUrl;
                relativeURL = documentLocation.RelativeUrl + "/";
                GetParentDir(parentLocation);
            }
            else {
                finalUrl = documentLocation.AbsoluteURL + "/" + finalUrl;
                absoluteUrl = documentLocation.AbsoluteURL;
            }
        }
    }
}

function GetParentDir(parentRecord) {
    try {
        if (parentRecord.LogicalName == "sharepointdocumentlocation")
            SDK.REST.retrieveRecord(parentRecord.Id, "SharePointDocumentLocation", null, null, retreiveParentLocationCallBack, errorHandler);
        else if (parentRecord.LogicalName == "sharepointsite")
            SDK.REST.retrieveRecord(parentRecord.Id, "SharePointSite", null, null, retreiveSharePointCallBack, errorHandler);
    }
    catch (error) {
        errorHandler(error)
    }
}

function retreiveParentLocationCallBack(retreivedLocation) {
    try {
        if (retreivedLocation.ParentSiteOrLocation.Id != null) {
            finalUrl = retreivedLocation.RelativeUrl + "/" + finalUrl;
            relativeURL = retreivedLocation.RelativeUrl + "/" + relativeURL
            GetParentDir(retreivedLocation.ParentSiteOrLocation);
        }
        else {
            finalUrl = retreivedLocation.AbsoluteURL + "/" + finalUrl;
            absoluteUrl = retreivedLocation.AbsoluteURL;
        }
    }
    catch (error) {
        errorHandler(error)
    }
}

function retreiveSharePointCallBack(retrievedSiteUrl) {
    try {
        if (retrievedSiteUrl.ParentSite.Id != null) {
            finalUrl = retrievedSiteUrl.RelativeUrl + "/" + finalUrl;
            GetParentDir(retrievedSiteUrl.ParentSite);
        }
        else {
            finalUrl = retrievedSiteUrl.AbsoluteURL + "/" + finalUrl;
            absoluteUrl = retrievedSiteUrl.AbsoluteURL;              
        }
    }
    catch (error) {
        errorHandler(error)
    }
}

function errorHandler(error) {
alert(error.message);
}


2 comments:

  1. Great post. Have you ever tried to save new files from Javascript (or CRM plugin) to the related Sharepoint site?

    ReplyDelete