1

I'm planning to get zone information of a web part using JSOM and I couldn't find any chance of getting it.

My Code:

var ctxOBJ = SP.ClientContext.get_current();
var webOBJ = ctxOBJ.get_web();
var siteOBJ = ctxOBJ.get_site();
var pageURL = window.location.pathname;
var webPartManagerOBJ = webOBJ.getFileByServerRelativeUrl(pageURL).getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
  //get the web parts on the current page
  var collWebPart = webPartManagerOBJ.get_webParts();
  //request the web part collection and load it from the server
  ctxOBJ.load(collWebPart);
  ctxOBJ.executeQueryAsync(Function.createDelegate(this, function() {
    // Go through all webparts
    for (var x = 0; x < collWebPart.get_count(); x++) {
      debugger;
      var webPartDef = collWebPart.get_item(x);
      console.log(webPartDef.get_id().toString());
    }
  }), Function.createDelegate(this, function() {
    alert("failed to fetch shared webparts from page")
  }));

I need the zone id of each web part in that page.
Can anyone help me on this?

2

For ZoneId you'll have to use get_zoneId().

Change the load:

ctxOBJ.load(collWebPart,'Include(ZoneId)');

And the use get_zoneId():

console.log(webPartDef.get_zoneId());

If you plan to use props on the webpart itself, you'll have to load that too:

ctxOBJ.load(collWebPart,  'Include(WebPart.Title,WebPart.ZoneIndex, ZoneId)');

And then:

    console.log(webPartDef.get_zoneId());
    console.log(webPartDef.get_webPart().get_title());
    console.log(webPartDef.get_webPart().get_zoneIndex())
  • Can I get the property of webpart whether hidden or not? – RealSteel Mar 23 '18 at 7:42
  • 1
    @RealSteel you are probably thinking about if its closed or not? Then you have webPartDef.get_webPart().get_isClosed(), remember to add the prop to the load: 'Include(WebPart.IsClosed,WebPart.Title,WebPart.ZoneIndex, ZoneId)' – Anders Aune Mar 23 '18 at 7:57

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.