Wednesday, May 25, 2016

Get the Web Collection/Subsites from a Web object using CSOM

To fetch the subsites from a Web site you need to pass the credential of user who is having access on the Site. These information cannot be fetched anonymously.

I tried with the ajax call with REST API without passing the credentials
<site Url> + "/_api/web/Webs

It popup a NTLM Prompt to pass the credential. Which I doesn't want. So I Wrote a SharePoint Service for this. And host it under the SharePoint. There I passed the credential of APP pool account/ Service account for these kind of operation

Here is the code for the same


   private WebCollection GetWebs(string url, string userID, string password, string domain)
        {
            using (ClientContext clientContext = new ClientContext(url))
            {
                NetworkCredential credentials = new NetworkCredential(userID, password, domain);
                clientContext.Credentials = credentials;
                Web web = clientContext.Web;
                clientContext.Load(web, website => website.Webs, website => website.Title);
                clientContext.ExecuteQuery();
                WebCollection webCol = web.Webs;
                return webCol;
            }

        }

No comments:

Post a Comment