Friday, 23 December 2016

Generic code to show SharePoint documents subgrid on main entity form in Dynamics CRM

Hello!

Many times we have faced the ask from our customers to show the SharePoint document sub-grid on the main form for an entity. I have come up with a generic code that shows the SharePoint sub-grid on the main form (taking only the tab name, entity schema name, record GUID and the IFrame name).

Please note that the code is only for the sub-grids created by using the SharePoint list component and as such it is not supported as we are hard-coding the list component parameters in the URL. So use it at your own risk:

/* Show the documents tab on the main entity form based on the entities that have been provided */
/* This method has dependency on XrmServiceToolkit.js */
common.showDocumentsGridOnMainEntityForm = function (documentGridTabName, entityLogicalName, recordGuid, documentGridIframeName) {
    if (Xrm && Xrm.Page && Xrm.Page.ui) {
        var CREATE_FORM_TYPE = 1;
        var formType = Xrm.Page.ui.getFormType();
        if (formType != CREATE_FORM_TYPE) {
            debugger;
            var sharePointSitesFetch = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
                "<entity name='sharepointsite'>" +
                "<attribute name='name'/>" +
                "<attribute name='parentsite'/>" +
                "<attribute name='relativeurl'/>" +
                "<attribute name='absoluteurl'/>" +
                "<attribute name='validationstatus'/>" +
                "<attribute name='isdefault'/>" +
                "<order descending='false' attribute='name'/>" +
                "<filter type='and'>" +
                "<condition attribute='isdefault' value='1' operator='eq'/>" +
                "<condition attribute='absoluteurl' operator='not-null'/>" +
                "</filter>" +
                "</entity>" +
                "</fetch>";
            var sharePointSiteRecords = XrmServiceToolkit.Soap.Fetch(sharePointSitesFetch);
            var sharePointBaseUrl = "";
            if (sharePointSiteRecords && sharePointSiteRecords.length > 0) {
                sharePointBaseUrl = sharePointSiteRecords[0].attributes["absoluteurl"].value;
            }

            var showDocumentsTab = false;
            if (sharePointBaseUrl) {
                var documentLocationsFetch = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
                    "<entity name='sharepointdocumentlocation'>" +
                    "<attribute name='name'/>" +
                    "<attribute name='regardingobjectid'/>" +
                    "<attribute name='parentsiteorlocation'/>" +
                    "<attribute name='relativeurl'/>" +
                    "<attribute name='absoluteurl'/>" +
                    "<order descending='false' attribute='name'/>" +
                    "<filter type='and'>" +
                    "<condition attribute='regardingobjectid' value='" + recordGuid + "' uitype='" + entityLogicalName + "' operator='eq'/>" +
                    "</filter>" +
                    "</entity>" +
                    "</fetch>";
                var documentLocationsForRecord = XrmServiceToolkit.Soap.Fetch(documentLocationsFetch);
                var documentLocationRelativeUrl = "";
                if (documentLocationsForRecord && documentLocationsForRecord.length > 0) {
                    documentLocationRelativeUrl = documentLocationsForRecord[0].attributes["relativeurl"].value;
                }

                var completeFolderUrl = "";
                if (documentLocationRelativeUrl) {
                    // Sample: http://192.168.85.9:8080/crmgrid/crmgridpage.aspx?langId=en-US&locationUrl=http%3a%2f%2f192.168.85.9%3a8080%2fcontoso_pricingproposal%2f_F66CBD47B6B8E61180D9000D3AA06EB4&pageSize=250
                    completeFolderUrl = sharePointBaseUrl + "crmgrid/crmgridpage.aspx?langId=en-US&locationUrl=" + sharePointBaseUrl + entityLogicalName + "/" + documentLocationRelativeUrl + "&pageSize=250";
                    console.log("The complete folder URL is: " + completeFolderUrl);
                    completeFolderUrl = encodeURI(completeFolderUrl);
                    console.log("The complete folder URL after encoding: " + completeFolderUrl);
                }

                if (completeFolderUrl) {
                    showDocumentsTab = true;
                }

                if (showDocumentsTab) {
                    if (Xrm.Page.ui.tabs) {
                        var documentGridTab = Xrm.Page.ui.tabs.get(documentGridTabName);
                        if (documentGridTab) {
                            documentGridTab.setVisible(true);
                            var documentIFrame = Xrm.Page.ui.controls.get(documentGridIframeName);
                            if (documentIFrame != null) {
                                documentIFrame.setSrc(completeFolderUrl);
                            }
                        }
                    }
                }
            }
        }
    }
}


Sample usage code:

/* Method to call common method to show sharepoint documents in the main entity form*/
pricingproposal.showDocumentsInMainform = function () {
    var recordGuid = Xrm.Page.data.entity.getId();
    if (recordGuid && common.isValidGuidHelper(recordGuid)) {
        common.showDocumentsGridOnMainEntityForm("tab_DocumentsTab", "contoso_pricingproposal", recordGuid, "IFRAME_Documents");
    }
}

As part of the configurations we need to create a tab in the main form inside which we will have an IFrame. These names will be part of the function parameter as shown above.

Let me know if you have any questions.

Merry Christmas and happy Dynamics!


No comments:

Post a Comment