locked
Sample code for using security role or title to display a toolbar button like "Resolve" on case form RRS feed

  • Question

  • I would like to create another button on the toolbar that mimics the resolve button, but "approves" a case. Is there any sample code to only make a toolbar visible to a select security group?
    Thursday, May 14, 2009 7:09 PM

Answers

  • To make a button disappear based on security role, you would have to do several different things.

    First, you have to get the id of your button, which can be found by viewing the source of the case form and looking for your button.  The tag your are looking for should be an li tag and the id would look like this: ISV_New_234_MyNewButton. 

    Second, review the code Jim Wang has provided here: http://jianwang.blogspot.com/2008/01/crm-40-check-current-users-security.html.  You can use that code to determine if the user has the particular role.

    Third, based on the result of the javascript code, you would set the display property of the button to none, using the sample code that Adi Katz provided here:  http://mscrm4ever.blogspot.com/2008/08/show-hide-crm-form-toolbar-buttons.html.

    Post back if you need more assistance.
    Thursday, May 14, 2009 7:39 PM
  • Got it figured out.....  For some reason I had to use the Tooltip text in the onLoad code.  No idea why but that worked.  

    The ISV has it correct, but when I did a View Source on the Case form, I saw it backwards

    here is the ISV.... I've tested it with other toolbar items as wll and if I use the tooltip, the item is hidden.  Now I can go back and try to make with work with the security role code. :)

     <Button Icon="/_imgs/ico_16_4206.gif" AvailableOffline="true" JavaScript="&#xD;&#xA;if (crmForm.all.kbarticleid.DataValue == null) { alert('A KB Article is required to resolve the Case.') } else if ( crmForm.all.hi_customerpo.DataValue == null ) { alert('A Customer PO is required to resolve the Case.'); } else {resolve(); };&#xD;&#xA;">
                  <Titles>
                    <Title LCID="1033" Text="Resolve Case" />
                  </Titles>
                  <ToolTips>
                    <ToolTip LCID="1033" Text="This will complete the case." />
                  </ToolTips>
                </Button>
                <Button Icon="/_imgs/ico_16_4206.gif" AvailableOffline="true" JavaScript="&#xD;&#xA;if (crmForm.all.kbarticleid.DataValue == null) { alert('A KB Article is required to approve the Case.') } else if ( crmForm.all.hi_customerpo.DataValue == null ) { alert('A Customer PO is required to approve the Case.'); } else {resolve(); };&#xD;&#xA;">
                  <Titles>
                    <Title LCID="1033" Text="Approve Case" />
                  </Titles>
                  <ToolTips>
                    <ToolTip LCID="1033" Text="This will approve the case." />
                  </ToolTips>
                </Button>



    Monday, May 18, 2009 9:46 PM

All replies

  • To make a button disappear based on security role, you would have to do several different things.

    First, you have to get the id of your button, which can be found by viewing the source of the case form and looking for your button.  The tag your are looking for should be an li tag and the id would look like this: ISV_New_234_MyNewButton. 

    Second, review the code Jim Wang has provided here: http://jianwang.blogspot.com/2008/01/crm-40-check-current-users-security.html.  You can use that code to determine if the user has the particular role.

    Third, based on the result of the javascript code, you would set the display property of the button to none, using the sample code that Adi Katz provided here:  http://mscrm4ever.blogspot.com/2008/08/show-hide-crm-form-toolbar-buttons.html.

    Post back if you need more assistance.
    Thursday, May 14, 2009 7:39 PM
  • Thanks for the great information.. This will surely keep me busy for a while. I am really excited about learning how to do this.
    Friday, May 15, 2009 3:07 AM
  • The id of the button changes everytime right? Or am I looking at the wrong thing.  It is currently ISV_New_81_ApproveCase, but was different earlier, ISV_New_26_ApproveCase
    Friday, May 15, 2009 6:21 PM
  • Yes, it does appear the id of the button changes every time.  I did not expect that to occur.

    You should still be able to use Adi Katz's code with the title of the button "Approve Case".

    Friday, May 15, 2009 6:29 PM
  • I made Adi's code work with other buttons on the form but I can't get it to work with my buttons.  The label is probably not the same as the title of the button.  I must not have a title for my buttons.  Is that possible?

    if (crmForm.all.kbarticleid.DataValue == null) { alert('A KB Article is required to approve the Case.') } else if ( crmForm.all.hi_customerpo.DataValue == null ) { alert('A Customer PO is required to approve the Case.'); } else {resolve(); };
    " onclick="window.execScript(action)" id="ISV_New_118_ApproveCase"><SPAN class="ms-crm-Menu-Label"><A onclick="return false;" href="javascript:onclick();" target="_self" tabIndex="-1" class="ms-crm-Menu-Label"><IMG  src="&#47;_imgs&#47;ico_16_4206.gif" alt="This&#32;will&#32;approve&#32;the&#32;case." tabIndex="-1" class="ms-crm-Menu-ButtonFirst"><SPAN tabIndex="0" class="ms-crm-MenuItem-TextRTL">Approve Case</SPAN></A></SPAN></LI>
    Friday, May 15, 2009 7:05 PM
  • If my code works with other buttons but not my button does that mean I am using the wrong title? Is it possible that a button does not have a title?
    Monday, May 18, 2009 6:51 PM
  • I don't think it's possible that your button does not have a title.  Can you post the entity tag from your isv.config file where you are setting the button and its properties?

    Monday, May 18, 2009 6:53 PM
  • I tried using Approve Case in the hide button code.          

                <Button Icon="/_imgs/ico_16_4206.gif" AvailableOffline="true" JavaScript="&#xD;&#xA;if (crmForm.all.kbarticleid.DataValue == null) { alert('A KB Article is required to approve the Case.') } else if ( crmForm.all.hi_customerpo.DataValue == null ) { alert('A Customer PO is required to approve the Case.'); } else {resolve(); };&#xD;&#xA;">
                  <Titles>
                    <Title LCID="1033" Text="Approve Case" />
                  </Titles>
                  <ToolTips>
                    <ToolTip LCID="1033" Text="This will approve the case." />
                  </ToolTips>
                </Button>
    Monday, May 18, 2009 6:56 PM
  • This is the second time in this thread I have had to back track.  I have tested the following code on my dev system and it successfully hides the button.

    Place this code in the onload event of your case form:
    var ULListItems = document.getElementById('mnuBar1').rows[0].cells[0].getElementsByTagName('UL')[0].getElementsByTagName('LI');
    
    for(var i=0; ULListItems.length - 1; i++)
    
    {
    
    if(ULListItems[i].id.indexOf('ApproveCase') &gt; 0)
    
    {
    
    ULListItems[i].style.display = 'none';
    
    break;
    
    }
    
    }
    I can't take credit for the loop in this code; I found this article: http://kennyvaes.wordpress.com/2009/02/03/hidden-buttons-reappear-when-resizing-a-crmform/ by Kenny Vaes.
    Monday, May 18, 2009 7:49 PM
  • Still no go....  Like I said earlier the code works with other buttons, but not mine Resolve Case or Approve Case buttons. This is driving me crazy....

    I have stripped out all of my other OnLoad code too, so none of that is interfering.



    Monday, May 18, 2009 8:46 PM
  • Did you get an error when clicking the button?
    Monday, May 18, 2009 8:48 PM
  • Only the one from the code telling me I need to enter a PO, so that is working correctly.  Using the code below, all of the buttons listed are hidden except Resolve and Approve....

    //Hide Toolbar Buttons

    var Spacer = {    
        Right : 1, // hides a right spacer if it exists
        Left : 2, // hides a left spacer if it exists
        Both : 3, // and so on...
        None : 4
    }

    var Display = {
        Show : "inline",
        Hide : "none"
    }

    function OnCrmPageLoad()
    {
      //Configure Display when the form loads.
      ConfigreToolbarDisplay();
      //Configure the display each time a user manually changes the window width size.
      attachEvent("onresize",ConfigreToolbarDisplay);
    }

    function ConfigreToolbarDisplay()
    {
        // Opportunity toolbar buttons
       
        ShowHideToolbarButton( "Save" , Spacer.None , Display.Hide );
      ShowHideToolbarButton( "Approve Case" , Spacer.None , Display.Hide );
      ShowHideToolbarButton( "Resolve Case" , Spacer.None , Display.Hide );
        ShowHideToolbarButton( "Follow Up" , Spacer.Right , Display.Hide );
        ShowHideToolbarButton( "Recalculate" , Spacer.Left , Display.Hide );
        ShowHideToolbarButton( "Attach a File" , Spacer.Both , Display.Hide );
        ShowHideToolbarButton( "Follow Up" , Spacer.Right , Display.Show );
    }

    function ShowHideToolbarButton( btnTitle , spacer , state )
    {   
        if( isNullOrEmpty( document.all.mnuBar1 ) )
            return;
        if( isNullOrEmpty( btnTitle ) )
            return;
        if( isNullOrEmpty( spacer ) )
            spacer = ToolbarSpacer.None;
        if( isNullOrEmpty( state ) )
            state = ButtonDisplay.Hide;
       
        //Get all toolbar buttons
        var toolBarButtons = document.all.mnuBar1.rows[0].cells[0].childNodes[0].childNodes;
       
        //Loop through each button
        for (var i = 0 ; i < toolBarButtons.length ; i++)
        {
            var button = toolBarButtons[i];
            if( button.title.match(btnTitle) != null )
            {            
                button.style.display = state;
                switch(spacer)
                {
                    case Spacer.Right:
                        ShowHideSpacer( button.nextSibling );
                        break;
                    case Spacer.Left:
                        ShowHideSpacer( button.previousSibling );
                        break;
                    case Spacer.Both:
                        ShowHideSpacer( button.nextSibling );
                        ShowHideSpacer( button.previousSibling );
                        break;
                }

                return;
            }
        }
        function ShowHideSpacer( btnSpacer ){
            if( !isNullOrEmpty( btnSpacer ) )
                btnSpacer.style.display = state;
        }
        function isNullOrEmpty( obj ){
            return obj == null || typeof(obj) == "undefined" || obj == "";
        }
    }

    OnCrmPageLoad();

    Monday, May 18, 2009 8:52 PM
  • Replace
            if( button.title.match(btnTitle) != null)
    

    with

            if( button.title.match(btnTitle) != null || button.id.indexOf(btnTitle) > 0)
    

    and replace

      ShowHideToolbarButton( "Approve Case" , Spacer.None , Display.Hide );
      ShowHideToolbarButton( "Resolve Case" , Spacer.None , Display.Hide );
    with
      ShowHideToolbarButton( "ApproveCase" , Spacer.None , Display.Hide );
      ShowHideToolbarButton( "ResolveCase" , Spacer.None , Display.Hide );
    Monday, May 18, 2009 9:13 PM
  • Got it figured out.....  For some reason I had to use the Tooltip text in the onLoad code.  No idea why but that worked.  

    The ISV has it correct, but when I did a View Source on the Case form, I saw it backwards

    here is the ISV.... I've tested it with other toolbar items as wll and if I use the tooltip, the item is hidden.  Now I can go back and try to make with work with the security role code. :)

     <Button Icon="/_imgs/ico_16_4206.gif" AvailableOffline="true" JavaScript="&#xD;&#xA;if (crmForm.all.kbarticleid.DataValue == null) { alert('A KB Article is required to resolve the Case.') } else if ( crmForm.all.hi_customerpo.DataValue == null ) { alert('A Customer PO is required to resolve the Case.'); } else {resolve(); };&#xD;&#xA;">
                  <Titles>
                    <Title LCID="1033" Text="Resolve Case" />
                  </Titles>
                  <ToolTips>
                    <ToolTip LCID="1033" Text="This will complete the case." />
                  </ToolTips>
                </Button>
                <Button Icon="/_imgs/ico_16_4206.gif" AvailableOffline="true" JavaScript="&#xD;&#xA;if (crmForm.all.kbarticleid.DataValue == null) { alert('A KB Article is required to approve the Case.') } else if ( crmForm.all.hi_customerpo.DataValue == null ) { alert('A Customer PO is required to approve the Case.'); } else {resolve(); };&#xD;&#xA;">
                  <Titles>
                    <Title LCID="1033" Text="Approve Case" />
                  </Titles>
                  <ToolTips>
                    <ToolTip LCID="1033" Text="This will approve the case." />
                  </ToolTips>
                </Button>



    Monday, May 18, 2009 9:46 PM