0

I need some understanding on using clientcontext and scope. I want to run code in sitecollection page that fetches information from a particular list column (status) that exists in all sub-sites and then populate a data table using JavaScript. Could someone give a high level explanation of how this is possible. I tried a few times but I think I get in trouble with scope so hasn't worked.

Do I need two ClientContext constructors?

  • 1
    It might be more worthwile in the log term to start with the REST API instead of using JSOM calls. – Danny '365CSI' Engelman Oct 27 '15 at 10:37
  • Thanks but prefer to stick with JavaScript for the moment as I've coded everything else the same. Is my scenario better solved with REST? – Orange Juice Jones Oct 27 '15 at 10:51
  • From how many sub sites do you have to pull these data from? – Unnie Oct 27 '15 at 11:00
  • 2
    REST is JavaScript, but a more modern API and yes, you do not have to bother about Context. If its better for your scenario you have to decide yourself. A year from now you will be glad you learned about REST a year ago. – Danny '365CSI' Engelman Oct 27 '15 at 11:51
  • 2
    REST is not JavaScript, REST is an architectural style for accessing information over HTTP, and in SharePoint's context refers to an API for accessing SharePoint data. You can use JavaScript or C# to interact with SharePoint's REST API <pedanticreply/> – John-M Oct 28 '15 at 13:51
2

The problem isn't which JavaScript API you pick here, the issue is that the data is distributed throughout various sub-sites.

You didn't mention which version of SharePoint you're using, but if you're using 2013 (or later) or SharePoint Online I would suggest you try accessing the search service since it will be able to easily query across different sites (there are many available techniques but you can use JSOM or REST) Here is a specific example using the REST API and an answer to another question with some more information on the same technique.

There are also some other techniques for fetching data from across different site collections that you might consider, but many of them require a lot more effort than tapping into the search service.

Some points of clarification:

It's possible to navigate to data in many different sub-sites using the JavaScript Client Object Model.

var myCtx = SP.ClientContext.get_current();
var root = myCtx.get_site().get_rootWeb(); // may need to do this if in a sub site
var webs = root.get_webs(); // fetch a reference to all the sub webs
myCtx.load(webs, "Include(Title)");
myCtx.executeQueryAsync(function() {
    // success -- log all of the web titles to the console
    webs.get_data().map(function(item) { console.log(item.get_title()) });
    // access a specific list in a sub web like...
    var subWebList = webs.get_item(1).get_lists().getByTitle("ListInSubWeb1");
    // do other work with the list like normal
    ...
    // navigate to other subweb lists...
    var subWebList2 = webs.get_item(2).get_lists().getByTitle("AnotherList");
    // do other stuff...
}, function() {
    // fail
});

The issue with this approach for what you're talking about is that you would need to know ahead of time what lists and what fields you were after or you would have to loop through everything and check. As far as I know, there isn't an easy way in the JSOM or the REST API to directly query for all the items of a specific content type from a particular site without using the search API. I see you have another question about enumerating content types which would allow you to check any lists or libraries manually though.

  • I agree, Search will be the way to go. Ideally with REST, since using JSOM for search is a pain (IMO). Great answer. – wjervis Oct 28 '15 at 14:08
  • Hi That's great, using SharePoint 2010 The sites now print to the console. I now need to fetch first item from the Projects list in every sub. Is 'subWebList' a collection, I've tried to fetch title of the 1st item but doesn't work, do I need to iterate the list? – Orange Juice Jones Oct 28 '15 at 15:07
  • I'm looking for a way where all subsites are scanned for list 'Projects'. When found the status of the 1st item from the list is returned to the top level, together with the subsite name to create a table. Here is another question I posted about this, the trouble seems to be I cant create a loop and add a asynchronous functions - sharepoint.stackexchange.com/questions/160599/… – Orange Juice Jones Oct 28 '15 at 15:56
  • @OrangeJuice Oh for 2010 it may be easiest to do something like that, I worked up a bit of an answer as an edit before I read your other question but I'll post over there how I would specifically loop through things in your webs – John-M Oct 28 '15 at 16:17

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.