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:
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:
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.
//// ----------------------------------------------- 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: