Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm attempting my first foray into the Client Object Model.

In the following code,

   using (ClientContext context = new ClientContext(url)) // url works w- SPSite
   {
      Web web = context.Web;
      context.Load(web);

      Log.Debug("Loading web.");
      context.ExecuteQuery();
   }

The call to ExecuteQuery throws an exception:

Unhandled Exception: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

With so little info, I'm a little lost on where to begin debugging. There is no traffic in the ULS relating to an event... just this simple exception. What can I look into?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

An important note that I didn't realize was relevant in my question: my code is in a console application.

It was necessary for me to provide credentials to the ClientContext like so:

using (ClientContext context = new ClientContext(url)) // url works w- SPSite
   {
      // the domain parameter is optional
      // there is a constructor that takes only a username and password
      context.Credentials = new NetworkCredential("username", "password", "domain"); 

      Web web = context.Web;
      context.Load(web);

      Log.Debug("Loading web.");
      context.ExecuteQuery();
   }

This resolved my HTTP 500. However, I now have to figure out why I'm seeing an HTTP 401.

share|improve this answer
    
Maybe also add context.AuthenticationMode = ClientAuthenticationMode.Default; And it's also better if you create a custom class which extends the ClientContext so you can do the authentication in the constructor from your own custom class. –  Stef Sep 4 '12 at 8:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.