1

I am trying to get my webparts on my page by title, on get_title().

I get the error:

object does not support get_Title()

ExecuteOrDelayUntilScriptLoaded(init,'SP.js');
    function init(){
       var clientContext = new SP.ClientContext.get_current();
       var web = clientContext.get_web();
       var theURL = window.location.pathname;
       var oFile = web.getFileByServerRelativeUrl(theURL);
       var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
       var collWebPart = limitedWebPartManager.get_webParts();

      clientContext.load(collWebPart);

      clientContext.executeQueryAsync(function () {
          var Enumerator = collWebPart.getEnumerator();
          while (Enumerator.moveNext()) {        
              var enumTitle = Enumerator.get_current();
              alert(enumTitle.get_title());
          }
      },
      function () {
      });
}
6
  • Your code does have syntax error. Did you check it in console?
    – Pradip R.
    Commented May 25, 2015 at 10:39
  • @PradipR. yes this, object doesn't support property or method 'get_title
    – Batistuta
    Commented May 25, 2015 at 10:43
  • I think this syntax error, makes so it does not work, 'collWebPart' is undefined
    – Batistuta
    Commented May 25, 2015 at 10:48
  • Did you check weather you are getting the collWebPart object in executeQueryAsync method in any developer tools?
    – Pradip R.
    Commented May 25, 2015 at 10:48
  • No i just found out that i am not getting it?
    – Batistuta
    Commented May 25, 2015 at 10:49

1 Answer 1

0

I would recommend try following snippet:

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();
       console.log(webPart.get_title());
    }
  },
  function(sender,args){
     console.log(args.get_message());
  }
);

Where pageName should be the relative url of page, i.e. /pages/default.aspx.

5
  • I worked know, thanks, what do u think the problem was with my code?
    – Batistuta
    Commented May 25, 2015 at 11:22
  • I guess the problem might be in iteration of webparts (var Enumerator = collWebPart.getEnumerator();).
    – Pradip R.
    Commented May 25, 2015 at 11:24
  • Alright thanks, my next step is to check if for example webpart title is mywebpart1 then hide that webpart? Any suggestions?
    – Batistuta
    Commented May 25, 2015 at 11:26
  • I am not sure but you can get the id of the webpart by comparing it to your desired title and then make that webpart hidden with CSS display: none.
    – Pradip R.
    Commented May 25, 2015 at 11:36
  • Alright, which object should i set to display:none,
    – Batistuta
    Commented May 25, 2015 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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