I get all webparts that I have in default.aspx using this code
var pageName = "/sites/default.aspx"; var ctx = new SP.ClientContext(); var pageFile = ctx.get_web().getFileByServerRelativeUrl(pageName); var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared); var webPartDefs = webPartManager.get_webParts(); ctx.load(webPartDefs,'Include(WebPart)'); ctx.executeQueryAsync( function () { for(var i = 0;i < webPartDefs.get_count();i++) { var webPartDef = webPartDefs.getItemAtIndex(i); var webPart = webPartDef.get_webPart(); var title = webPart.get_title(); }); I get here 4 webparts, Webpart1,Webpart2,Webpart3,Webpart4
Of this 4 webparts I want to set display:none on Webpart1,Webpart2,Webpart3, So I only want to display Webpart4.
How could I create an array of the webparts that I want to display, and then check if my title of webparts contains that and then if it contains display:none on them and do nothing with
var webPartsArray = ["Webpart1", "Webpart2", "Webpart3"]; UPDATE
==================================== My webpart wont hide?
function init() { var pageName = "/sites/default.aspx"; var ctx = new SP.ClientContext(); var pageFile = ctx.get_web().getFileByServerRelativeUrl(pageName); var webPartManager = pageFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared); var webPartDefs = webPartManager.get_webParts(); ctx.load(webPartDefs, 'Include(WebPart)'); ctx.executeQueryAsync( function () { var pageWPArray = []; for (var i = 0; i < webPartDefs.get_count() ; i++) { var webPartDef = webPartDefs.getItemAtIndex(i); var webPart = webPartDef.get_webPart(); pageWPArray.push(webPart.get_title()); } var webPartsArray = ["Webpart1", "Webpart2", "Webpart3"]; for (var j = 0; j < pageWPArray.length; j++) { for (var i = 0; i < webPartsArray.length; i++) { if (pageWPArray[j].indexOf(webPartsArray[i]) > -1) alert("pageWPArray:" + "" + pageWPArray[j] +" "+ "=:" + " " + (webPartsArray[i])); { jQuery("#"+webPartsArray[i]).hide(); } } } }, function (sender, args) { console.log(args.get_message()); } ); }
ExecuteOrDelayUntilScriptLoaded(init, 'SP.js');