Using OData, will see an example to retrieve Security Roles of Logged In User.
Task: Retrieve Logged In User Security Roles on Onload of Contact Entity
Solution:
Step 1: Include Json2 web resource in the Contact Entity.
If you don’t have this file, download Latest SDK and go to below path and create,
\CRM SDK\SDK\SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts
Step 2: Create new JavaScript web resource (Ex: new_contact) and include the below script,
function getLoggedInUserRoles() { var Id = Xrm.Page.context.getUserId(); retrieveMultiple("SystemUserSet", "?$select=systemuserroles_association/Name&$expand=systemuserroles_association&$filter=SystemUserId eq (guid'" + Id + "')", getSecurityRoleNames, null, null); } function retrieveMultiple(odataSetName, filter, successCallback, errorCallback, _executionObj) { var context = Xrm.Page.context; var serverUrl = context.getServerUrl(); var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc"; //odataSetName is required, i.e. "AccountSet" if (!odataSetName) { alert("odataSetName is required."); return; } //Build the URI var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName; //If a filter is supplied, append it to the OData URI if (filter) { odataUri += filter; } //Asynchronous AJAX function to Retrieve CRM records using OData $.ajax({ type: "GET", async: true, contentType: "application/json; charset=utf-8", datatype: "json", url: odataUri, beforeSend: function(XMLHttpRequest) { //Specifying this header ensures that the results will be returned as JSON. XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function(data, textStatus, XmlHttpRequest) { if (successCallback) { if (data && data.d && data.d.results) { successCallback(data.d.results, textStatus, XmlHttpRequest); } else if (data && data.d) { successCallback(data.d, textStatus, XmlHttpRequest); } else { successCallback(data, textStatus, XmlHttpRequest); } } }, error: function(XmlHttpRequest, textStatus, errorThrown) { if (errorCallback) errorCallback(XmlHttpRequest, textStatus, errorThrown); else errorHandler(XmlHttpRequest, textStatus, errorThrown); } }); } function errorHandler(xmlHttpRequest, textStatus, errorThrow) { alert("Error : " + textStatus + ": " + xmlHttpRequest.statusText); } function getSecurityRoleNames(data, textStatus, XmlHttpRequest) { var totalCount = data[0].systemuserroles_association.results.length; var userString = null; if (totalCount > 0) { userString = "User Role : "; for (var i = 0; i < totalCount; i++) userString = userString + data[0].systemuserroles_association.results[i].Name + "\n"; alert(userString); } else alert("No Role Associated with LoggedIn User"); }
Step 3: Add new_contact Jscript web resource in the Contact Entity. Add getLoggedInUsersRoles function on Onload of Contact.
Step 4: Click on Ok. Save and Publish Contact Entity.
Step 5: Open any Contact record and see the below Popup.
Please provide your valuable comments on this article.
Discover more from Arun Potti's Power Platform blog
Subscribe to get the latest posts to your email.