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? Commented May 25, 2015 at 10:39
  • @PradipR. yes this, object doesn't support property or method 'get_title Commented May 25, 2015 at 10:43
  • I think this syntax error, makes so it does not work, 'collWebPart' is undefined Commented May 25, 2015 at 10:48
  • Did you check weather you are getting the collWebPart object in executeQueryAsync method in any developer tools? Commented May 25, 2015 at 10:48
  • No i just found out that i am not getting it? 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? Commented May 25, 2015 at 11:22
  • I guess the problem might be in iteration of webparts (var Enumerator = collWebPart.getEnumerator();). 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? 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. Commented May 25, 2015 at 11:36
  • Alright, which object should i set to display:none, Commented May 25, 2015 at 12:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.