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?

6
  • 3
    What is the intent of getting the webpart id? What do you want the code to do? This may help define he path needed to accomplish the goal. Commented Apr 16, 2013 at 12:26
  • We have a webpart that displays a list. I want to get information from this web part, e.g. e-mails.
    – Dmitry T
    Commented Apr 17, 2013 at 14:39
  • If you are using client object model you should be able to use jQuery just as well. It may be easier! Commented Apr 18, 2013 at 8:49
  • It has the answer i tested by id msdn.microsoft.com/en-us/library/jj164022.aspx Commented Jun 14, 2013 at 5:46
  • This is a duplicate of: sharepoint.stackexchange.com/questions/46474/… Marking as duplicate.
    – Hugh Wood
    Commented Oct 30, 2013 at 9:47

1 Answer 1

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 and acknowledge you have read our privacy policy.

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