Friday, 18 May 2012

Hiding Optionset (Picklist) values based on user role in MS CRM 2011

To achieve this let's assume we have a Optionset called "my_stage".Which has some values like
1)Won
2)Lost
3)Goal shared  etc.
Based on the user role I want to hide some Optionset value.Let assume user role does not allow him  to change the stage as 'Won'.So lets hide 'Won'  from Optionset.

Let's assume you want to hide it for "MyRoleName".

1.Optionset original values


2.Get current user Role using OData functions
  var roles = GetCurrentUserRoles();
    if (roles != null) {
        for (i = 0; i < roles.length; i++) {
                     
            if (roles[i] == "MyRoleName") {
                var PickListControl = Xrm.Page.getControl("my_stage");
// Provide the value of the Optionset item need to be hide 
//(7 for "Won" in my case) 
                PickListControl.removeOption(7);
            }
        }
    }

    function GetCurrentUserRoles()
     {
        var userRoleNamesArr = null;
        var CRMContext = GetContext();
    if (CRMContext != null) 
    {
        var currentuserid = CRMContext.getUserId();

        //Creating Odata Query
        var ODataSelect = "SystemUserSet(guid'" + currentuserid + "')/systemuserroles_association?$select=Name";
        var userrolenames = GetDataUsingODataServiceWithXmlHttp(ODataSelect);
        if (userrolenames != null && userrolenames.results.length > 0) 
        {
            userRoleNamesArr = new Array();
            for (var i = 0; i < userrolenames.results.length; i++)
                userRoleNamesArr[i] = userrolenames.results[i].Name;
        }
    }
    return userRoleNamesArr;
}

//Function to get the Context
   function GetContext() 
    {
    var _context = null;
    if (typeof GetGlobalContext != "undefined")
        _context = GetGlobalContext();
    else if (typeof Xrm != "undefined")
        _context = Xrm.Page.context;
    return _context
    }

// ========== OData Service - XML HTTP Request Functions Start ==========

    function GetDataUsingODataServiceWithXmlHttp(ODataSelect) {
    var resultOfGet = null;
    var CRMContext = GetContext();
    if (CRMContext != null)
     {
        var serverUrl = CRMContext.getServerUrl();
        var ODataEndpoint = "/XRMServices/2011/OrganizationData.svc";
        try {
            var ODataURL = serverUrl + ODataEndpoint + "/" + ODataSelect;
            var service = GetXMLHttpRequest();
            if (service != null) {
                service.open("GET", ODataURL, false);
                service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
                service.setRequestHeader("Accept", "application/json, text/javascript, */*");
                service.send(null);
                if (service.statusText == "OK")
                    resultOfGet = eval('(' + service.responseText + ')').d;
                else
                    DisplayError("ODataService", "Fetch", service.statusText);
            }
        }
        catch (e) 
        {
            DisplayError("ODataService", "Fetch", e.description);
            
        }
    }
    return resultOfGet;
}

 function GetXMLHttpRequest()
  {
    try {
        if (window.XMLHttpRequest) 
        {
            return new window.XMLHttpRequest;
        }
        else
         {
            try
         { 
              return new ActiveXObject("MSXML2.XMLHTTP.3.0");
         }
            catch (ex) 
            {
                return null;
            }
        }
    } catch (e) 
    {
        DisplayError("XML Http request ", "Request", e.description);
    }
}
  function DisplayError(cntrl, func, err)
   {
    alert("Control : " + cntrl + "\n " + func + "\nError : " + err);
   }

//============== Javascript function End ================


Note :Use this javascript code on Form_onload event. It will hide the Optionset item "Won" for the role "MyRoleName". Optionset values after applying javascript.

Hope it will help You.

 Thanks,
 Yusuf

1 comment:

  1. No one can lie, no one can hide anything, when he looks directly into someone's eyes. See the link below for more info.

    #hide
    www.ufgop.org

    ReplyDelete