locked
Hide Button on form ribbon depending on security role RRS feed

  • Question

  • Hi,

    I have a custom entity form. On this form i have a button. I want to hide this button depending on the security role. Can anyone suggest me the process to do it.

    Regards,

    Kishan.

    Monday, October 3, 2011 6:33 AM

All replies

  • You will find few blog posts around this topic..

    Check out following;

    http://dynamicscrm2011.wordpress.com/tag/enable-crm-2011-ribbon-button/

    http://howto-mscrm.blogspot.com/2011/04/how-to-series-6-how-to-use-customrule.html


    MS CRM MVP :: uMar Khan :: Microsoft CRM Consultant (Blog :: http://umarkhan.wordpress.com)
    Monday, October 3, 2011 6:50 AM
    Moderator
  • Hi,

    I have written the below code

    function UserHasRole(roleName)
    {
    debugger
        var serverUrl = Xrm.Page.context.getServerUrl();

        var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
        oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";

        var service = GetRequestObject();

        if (service != null)
        {
            service.open("GET", oDataEndpointUrl, false);
            service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
            service.setRequestHeader("Accept", "application/json, text/javascript, */*");
            service.send(null);

            var requestResults = eval('(' + service.responseText + ')').d;

            if (requestResults != null && requestResults.length == 1)
            {
                var role = requestResults[0];

                var id = role.RoleId;

                var currentUserRoles = Xrm.Page.context.getUserRoles();

                for (var i = 0; i < currentUserRoles.length; i++)
                {
                    var userRole = currentUserRoles[i];

                    if (GuidsAreEqual(userRole, id))
                    {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    function GetRequestObject()
    {
        if (window.XMLHttpRequest)
        {
            return new window.XMLHttpRequest;
        }
        else
        {
            try
            {
                return new ActiveXObject("MSXML2.XMLHTTP.3.0");
            }
            catch (ex)
            {
                return null;
            }
        }
    }

    function GuidsAreEqual(guid1, guid2)
    {
        var isEqual = false;

        if (guid1 == null || guid2 == null)
        {
            isEqual = false;
        }
        else
        {
            isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
        }

        return isEqual;
    }

    function callMain()
    {
    if(UserHasRole("System Administrator"))
    {
    alert('System Administrator Role');
    }
    }

    It is giving error at  service.open("GET", oDataEndpointUrl, false).

    Please see my code once if there are any mistake.

    Monday, October 3, 2011 8:44 AM
  • debugger // missing ;

    What's the error message?

     


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    Monday, October 3, 2011 9:02 AM
  • Javascript is giving message break continue ignore at service.open("GET", oDataEndpointUrl, false)
    Monday, October 3, 2011 9:18 AM
  • Hi,

    I am using thr below code

    function UserHasRole(roleName)

     

    {

    var serverUrl = Xrm.Page.context.getServerUrl();

    var oDataEndpointUrl = "http://192.168.64.101:5555/Choice/XRMServices/2011/OrganizationData.svc/";

    oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";

    var service = GetRequestObject();

    if (service != null)

    {

    service.open("GET", oDataEndpointUrl, false);

    service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");

    service.setRequestHeader("Accept", "application/json, text/javascript, */*");

    service.send(null);

    var requestResults = eval('(' + service.responseText + ')').d;

    if (requestResults != null && requestResults.length == 1)

    {

    var role = requestResults[0];

    var id = role.RoleId;

    var currentUserRoles = Xrm.Page.context.getUserRoles();

    for (var i = 0; i < currentUserRoles.length; i++)

    {

    var userRole = currentUserRoles[i];

    if (GuidsAreEqual(userRole, id))

    {

    return true;

    }

    }

    }

    }

    return false;

    }

    I am getting stop at the if condition checking requestResults. if condition is failing at length.

     

    Tuesday, October 4, 2011 9:37 AM
  • Hi Kishan,

    I run the code not from the last post but from the original post. I make few changes and it is working. Here is a screen shot

    I changed the following lines with

     if (requestResults != null && requestResults.length == 1)
            {
                var role = requestResults[0];

    with

     if (requestResults != null && requestResults.results.length == 1)
            {
                var role = requestResults.results[0];

    Here is the code

    function UserHasRole(roleName)
    {
    //debugger;
        var serverUrl = Xrm.Page.context.getServerUrl();

        var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
        oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";

        var service = GetRequestObject();

        if (service != null)
        {
            service.open("GET", oDataEndpointUrl, false);
            service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
            service.setRequestHeader("Accept", "application/json, text/javascript, */*");
            service.send(null);

            var requestResults = eval('(' + service.responseText + ')').d;

            if (requestResults != null && requestResults.results.length == 1)
            {
                var role = requestResults.results[0];

                var id = role.RoleId;

                var currentUserRoles = Xrm.Page.context.getUserRoles();

                for (var i = 0; i < currentUserRoles.length; i++)
                {
                    var userRole = currentUserRoles[i];

                    if (GuidsAreEqual(userRole, id))
                    {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    function GetRequestObject()
    {
        if (window.XMLHttpRequest)
        {
            return new window.XMLHttpRequest;
        }
        else
        {
            try
            {
                return new ActiveXObject("MSXML2.XMLHTTP.3.0");
            }
            catch (ex)
            {
                return null;
            }
        }
    }

    function GuidsAreEqual(guid1, guid2)
    {
        var isEqual = false;

        if (guid1 == null || guid2 == null)
        {
            isEqual = false;
        }
        else
        {
            isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
        }

        return isEqual;
    }

    function callMain()
    {
    if(UserHasRole("System Administrator"))
    {
    alert('System Administrator Role');
    }
    }

    I hope this works for you.

    If this answer your question please mark this post as answered.

    I ll really appreciate that.

    I hope this helps.


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    • Proposed as answer by Amreek Singh Tuesday, October 4, 2011 11:31 AM
    • Edited by Amreek Singh Wednesday, October 5, 2011 11:17 AM
    Tuesday, October 4, 2011 11:26 AM
  • Hi,

    I want to use two custom rules but two rules are not working at a time

    <RibbonDiffXml>
            <CustomActions>
       <CustomAction Id="CI_formlevelSubmit" Location="Mscrm.Form.new_copf.MainTab.ExportData.Controls._children" Sequence="75">
        <CommandUIDefinition>
        <Button Id="B_formbuttonSubmit" Command="Cmd_JavaScript3" LabelText="Submit" ToolTipTitle="Submit COPF" ToolTipDescription="Form Level Button"
        TemplateAlias="o1"/>
        </CommandUIDefinition>
                </CustomAction>
       <CustomAction Id="CI_formlevelAccept" Location="Mscrm.Form.new_copf.MainTab.ExportData.Controls._children" Sequence="76">
        <CommandUIDefinition>
        <Button Id="B_formbuttonAccept" Command="Cmd_JavaScript1" LabelText="Accept" ToolTipTitle="Accept COPF" ToolTipDescription="Form Level Button"
        TemplateAlias="o1"/>
        </CommandUIDefinition>
                </CustomAction>
       <CustomAction Id="CI_formlevelReject" Location="Mscrm.Form.new_copf.MainTab.ExportData.Controls._children" Sequence="77">
        <CommandUIDefinition>
        <Button Id="B_formbuttonReject" Command="Cmd_JavaScript2" LabelText="Reject" ToolTipTitle="Reject COPF" ToolTipDescription="Form Level Button"
        TemplateAlias="o1" />
        </CommandUIDefinition>
                </CustomAction>

            </CustomActions>
            <Templates>
              <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
            </Templates>
            <CommandDefinitions>
       <CommandDefinition Id="Cmd_JavaScript1">
        <EnableRules>
         <EnableRule Id="Mscrm.CheckAcceptRejectRule"></EnableRule>
        </EnableRules>
        <DisplayRules/>
        <Actions>
        <JavaScriptFunction Library="$webresource:new_copf_acceptreject_buttonclick" FunctionName="Accept"></JavaScriptFunction>
        </Actions>
       </CommandDefinition>
       <CommandDefinition Id="Cmd_JavaScript2">
        <EnableRules>
         <EnableRule Id="Mscrm.CheckAcceptRejectRule"></EnableRule>
        </EnableRules>
        <DisplayRules/>
        <Actions>
        <JavaScriptFunction Library="$webresource:new_copf_acceptreject_buttonclick" FunctionName="Reject"></JavaScriptFunction>
        </Actions>
       </CommandDefinition>
       <CommandDefinition Id="Cmd_JavaScript3">
        <EnableRules>
         <EnableRule Id="Mscrm.CheckSubmitRule"></EnableRule>
        </EnableRules>
        <DisplayRules/>
        <Actions>
        <JavaScriptFunction Library="$webresource:new_copf_acceptreject_buttonclick" FunctionName="Submit"></JavaScriptFunction>
        </Actions>
       </CommandDefinition>
            </CommandDefinitions>
            <RuleDefinitions>
      <TabDisplayRules />
      <DisplayRules/>
      <EnableRules>
       <EnableRule Id="Mscrm.CheckSubmitRule">
        <CustomRule FunctionName="callMain" Library="$webresource:new_copf_submitbutton_disable"></CustomRule>
       </EnableRule>
       <EnableRule Id="Mscrm.CheckAcceptRejectRule">
        <CustomRule FunctionName="callMain" Library="$webresource:new_copf_acceptrejectbutton_disable"></CustomRule>
       </EnableRule>
      </EnableRules>
      </RuleDefinitions>
            <LocLabels />
          </RibbonDiffXml>

    Wednesday, October 5, 2011 5:45 AM
  • Hi Buddy,

    Both functions need to return true to enable the button

    there is no "OR" kind a consdition in here.

    use debugger; in your function to check what theya re returning

    I hope this helps.

    Anyway the question starts with your code and run in it on my machine and posted the result. Can you mark that post as helpful?

    The other question was answered too and You did not mark as answered or helpful. I bit of effort goes in running the code and stuff.

    Could you please do that http://social.microsoft.com/Forums/en-US/crm/thread/24c1d256-f7f0-46dc-9d6b-17905899efc8/

    Thanks buddy.

     

     


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/
    • Edited by Amreek Singh Wednesday, October 5, 2011 11:18 AM
    • Proposed as answer by Amreek Singh Wednesday, October 5, 2011 11:18 AM
    Wednesday, October 5, 2011 11:16 AM
  • Friday, November 18, 2011 12:41 PM