none
document.getElementsByTagName('ie:menuitem'); length is 'zero'?

    Question

  • I am trying to Hide ListView Toolbar Items. I am using below javascript

    hideListViewToolbarItems("Edit in Datasheet", "export to Spreadsheet",
        "view rss feed","settings:create view");
    
    function hideListViewToolbarItems()
    {
    	/// <summary>
    	/// By : Ayman M. El-Hattab ( ayman.elhattab@gmail.com )
    	/// http://ayman-elhattab.blogspot.com
    	/// </summary>
    	
    	var menuItem;	
    	var menuItemName;
    	var menuItemIndex=-1;
    	var menuItemNames=new Array("edit in datasheet", 
    	  "open with windows explorer",
    	  "connect to outlook",'export to spreadsheet','view rss feed','alert me'
    	  ,"create column","settings:create view","list settings",
    	  "document library settings","explorer view","all documents",
    	  "all items","modify this view", 
    	  "view:create view","new document",
    	  "new item","new folder","upload document", 
    	  "upload multiple documents");
    	var menuItems = new Array("EditInGridButton", 
    	  "OpenInExplorer","OfflineButton",
    	  "ExportToSpreadsheet","ViewRSS",
    	  "SubscribeButton","AddColumn",
    	  "AddView","ListSettings","ListSettings", 
    	  "View1","DefaultView",
    	  "DefaultView","ModifyView","CreateView", 
    	  "New0","New0",
    	  "NewFolder","Upload","MultipleUpload");		
    
    	var allMenuItems = document.getElementsByTagName('ie:menuitem');
    	for(var i = 0; i < hideListViewToolbarItems.arguments.length; i++ ) 
    	{								
    		menuItemName= hideListViewToolbarItems.arguments[i].toLowerCase();
    		for (j=0; j < menuItemNames.length; j++) 
    		{
    			if(menuItemNames[j]==menuItemName)
    			{				
    				menuItemIndex = j;
    				break;
    			}
    		}
    
    		menuItem=menuItems[menuItemIndex];
    				
    		for (var l = 0; l < allMenuItems.length; l++)
    		{		
    			if(menuItemName.indexOf(":")!=-1)
    			{
    				menuItemName = menuItemName.split(":")[1];
    			}
    			if (allMenuItems[l].id.indexOf(menuItem)!=-1 
    		        && allMenuItems[l].text.toLowerCase() == menuItemName)
    			{		
    				// For FireFox Compatibility
    				var parentNodeOfMenuItem = allMenuItems[l].parentNode;
    				parentNodeOfMenuItem.removeChild(allMenuItems[l]);
    				break;
    			}
    		}
    	}
    }

    I am getting document.getElementsByTagName('ie:menuitem') length is '0' . So It is not going to3 rd loop.

    Any help???


    Jamsbond
    • Edited by ItsMeSri Monday, August 10, 2009 9:58 PM
    Monday, August 10, 2009 9:44 PM

Answers

  • I use the same technique here and it works fine:
      http://techtrainingnotes.blogspot.com/2008/02/sharepoint-hiding-menus-not-using.html

    Make sure your JavaScript is being loaded below the menus, or use _spBodyOnLoadFunctionNames.push(yourfunctionname) to insure your JavaScript is run after the page load is complete:


    _spBodyOnLoadFunctionNames.push("HideStuff")

    function HideStuff ()
    {
    hideListViewToolbarItems("Edit in Datasheet", "export to Spreadsheet",
        "view rss feed","settings:create view");
    }

    ... your hide function here ...


    Mike Smith TechTrainingNotes.blogspot.com
    Tuesday, August 11, 2009 1:16 AM

All replies

  • I use the same technique here and it works fine:
      http://techtrainingnotes.blogspot.com/2008/02/sharepoint-hiding-menus-not-using.html

    Make sure your JavaScript is being loaded below the menus, or use _spBodyOnLoadFunctionNames.push(yourfunctionname) to insure your JavaScript is run after the page load is complete:


    _spBodyOnLoadFunctionNames.push("HideStuff")

    function HideStuff ()
    {
    hideListViewToolbarItems("Edit in Datasheet", "export to Spreadsheet",
        "view rss feed","settings:create view");
    }

    ... your hide function here ...


    Mike Smith TechTrainingNotes.blogspot.com
    Tuesday, August 11, 2009 1:16 AM
  • Your code works.

    function hideListViewToolbarItems()
    {
    var doc = document.getElementsByTagName('ie:menuitem');
    for (var i = 0; i < doc.length; i++) {
    var itm = doc[i];

    if (itm.id.match('OpenInExplorer') != null || itm.id.match('EditInGridButton') != null|| itm.id.match('OfflineButton') != null|| itm.id.match('ExportToSpreadsheet') != null|| itm.id.match('OpenInAccess') != null) {

    var p = itm.parentNode;
    p.removeChild(itm);
    }
    }
    }
    I can hide 'EditInGridbutton' and 'OfflineButton'. But I am can not hide
    'OpenInExplorer' and 'ExportToSpreadsheet'
    In the page. If I see ViewSource I can see these values.



    Jamsbond
    Tuesday, August 11, 2009 4:59 PM

  • > Your code works.

    well... use it then...   :-)


    > But I can not hide...

    Maybe when you remove the child node you break the process for any nodes that follow. You could either loop through the nodes in reverse order, or do what I did and just hide the node:

        itm.hidden=true;


    Mike Smith TechTrainingNotes.blogspot.com
    Tuesday, August 11, 2009 5:55 PM
  • Small problem here in Firefox and Chrome.

    'Multiupload' is not displaying.


    and If use

    if (itm.id.match('ExportToSpreadsheet') != null || itm.id.match('OfflineButton') != null) {
                                 
                        var p = itm.parentNode;
                        p.removeChild(itm);
                    }

    'ExportToSpreadsheet' is showing (firefox). If I dont use 'OfflineButton' then It is removing 'ExportToSpreadsheet'.

    Any idea?
    Jamsbond
    Tuesday, August 11, 2009 7:30 PM
  • Hi the script is not working:

    i have this values in allMenuItems :

    0   ie:menuitem#zz4_ID_PersonalInformation 
    1   ie:menuitem#zz5_ID_LoginAsDifferentUser
    2   ie:menuitem#zz6_ID_RequestAccess 
    3   ie:menuitem#zz7_ID_Logout 
    4   ie:menuitem#zz8_ID_PersonalizePage 
    5   ie:menuitem#zz11_MenuItem_Create
    6   ie:menuitem#ctl00_PlaceHolderTopNavBar_SiteActionsMenuMain_ctl00_MenuItem_EditPag 
    7   ie:menuitem#zz12_MenuItem_Settings
    8   ie:menuitem#zz14_New0 
    9   ie:menuitem#zz17_OfflineButton   
    10   ie:menuitem#zz18_ExportToSpreadsheet   
    11   ie:menuitem#zz19_ExportToDatabase
    12   ie:menuitem#zz20_ViewRSS
    13   ie:menuitem#zz21_SubscribeButton
    14   ie:menuitem#zz24_AddColumn   
    15   ie:menuitem#zz26_ListSettings  
    16   ie:menuitem#zz29_DefaultView   
    17   ie:menuitem#zz30_View1 
    18   ie:menuitem#zz31_View2  
    19   ie:menuitem#zz32_ModifyView 
    20   ie:menuitem#zz33_CreateView

     

    I want to hide all elements in Settings and Actions.

    I try to use if loop of l the:

    if (allMenuItems[l].id.match('zz33_CreateView')!=null)
                                {
                                var parentNodeOfMenuItem = allMenuItems[l].parentNode;
                                parentNodeOfMenuItem.removeChild(allMenuItems[l]);

                                }   

     

    or

    if (allMenuItems[l].id.match('CreateView')!=null)
                                {
                                var parentNodeOfMenuItem = allMenuItems[l].parentNode;
                                parentNodeOfMenuItem.removeChild(allMenuItems[l]);

                                }   


    why this not working ?                           

    Tuesday, July 27, 2010 1:20 PM
  • A few thoughts as to why it's not working...

    1) Match for part of the text and leave off the "zz33_" as that part can be different for every page.

    2) Don't delete the nodes, hide them

    3) Make sure your code is loaded after the web part with the View dropdown (i.e. if using a Content Editor Web Part, move it under the list web part)

     

    Here's my code from my blog article listed earlier. I just tested it and it works fine, at least in SharePoint 2007:

     

    <script language="JavaScript">
    
     var doc = document.getElementsByTagName('ie:menuitem'); 
    
     for (var i = 0; i < doc.length; i++)
    
     {
    
      itm = doc[i];
    
      if (itm.id.match('CreateView')!=null)
    
       { itm.hidden=true; }
    
     } 
    
    </script> 

     


    Mike Smith TechTrainingNotes.blogspot.com
    Wednesday, July 28, 2010 4:01 AM