4

How can i access a list in a subsite?

function Read () {

var caml = "<View><ViewFields><FieldRef Name='Title'/><FieldRef Name='ID'/><RowLimit>2</RowLimit></ViewFields ></View>";

var context = new SP.ClientContext.get_current();
var items = contextManagement.get_web().get_lists().getByTitle("Posts");

var query = new SP.CamlQuery();
query.set_viewXml(caml);
results = items.getItems(query);

context.load(results);

context.executeQueryAsync(Function.createDelegate(this, this.Div_befuellen), Function.createDelegate(this, this.onFailure));

}

This is the code, i all ready use. But how i has to change the code when the list is not on a subsite (Root\SubSite)?

I like to show News on the start page of a site collection but the news list is on a subsite (SharePointServer\News) from the sitecollection.

Thanks for your help Stefan

2
  • This is not directly an answer to your question, but I am curious why you aren't using the built in List View web part or even the Content Query web part? CQWP even lets you point it directly at the list you wish to use, even if it is in a subsite.
    – Dave Wise
    Dec 10 '12 at 20:51
  • Thanks for your help Dave. Because the list is in the subsite of the sitecollection and i want to show the items on a page in the root site. I dont cant finde the content query webpart in the foundation. And i have to use the foundation.
    – Stefan
    Dec 10 '12 at 21:12
10

Instead of using SP.ClientContext.get_current() use

var clientContext = new SP.ClientContext('server realtive url of the web');

Check this documentation for more info.

3
  • Thanks for your help. Code like this? Where News is a subsite. var context = new SP.ClientContext("SPServer/News"); var items = contextManagement.get_web().get_lists().getByTitle("Posts");
    – Stefan
    Dec 10 '12 at 21:14
  • 2
    var context = new SP.ClientContext('/News'); var list = context.get_web().get_lists().getByTitle("posts");
    – sssreddy
    Dec 10 '12 at 21:46
  • I can tell you that I tried this and got this ever-so-helpful message from SharePoint: "Cannot complete this action. Please try again." Edit: My bad. I was able to get the list, just not the items I wanted b/c of some issue with my CAML query. It's all good.
    – Newclique
    Aug 31 '17 at 18:12

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.