0

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');

====================================

1

Try this code. It assumes the WebPart title is the ID in the DOM

var pageWPArray;

function () {
for(var i = 0;i < webPartDefs.get_count();i++) {
   var webPartDef = webPartDefs.getItemAtIndex(i);
   var webPart = webPartDef.get_webPart();

   pageWPArray.push(webPart.get_title());
});

//Loop through your array of Web Parts you'd like hidden
for (var i = 0; i < webPartsArray.length; i++) {
    //Does your array contain this value?
    if(pageWPArray.indexOf(webPartsArray[i]) > -1)
        $("#" + webPartsArray[i]).hide());
}
  • It does not hide my webparts? – Batistuta May 26 '15 at 7:20
  • I am not getting the id, i am geting the webpart title and also have a an array of the webparts titles that i want to hide? @Reek – Batistuta May 26 '15 at 7:28
  • I will update the code that i have now. – Batistuta May 26 '15 at 7:36
1

As per my understanding you are trying to run the above code from the same page (default.aspx)

If so, you can actually run the normal jQuery code to get the webpart divs and hide them according to your requirement.

The following code will give you all the webpart divs with "webpartid" custom attribute

$('div[webpartid]')

just loop through the list above and hide the webpart divs accordingly

$('div[webpartid]').each(function(e){
 if($(this).attr('id') != 'WebPartWPQ4')
 {
   $(this).hide();
 }
});

Let me know if you have any questions

  • Hi, but the id of the webpart sare not the same every time, i never now if it would be WebPartWPQ4 or WebPartWPQ5 or non of them? Thats why i want to get them by title and compare to my array and then hide. if i was so simple if the webparts always hade the same id, could set on them display:none; – Batistuta May 26 '15 at 8:11
  • Hello TheRock, If you can give me the full requirement, I can help you better. Even in your question I see some IDs or Titles hard coded. I reckon you can still read the values from some SharePoint list if you are storing the values for which web part you wanna show/hide on some page. If you are going by the Titles of the web part also you must have been storing those values somewhere and read them from there using CSOM. – Naveen May 26 '15 at 9:24
  • I have posted my full code,? The titles of the webparts are hard coded yes, they should be that. – Batistuta May 26 '15 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.