2

So I have a collection of some teamsites and subsites with their lists. And if I for example as a user don't have permissions for one subsite, then I have an exception after sending a request:

        var rootWeb = ClientContext.Site.RootWeb;

        ClientContext.Load(
            rootWeb,
            w => w.Title,
            w => w.Url,
            w => w.Lists,
            w => w.Lists.Include(
                list => list.RootFolder.Name,
                list => list.RootFolder.ServerRelativeUrl,
                list => list.ParentWeb.Url
            ),
            w => w.Id,
            w => w.Webs.Include(
                subWeb => subWeb.Title,
                subWeb => subWeb.Url,
                subWeb => subWeb.Lists,
                subWeb => subWeb.Lists.Include(
                    list => list.RootFolder.Name,
                    list => list.RootFolder.ServerRelativeUrl,
                    list => list.ParentWeb.Url
                ),
                subWeb => subWeb.Id
            )
        );
        ClientContext.ExecuteQuery();

because of that I can't get entire structure.

How can I get entire structure if I don't have permissions for one subsite? Thanks

2
  • 1
    One way might be to split it up in seperate loads that you run recursively. Sep 18 '14 at 7:41
  • THanks for your answer. YEs, I thought about this, but it will be not very good for performance, so I thought there is another solution
    – Vitaly
    Sep 18 '14 at 8:40
2

I would recommend to utilize Web.GetSubwebsForCurrentUser method instead Web.Webs property since it returns the security trimmed (the current users has access) list of sub sites.

Modified example

var objectsToInclude = new Expression<Func<Web, object>>[]
     {
           w => w.Title,
           w => w.Url,
           w => w.Lists,
           w => w.Lists.Include(
           list => list.RootFolder.Name,
           list => list.RootFolder.ServerRelativeUrl,
           list => list.ParentWeb.Url)
     };

//1.Load root web
ctx.Load(ctx.Site.RootWeb, objectsToInclude);
var subWebs = ctx.Site.RootWeb.GetSubwebsForCurrentUser(null);
//2.Load sub webs
ctx.Load(subWebs, wcol => wcol.Include(objectsToInclude));
ctx.ExecuteQuery();

Note: both Web.GetSubwebsForCurrentUser method and Web.Webs property returns only one level beneath web sites, nothing more. So, if you are interested in returning the whole web tree structure, the example will be different.

2
  • we use two levels max. I've tried your solution but as the result it's throwed the exception little bit later
    – Vitaly
    Sep 18 '14 at 13:39
  • How can I get the tree with two levels of depth ?
    – Vitaly
    Sep 18 '14 at 16:28

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.