4

I try to get list from remote SharePoint server using CSOM in .NET Web Application, but ClientContext.ExecuteQuery() return exception

Cannot contact site at the specified URL

If I try to get list from local server, all work fine. How do I fix it? Why an exception occurs?

I use following code:

    public string testRest()
    {
        try
        {
            ClientContext context = new ClientContext(new Uri(@"http://sharepointRemoteServer/sites/subsite"));
            context.AuthenticationMode = ClientAuthenticationMode.Default;
            context.Credentials = CredentialCache.DefaultNetworkCredentials;
            List spList = context.Web.Lists.GetByTitle("ListName");
            context.Load(spList);
            context.ExecuteQuery();
            return "OK";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
0

You're most probably facing what is known as a "double-hop" issue:

  1. Your client user connects and authenticate as himself against the Web app (through Windows NTLM protocol, that requires the client to know her/his password).
  2. The Web app uses impersonation. It means the code runs under the user's identity.
  3. With your context.Credentials = CredentialCache.DefaultNetworkCredentials, the call to SharePoint tries to authenticate with the user's identity, with NTLM. However, this requires the Web app to know the user's password: as it does not, the authentication fails (unless the client browses the Web app from the Web app server, in witch case it works fine).

Simplest solution here is to avoid impersonation at the Web app level (in the web.config); all requests to SharePoint will then be made Under the application pool's identity (that will require permissions under SharePoint, of course).

Another option I described here: Saving file from SharePoint Server to shared folder - Which user account?.

With these two above solutions, the connection to SP will always be made under the same identity, i.e. the app pool's identity.
If you really require the connection to be made on behalf of the initial user, the solution would be use Kerberos instead of NTLM, but that's a complicated one, involving Active Directory configuration, client requirements (IE, user logged on the same domain, ...).

  • Ok. I try disable impersonate for this Web app by adding to web.config identity impersonate="true", comment string context.Credentials = CredentialCache.DefaultNetworkCredentials, and enter to app pool's identity user with farm admin permissions (for test only), but Web app not work, same error. May be i misunderstood. – Sergey Jan 26 '16 at 13:48
  • You should have identity impersonate="false", keep context.Credentials = CredentialCache.DefaultNetworkCredentials, and set the app pool's identity as a site collection administrator on http://sharepointRemoteServer/sites/subsite. – Evariste Jan 26 '16 at 14:11
  • Not work. I try create ConsoleApp using this code. Same error. – Sergey Jan 26 '16 at 14:58
0

You can't get a subsite using ClientContext constructor. Try this:

public string testRest()
{
    try
    {   // In constructor, put only the Site URL
        ClientContext context = new ClientContext(new Uri(@"http://sharepointRemoteServer/"));
        context.AuthenticationMode = ClientAuthenticationMode.Default;
        context.Credentials = CredentialCache.DefaultNetworkCredentials;

        // Load subsites and their titles
        Web w = context.Web;
        context.Load(w, website => website.Webs,
                        website => website.Title);
        context.ExecuteQuery();

        // Get list's subsite by title
        Web subsite = (Web) webs.Select(w => w.Title.Equals("subsite"));

        List spList = subsite.Lists.GetByTitle("ListName");
        context.Load(spList);
        context.ExecuteQuery();
        return "OK";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

I hope it helps!

  • Nope. On first context.ExecuteQuery() same error. Cannot contact site at the specified URL sharepointRemoteServer/sites. – Sergey Jan 26 '16 at 19:44
  • The time I got the same exception was due to have put the subsite's URL instead of Site's URL in ClientContext constructor. Maybe you have to instantiate your ClientContext like this: ClientContext context = new ClientContext(new Uri(@"http://sharepointRemoteServer/")) – Celtik Jan 27 '16 at 10:30
  • I try use BurnSuite and locate trouble. My app try call /_vti_bin/sites.asmx and receive server response object reference not set to an instance of an object. In ULS Logs i see message: Application error when access /sites/subsite/_vti_bin/sites.asmx, Error=Object reference not set to an instance of an object. at ASP.global_asax.Application_BeginRequest(Object sender, EventArgs e) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) – Sergey Jan 27 '16 at 11:59
0

Solved! Trouble in JS for cross-site REST-query located in global.asax. This JS get User-agent from request header and replace request header in all query. Web application not send user-agent in request header and JS crash.

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.