Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Showing Documents tab on form - script error in UCI

Posted on by 1,320

I'm using the js provided on this blog to display via iframe the Documents tab on the account form. This is working in the web interface, but I get an error in the new UI. Any help would be appreciated.

/// Show Documents subgrid on the main form
function setDocumentsIFrame(executionContext, tabName, iframeName) {
    var formContext = executionContext.getFormContext();

    // Hide the tab until we're sure there's something to show
    var tab = formContext.ui.tabs.get(tabName);
    if (tab === null) {
        console.log("Could not find tab: " + tabName);
        return;
    }
    tab.setVisible(false);

    // Only want to run the code if the record is just being created, otherwise
    // there will no documents to show anyway
    if (formContext.ui.getFormType() !== 1) {
        var iframe = formContext.getControl(iframeName);
        if (iframe === null) {
            console.log("Could not find iframe: " + iframeName);
            return;
        }

        var id = formContext.data.entity.getId().replace("{", "").replace("}", "");

        // Query SharePoint Documents Locations to find any records which are related to the 
        // record we are currently viewing
        var req = new XMLHttpRequest();
        req.open("GET",
            formContext.context.getClientUrl() +
            "/api/data/v8.0/sharepointdocumentlocations?$select=_regardingobjectid_value&$filter=_regardingobjectid_value eq " +
            id, true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var results = JSON.parse(this.response);
                    if (results.value.length > 0) {
                        // If we have any related items then show the tab and set the IFrame URL accordingly
                        tab.setVisible(true);
                        var url = formContext.context.getClientUrl() +
                            "/userdefined/areas.aspx?formid=ab44efca-df12-432e-a74a-83de61c3f3e9&inlineEdit=1&navItemName=Documents&oId=%7b" +
                            formContext.data.entity.getId().replace("{", "").replace("}", "") +
                            "%7d&oType=" +
                            formContext.context.getQueryStringParameters().etc +
                            "&pagemode=iframe&rof=true&security=852023&tabSet=areaSPDocuments&theme=Outlook15White";
                        iframe.setSrc(url);
                    } else {
                        tab.setVisible(false);
                    }

                } else {
                    alert(this.statusText);
                }
            }
        };
        req.send();
    }
}

818786.Capture.PNG

*This post is locked for comments

  • Suggested answer
    Charles Abi Khirs Profile Picture
    Charles Abi Khirs 3,569 on at
    RE: Showing Documents tab on form - script error in UCI

    Hello,

    The error is in this line of code "formContext.context.getQueryStringParameters().etc +" because in the UCI it will return null.

    You have to put &oType=1 instead.

  • Sebastian Profile Picture
    Sebastian 10 on at
    RE: Showing Documents tab on form - script error in UCI

    Hello,

    seems to me like the "formContext.context.getQueryStringParameters().etc" property returns null/undefined. To solve the problem you can either hard code the value or do it with this workaround (insert the following code into the "onreadystatechange" function of the first request):

    var req2 = new XMLHttpRequest();

                               req2.open("GET", context.getClientUrl() + requestUrl, true);

                               req2.setRequestHeader("OData-MaxVersion", "4.0");

                               req2.setRequestHeader("OData-Version", "4.0");

                               req2.setRequestHeader("Accept", "application/json");

                               req2.setRequestHeader("Content-Type", "application/json; charset=utf-8");

                               req2.onreadystatechange = function () {

                                   if (this.readyState === 4) {

                                       req.onreadystatechange = null;

                                       if (this.status === 200) {

                                           var result = JSON.parse(this.response);

                                           var oType= result.value[0].ObjectTypeCode;

    tab.setVisible(true);

                           var url = formContext.context.getClientUrl() +

                               "/userdefined/areas.aspx?formid=ab44efca-df12-432e-a74a-83de61c3f3e9&inlineEdit=1&navItemName=Documents&oId=%7b" +

                               formContext.data.entity.getId().replace("{", "").replace("}", "") +

                               "%7d&oType=" +

                               oType +

                               "&pagemode=iframe&rof=true&security=852023&tabSet=areaSPDocuments&theme=Outlook15White";

                           iframe.setSrc(url);

    } else {

                                           var errorText = this.responseText;

                                           //handle error here

                                       }

                                   }

                               };

                               req2.send();

    Best regards,

    Sebastian

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Showing Documents tab on form - script error in UCI

    Did you find a workaround?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans