1

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?

RAS
  • 8,100
  • 16
  • 64
  • 86
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • 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`: http://msdn.microsoft.com/en-us/library/ee658739 – Rawling Aug 17 '12 at 07:18
  • Ok, I discovered how to solve this: – Thiago Custodio Aug 17 '12 at 14:09
  • ClientContext clientContext = new ClientContext(string.Format("http://{0}:{1}/", server, port)); WebCollection webs = clientContext.Web.GetSubwebsForCurrentUser(null); – Thiago Custodio Aug 17 '12 at 14:10

1 Answers1

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

WebCollection webs = clientContext.Web.GetSubwebsForCurrentUser(null);
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90