0

I am trying to use the CSOM to access data from a root site, then loop through its subsites to get into files that are stored in its lists. When I go to create the ClientContext, it works fine with the root URL, but when I use one of the subsites' URL's, I get a 401 when I hit ExecuteQuery().

using (var clientContext = new ClientContext(rootURL))
        {
            Console.WriteLine("Establishing connection...");

            var userName = System.Configuration.ConfigurationManager.AppSettings["userName"];
            var domain = System.Configuration.ConfigurationManager.AppSettings["domain"];
            var password = System.Configuration.ConfigurationManager.AppSettings["pwd"];
            var credentials = new NetworkCredential(userName, password, domain);
            clientContext.Credentials = credentials;

            _clientContext = clientContext;

            var spContext2 = new SharePointClientDataContext(clientContext);

            ClientContext newContext = new ClientContext(subsiteURL);
            var allLists = newContext.Web.Lists;
            newContext.Load(allLists);
            newContext.ExecuteQuery();

            try
            {
...

The code fails at newContext.ExecuteQuery(). Any ideas why I would run into 401's at the subsite level, but not the root level?

It is also worth noting that this DOES work locally, but I am currently trying to run this from my host OS to access files from my VM.

0

My problem was that I was not re-setting the credentials for the new ClientContext, so I just needed to do:

newContext.Credentials = credentials;

After doing that, it worked fine.

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.