none
client object model using vs 2010 return "the remote server returned an error 401 unauthorized" RRS feed

  • Question

  • hi,

    I've tried out example in the msdn (the basic one) on "using the SharePoint Foundation 2010 Managed Client Object Model" [http://msdn.microsoft.com/en-us/library/ee857094%28office.14%29.aspx] but unfortunately I've run out to "the remote server returned an error 401 unauthorized.

     

    here's the line where it gave me the error.

     clientContext.ExecuteQuery();



    i don't know what i did wrong coz i just change the url provided with mine only.

     

    hope you can help me with this. I work alone in this office and i'm the only person that use sharepoint with this method plus i'm totally new in sharepoint.

     

    TQ

    Thursday, February 24, 2011 6:25 AM

Answers

  • Hi,

    You should state the credentials. Please look at my example:

     

    using System;
    using Microsoft.SharePoint.Client;
    using System.Net;
    
    class DisplayWebTitle
    {
      static void Main()
      {
        ClientContext clientContext = new ClientContext("http://hercules");
        clientContext.Credentials = new NetworkCredential("Administrator", "12345");
        Web site = clientContext.Web;
        clientContext.Load(site);
        clientContext.ExecuteQuery();
        Console.WriteLine("Title: {0}", site.Title);
      }
    }
    

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    • Marked as answer by Wayne Fan Monday, February 28, 2011 8:04 AM
    Thursday, February 24, 2011 10:29 AM

All replies

  • hi,

    I've tried out example in the msdn (the basic one) on "using the SharePoint Foundation 2010 Managed Client Object Model but unfortunately I've run out to "the remote server returned an error 401 unauthorized.

     

    here's the line where it gave me the error.

     clientContext.ExecuteQuery();

    i don't know what i did wrong coz i just change the url provided with mine only.

     

    hope you can help me with this. I work alone in this office and i'm the only person that use sharepoint with this method plus i'm totally new in sharepoint.

     

    TQ

    clueless girl.

    • Moved by Mike Walsh FIN Thursday, February 24, 2011 8:31 AM SP 2010 q (From:SharePoint - Development and Programming (pre-SharePoint 2010))
    • Merged by Wayne Fan Friday, February 25, 2011 6:48 AM The same issue
    Thursday, February 24, 2011 5:06 AM
  • It seems you are asking a question related to SP 2010. Please post your questions related to SP 2010 programming here - http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010programming/threads.
    NLV - MCTS - Blog - Twitter
    Thursday, February 24, 2011 5:54 AM
  • i guess it's because of, access denied by URL authorization policy on the Web server,

    NetworkCredential credentials = new NetworkCredential("username", "pwd", "domain");
    ClientContext context = new ClientContext("http://site-url");
    context.Credentials = credentials;

    Vrishali

    Thursday, February 24, 2011 10:21 AM
  • Hi,

    You should state the credentials. Please look at my example:

     

    using System;
    using Microsoft.SharePoint.Client;
    using System.Net;
    
    class DisplayWebTitle
    {
      static void Main()
      {
        ClientContext clientContext = new ClientContext("http://hercules");
        clientContext.Credentials = new NetworkCredential("Administrator", "12345");
        Web site = clientContext.Web;
        clientContext.Load(site);
        clientContext.ExecuteQuery();
        Console.WriteLine("Title: {0}", site.Title);
      }
    }
    

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    • Marked as answer by Wayne Fan Monday, February 28, 2011 8:04 AM
    Thursday, February 24, 2011 10:29 AM
  • Hi Biru,

    Dmitry has provided you the correct answer.

    Happy Coding.

    Regards,

    Jaliya

    Thursday, February 24, 2011 12:25 PM
  • Hi cempaka,

     

    Thanks for your question.

    As I understand, you are using the Client Object Model to communicate with SharePoint. And the client object likes this:

    1. ClientContext clientContext = new ClientContext("http://intranet.contoso.com");
    2.         Web site = clientContext.Web;
    3.         clientContext.Load(site);
    4.         clientContext.ExecuteQuery();
    5.         Console.WriteLine("Title: {0}", site.Title);

     

    According to the error message, it is because the application cannot communicate with the Server with url.

     

    1.       Please check whether you have entered the right url for the SharePoint Site at first;

    2.       Please check whether you can access the SharePoint site by IE browsers in the current server. 

     

    Share your result.

    Friday, February 25, 2011 6:13 AM
  • Hi Wayne,

    With respect do we really have to put the URL as you have mentioned? Because in my project it is working nicely when i have put the URL as Dmitry has mentioned. Appreciate your answer.

    Happy Coding.

    Regards,

    Jaliya

    Friday, February 25, 2011 8:09 AM
  • hi,

     Try this way

        Windows Authentication :

          NetworkCredential netCre = new NetworkCredential("UserName","Password","Domain");
          ClientContext oContext = new ClientContext("http://intranet.contoso.com");
          oContext.AuthenticationMode = ClientAuthenticationMode.Default;
          oContext.Credentials = netCre;

     Regards,
     Rajeswari

     

     

     

      

      

      

     

     

    • Proposed as answer by B.Rajeswari Wednesday, March 2, 2011 8:21 AM
    • Unproposed as answer by B.Rajeswari Wednesday, March 2, 2011 8:21 AM
    Wednesday, March 2, 2011 8:20 AM