12

I have a page with some webparts on it, I want to get specific one (by name or ID) using client javascript. I try this code

var ctx=SP.ClientContext.get_current();
var web = ctx.get_web();
var site = ctx.get_site();
var theURL = window.location.pathname;
var oFile = web.getFileByServerRelativeUrl(theURL);
var limitedWebPartManager = oFile.getLimitedWebPartManager(SP.WebParts.PersonalizationScope.shared);
var collWebPart = limitedWebPartManager.get_webParts();
ctx.load(collWebPart);
ctx.executeQueryAsync();

How to get webpart by name or id after i get a collection? Maybe there is a easier way to do it?

3

Once you execute the example you have above, you can (for example) get the title of all web parts on the page by executing this:

var i = collWebPart.get_count()
for (var j=0;j<i;j++)
{ 
    var webPart = collWebPart.get_item(j).get_webPart()
    ctx.load(webPart);
    ctx.executeQueryAsync();

    webPart.get_title();
}

Source: http://msdn.microsoft.com/en-us/library/hh185010.aspx

However, based on your comment above, it sounds like you might be looking for a value from a field from a listitem displayed in the list displayed by this webpart. If that is the case, you might want to try a more direct approach by getting the list, and then the list item, rather than getting the webpart.

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.