//array to store webpart titles in the page.
var WebpartTitles = [];
// to get webpart count and index count.
var WebpartsCount=0,wpindex = 0;
function GetWebPartTitlesFromPage() {
this.clientContext = new SP.ClientContext.get_current();
var oFile = clientContext.get_web().getFileByServerRelativeUrl(serverRelativeUrl);
var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
this.collWebPart = limitedWebPartManager.get_webParts();
clientContext.load(collWebPart);
clientContext.executeQueryAsync(Function.createDelegate(this, this.getTitle), Function.createDelegate(this, this.onQueryFailed));
}
function getTitle() {
if(WebpartsCount == 0)
{
WebpartsCount = collWebPart.get_count();
}
if(!WebpartsCount )
{
alert("No webparts in the current Page");
return;
}
var oWebPartDefinition = collWebPart.get_item(wpindex);
this.oWebPart = oWebPartDefinition.get_webPart();
clientContext.load(oWebPart, 'Title');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededGetTitle), Function.createDelegate(this, this.onQueryFailed));
} function onQuerySucceededGetTitle()
{
wpindex++;
var title = this.oWebPart.get_title(); WebpartTitles.push(title); if(wpindex < WebpartsCount)
{
getTitle(); }
else{ //bingo u got all webparts titles here.
alert(WebpartTitles); }
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n'); }
Great post.
ReplyDeleteI'm using your script within a script editor web part to get all the titles of the web parts on the page. However, when I'm running the script I'm getting this error:
ReplyDelete"SCRIPT5009: oWebPart is undefined"
Any ideas on what I'm doing wrong?