1
votes

I need to retrieve subsites that user has access using Client Object Model.

Here's what I've got:

          NetworkCredential credential = new NetworkCredential(user, pwd, domain);
          ClientContext clientContext = new ClientContext(string.Format("http://{0}:{1}/", server, port));
          clientContext.Credentials = credential;

            Web oWebsite = clientContext.Web;

            clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
            clientContext.ExecuteQuery();

            infos = new List<Infos>();

            Infos info = null;

            for (int i = 0; i != oWebsite.Webs.Count; i++)
            {
                info = new Infos();

                info.SubSite = oWebsite.Webs[i].Title;
                info.UrlSubSite = oWebsite.Webs[i].ServerRelativeUrl;

                ListCollection lists = oWebsite.Webs[i].Lists;
                clientContext.Load(lists);
                clientContext.ExecuteQuery();

                foreach (var list in lists)
                {
                    Lists myList = new Lists();
                    myList.Title = list.Title;

                    info.Listas.Add(myList);
                }

                infos.Add(info);
            }

            var query = from Infos i in infos
                        select i.SubSite;

            return query.ToList();
        }
        catch(Exception ex)
        {
            throw ex;
        }

        return null;
    }

=====

Using this code, I'm getting an 403 forbidden error. My client user doesn't have access on the root site (that I`m using in ClientContext constructor). But he has under some subsites. I need to get the subsites that he has access (just list them all), using Client Object Model.

Is there another way?

1
Where are you getting the error? If it's when you get the top-level web, I'm not sure how to help. But if it's when you iterate the child webs, you can use getSubwebsForCurrentUser instead of Webs: msdn.microsoft.com/en-us/library/ee658739Rawling
Ok, I discovered how to solve this:Thiago Custodio
ClientContext clientContext = new ClientContext(string.Format("http://{0}:{1}/", server, port)); WebCollection webs = clientContext.Web.GetSubwebsForCurrentUser(null);Thiago Custodio

1 Answers

1
votes
ClientContext clientContext =
 new ClientContext(string.Format("http://{0}:{1}/", 
                                            server, 
                                            port)); 

WebCollection webs = clientContext.Web.GetSubwebsForCurrentUser(null);