1

I have a ctx object which points to the root site collection e.g "https://rootsitecoll" we are doing some processing on the list on the root level. Then we have a list at the sub sub site level "https://rootsitecoll/subsite1/subsite2/List1". Can we use the same

 using (var ctx = new ClientContext(rootSiteCollectionURL))
    {
//can i use the same ctx to accesss the list under the sub sub site level, if so how
    }
5

Yes, you can use the same context. To access the list in the subsite, you need to use the site.OpenWeb method.

You can use it as below:

using (ClientContext context = new ClientContext(rootSiteCollectionURL))
{

    Web oWeb = context.Site.OpenWeb("server-relativeurl"); 
    //Web oWeb = context.Site.OpenWeb("subsite1/subsite2/");
    context.Load(oWeb);
    context.ExecuteQuery();

    var list = oWeb.Lists.GetByTitle("List1");
    context.Load(list);
    context.ExecuteQuery();

    ...your code to process the list items
}
1
  • hi @spStacker, did you try the above code ? Is it working for you ? Jun 30 '17 at 15:54
1

You can use the same context and read all the subsites. You can refer the following code.

ClientContext ctx = new ClientContext(siteUrl);
ctx.Credentials = new SharePointOnlineCredentials(Username, Password);

var subWebs = ctx.Web.Webs;
ctx.Load(subWebs);
ctx.ExecuteQuery();

foreach(var subWeb in subWebs)
{

    ctx.Load(subWeb);
    ctx.ExecuteQuery();
    Console.Write(subWeb.Title);
}

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.