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 am using Client Object Model to access a document library on a VM SharePoint server where I do have 'FullControl' access to a Site & Site collection.

The below code works on my SharePoint Foundation environment installed on my machine, but not on the network SharePoint Server (HTTP 500 error).

  ClientContext clientContext = new ClientContext(siteUrl);
  Web site = clientContext.Web;
  clientContext.Load(site);
  clientContext.ExecuteQuery();

So, I have tried using

  System.Net.CredentialCache cc = new System.Net.CredentialCache();
  NetworkCredential nc = new NetworkCredential("uName","pwd", "domain");

Using the above code gives HTTP 401 unauthorized error.

I go to the site and see my name on the site with Full Control. How to resolve this?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

There can be many reasons why you are receiving 401 and it really depends on how your 'network SP server' is set and mainly what kind of authorization it is using.

Before proceeding first verify that your username, password and domain are correct (and then double check it).

You can also check this similar question: HTTP 401, Unauthorized using the Managed Client Object Model

If you are using FBA (Forms Based Authentication) you need to specify your credentials as follows:

ClientContext ctx = new ClientContext(someSiteUrl); 
ctx.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication; 
ctx.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo(myUserName, myUserPwd);

Using the Client Object Model with a Forms Based Auth Site in SharePoint 2010

And if you are using mixed authentication (Windows & FBA) then you need also to apply a little 'hack':

...
clientContext.ExecutingWebRequest += new EventHandler(clientContext_ExecutingWebRequest);
...
}

static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
  e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}

Accessing mixed authentication web app with Client Object Model and Web Serices

share|improve this answer
    
clientContext.Credentials = CredentialCache.DefaultNetworkCredentials; worked for me. –  Yalaman Apr 11 '12 at 18:46
    
@Yalaman - great! You should upvote linked answer (that I provided) if it was helpfull. You should also consider accepting/upvoting my answer or you can even add your own solution to this problem and then accept it. By doing so you can help someone else facing same issue. –  Vedran Rasol Apr 11 '12 at 18:54
add comment

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.