Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
2  
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. –  spevilgenius Apr 16 '13 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 Apr 17 '13 at 14:39
    
If you are using client object model you should be able to use jQuery just as well. It may be easier! –  spevilgenius Apr 18 '13 at 8:49
    
It has the answer i tested by id msdn.microsoft.com/en-us/library/jj164022.aspx –  user2098258 Jun 14 '13 at 5:46
    
This is a duplicate of: sharepoint.stackexchange.com/questions/46474/… Marking as duplicate. –  Hugh Wood Oct 30 '13 at 9:47
show 1 more comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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