Wednesday, August 3, 2011

Hide content types from new menu using Javascript in SharePoint 2010

below code uses for hiding content types from the new menu button in the document library. it hides all content types except matching  one.

 _spBodyOnLoadFunctionNames.push("hideMenuItems");

  function hideMenuItems() {
            //alert("hideMenuItems");
            var contenttype = document.getElementById('<%=hdnCType.ClientID %>').value;

            //gets content type menu items
            var menuid = getTagFromIdentifierAndTitle("menu", "ServerMenu", "toolBarTbl_RptControls");

            if (menuid != null) {

                var menu = document.getElementById(menuid);

                for (var i = 0; i < menu.childNodes.length; i++) {

                    var child = menu.childNodes[i];

                    if (child.text == contenttype) {
                       //it wont hide matching content type                        
                    }
                    else {
                        //menu.removeChild(child);
                      //  if (child.text != 'Folder') {
                            child.hidden = true;  // hides all content types
                    //    }
                    }

                }
              }
        }
       


        function getTagFromIdentifierAndTitle(tagName, identifier, title) {

            var len = identifier.length;

            var tags = document.getElementsByTagName(tagName);

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

                var tempString = tags[i].id;

                if (tempString.match(title)) {

                    return tags[i].id;
                }

            }
            return null;
        }



No comments:

Post a Comment

Followers

Blog Archive