There is a very common requirement in dynamics CRM to hide
some optionset values based on some other entity’s privileges. We have a
requirement to hide approval option from status field in case entity if current
user doesn’t have write permission for waiver rights entity.
Here is code sample :
// function call
RetrieveUserPrivileges("37DC5BE0-B813-E311-9A5C-00221960BC47", "prvWriteNew_waiverrights");
// function definition
function RetrieveUserPrivileges(_UserId, _PrivilegeName) {
var _RequestMain = "";
_RequestMain += " <request i:type=\"b:RetrieveUserPrivilegesRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
_RequestMain += " <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
_RequestMain += " <a:KeyValuePairOfstringanyType>";
_RequestMain += " <c:key>UserId</c:key>";
_RequestMain += " <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + _UserId + "</c:value>";
_RequestMain += " </a:KeyValuePairOfstringanyType>";
_RequestMain += " </a:Parameters>";
_RequestMain += " <a:RequestId i:nil=\"true\" />";
_RequestMain += " <a:RequestName>RetrieveUserPrivileges</a:RequestName>";
_RequestMain += " </request>";
// Get privilegeid based on Name
var d = fncLookupValueFromEntity("privilege", "name", _PrivilegeName, "", "", "privilegeid");
var flag = false;
if (d != null) {
var _RestultXml = XrmServiceToolkit.Soap.Execute(_RequestMain);
if ($(_RestultXml).find("b\\:RolePrivilege").length > 0) {
$(_RestultXml).find("b\\:RolePrivilege").each(function () {
//inner loop
$(this).find("b\\:PrivilegeId").each(function () {
var _PrivilegeId = $(this).text();
if (_PrivilegeId.toString().toLowerCase() == d.toLowerCase()) {
flag = true;
}
});
});
}
else if ($(_RestultXml).find("RolePrivilege").length > 0) {
$(_RestultXml).find("RolePrivilege").each(function () {
//inner loop
$(this).find("PrivilegeId").each(function () {
var _PrivilegeId = $(this).text();
if (_PrivilegeId.toString().toLowerCase() == d.toLowerCase()) {
flag = true;
}
});
});
}
// If there user don't have Privilege remove status option
flag == false && Xrm.Page.ui.controls.get("statuscode").removeOption(100000)
}
}
catch (ex) {
alert(ex.message);
}
}
function fncLookupValueFromEntity(a, b, c, d, e, g) {
var m = "";
"" != d && (m = "<condition attribute='" + d + "' value='" + e + "' operator='eq'/>");
var fetchXml = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" +
"<entity name='" + a + "'>" +
"<attribute name='" + g + "'/>" +
"<filter type='and'>" +
"<condition attribute='" + b + "' value='" + c + "' operator='eq'/>" + m +
"</filter></entity></fetch>";
a = XrmServiceToolkit.Soap.Fetch(fetchXml);
if (a.length > 0) {
if (a[0].attributes[g] != null) {
return a[0].attributes[g].value;
}
}
return null
}
Note: Please attach Jquery web resource/library on Form libraries
list.