Dynamics 365: JavaScript Enabled Button For Box.com Using KingswaySoft And Ribbon Workbench – Unified Interface Compatibility

Patrick McClureDyn365CE4 years ago21 Views

Recently I was tasked with devising a way where our end users could click a button located on an entity form in Dynamics 365 that would launch our Enterprise Box.com file sharing site.  This button would be dynamic, driven via JavaScript, and would open the related Box.com folder location that would be dependent upon the entity record the button was initiated from.

Series Links Parts 1-4:

I wanted to quickly clarify that this project does in fact work in Dynamics 365 Online and in the new Unified Interface that Microsoft will soon be forcing upon everyone end of year 2020.  However, in order to get this to work correctly, one small JavaScript code change is required.  I wanted to make sure that everyone understands that this small coding change is only needed because we are passing in the form context to the ribbon actions.  Below I have included the entire script from my Dynamics 365: JavaScript Enabled Button For Box.com Using KingswaySoft And Ribbon Workbench – Part 4 post with the required modification.  Yes, it is a very simply change.

Modify Code:

  • Before: var formContext=primaryControl.getFormContext(); 
  • After: var formContext=primaryControl; 
    var siteCollectionId="{YOURSITECOLLECTIONGUID}";
    var formContext=primaryControl; // get formContext
    var lookupId=formContext.data.entity.getId();
    //alert("lookupFieldName: "+lookupFieldName+";");
    if(typeof (lookupFieldName)!="undefined"&&lookupFieldName!=null) {
      var lookupField=formContext.getAttribute(lookupFieldName);
      if(typeof (lookupField)!="undefined"&&lookupField!=null) {
        //alert("Name: "+lookupField.getValue()[0].name+"; Id: "+lookupField.getValue()[0].id);
        lookupId=lookupField.getValue()[0].id;
      }
    }

Helpful Websites:

Complete JavaScript Code – Revised

Below is the entire updated script you’ll need to get your custom Ribbon Workbench button to function correctly in the new and soon to be required Unified Interface.  Make changes and upload over the existing file in Dynamics 365 as a Web Resource.

  • SiteCollectionId – Site collection defined in Dynamics 365
  • Cloud Service Url – Your cloud service Url path
  • Cloud Service Default Folder Value – For Box.com this is an integer value given to the folder when it was created; so essentially it’s the Box Id
//// -----------------------------------------------
if (typeof (DEV) == "undefined") {
  DEV = { __namespace: true };
}
//// ----------------------------------------------------------
if (typeof (DEV.Box) == "undefined") {
  DEV.Box = { __namespace: true };
}
//// --- ONCLICK ROUTINES---------------------------------------
DEV.Box.Link ={
  OnClick:function(primaryControl,lookupFieldName) {
    var siteCollectionId="{YOURSITECOLLECTIONGUID}";
    var formContext=primaryControl; // get formContext
    var lookupId=formContext.data.entity.getId();
    //alert("lookupFieldName: "+lookupFieldName+";");
    if(typeof (lookupFieldName)!="undefined"&&lookupFieldName!=null) {
      var lookupField=formContext.getAttribute(lookupFieldName);
      if(typeof (lookupField)!="undefined"&&lookupField!=null) {
        //alert("Name: "+lookupField.getValue()[0].name+"; Id: "+lookupField.getValue()[0].id);
        lookupId=lookupField.getValue()[0].id;
      }
    }
    if(typeof (lookupId)!="undefined"&&lookupId!=null) {
      var oDataURI=Xrm.Utility.getGlobalContext().getClientUrl()
        +"/api/data/v9.1/"
        +"sharepointdocumentlocations"
        +"?$select="
        +"sharepointdocumentlocationid,"
        +"name,"
        +"absoluteurl"
        +"&$filter=_regardingobjectid_value eq "+lookupId.slice(1,-1)
        +" and sitecollectionid eq "+siteCollectionId.slice(1,-1);
      var req=new XMLHttpRequest(); /* Async Call */
      req.open("GET",encodeURI(oDataURI),true);
      req.setRequestHeader("Accept","application/json");
      req.setRequestHeader("Content-Type","application/json; charset=utf-8");
      req.setRequestHeader("OData-MaxVersion","4.0");
      req.setRequestHeader("OData-Version","4.0");
      req.setRequestHeader("Prefer","odata.include-annotations="*"");
      req.onreadystatechange=function() {
        if(this.readyState==4) {/* Complete */
          req.onreadystatechange=null; /* Avoids memory leaks */
          if(this.status==200) {
            var results=JSON.parse(this.response);
            var bFound=false;
            for(var i=0; i<results.value.length; i++) { var name=results.value[i]["name"]; var absoluteUrl=results.value[i]["absoluteurl"]; if(typeof (absoluteUrl)!="undefined"&&absoluteUrl!=null) { bFound=true; window.open(absoluteUrl,'_blank','width=800,height=600,top=200'); } else {// Default Box Client Directiory [All Files>Client]
                bFound=true;
                window.open("https://[YOURSITEHERE].[CLOUD_DOMAIN].com/folder/[ROOTFOLDERHERE]",'_blank','width=800,height=600,top=200');
              }
            }
            if(bFound==false) {// Nothing found then open default location
              window.open("https://[YOURSITEHERE].[CLOUD_DOMAIN].com/folder/[ROOTFOLDERHERE]",'_blank','width=800,height=600,top=200');
            }
          } else {
            var resultError=JSON.parse(this.response).error; // Use to throw an error
            var msg="DEV.Box.Link.OnClick() Error: Unable to return Box Drive, Error Message: "+resultError.message+"; Lookup Id: "+lookupId+".";
            var alertStrings={confirmButtonLabel:"OK",text:msg};
            var alertOptions={height:180,width:390};
            Xrm.Navigation.openAlertDialog(alertStrings,alertOptions).then(
              function success(result) {
                console.log("Error alert dialog closed");
              },
              function(resultError) {
                console.log(resultError.message);
              }
            );
          }
        }
      };
      req.send();
    }
  }
};

Series Links Parts 1-4:

Original Post https://therapyincode.wordpress.com/2020/03/09/dynamics-365-javascript-enabled-button-for-box-com-using-kingswaysoft-and-ribbon-workbench-unified-interface-compatibility/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...