none
Failed to connect to SharePoint by Client Object Model RRS feed

  • Question

  • Hello,

     I write the following code to connect to share point by client object model :

      ClientContext context = new ClientContext("http://servername:8010");
       context.Credentials = new System.Net.NetworkCredential(@"admin", "123", "TestDomain");
        context.ExecutingWebRequest += clientContext_ExecutingWebRequest;

    I get the following exception :

      Unhandled Exception: System.Net.WebException: Unable to connect to the remote se
    rver ---> System.Net.Sockets.SocketException: A connection attempt failed becaus
    e the connected party did not properly respond after a period of time, or establ
    ished connection failed because connected host has failed to respond 

    How Can I solve it ?


    ASk

    Sunday, September 28, 2014 12:48 PM

Answers

  • Hi Himo,

    if you are connecting to office 365 then use the following code:

     using (srcContext = new ClientContext(txtUrlFrom.Text))
                    {
    
                        SecureString passWord = new SecureString();
                        foreach (char c in txtPasswordFrom.Text.ToCharArray()) passWord.AppendChar(c);
                        srcContext.Credentials = new SharePointOnlineCredentials(txtUserNameFrom.Text, passWord);
                    }

    if you are using on premise SharePoint 2013 use this code:

     using (srcContext = new ClientContext(txtUrlFrom.Text))
                    {
    
                        NetworkCredential credentials = new NetworkCredential(txtUserNameFrom.Text, txtPasswordFrom.Text);
                    }

    example for retrieving 

     Web srcWeb = srcContext.Web;
                List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
                ListItemCollection col = srcList.GetItems(new CamlQuery());
                srcContext.Load(srcList.RootFolder);
                srcContext.Load(srcList.RootFolder.Folders);
    
                srcContext.Load(col);
                srcContext.ExecuteQuery();

    Let me know if this helps


    Kind Regards, John Naguib Technical Consultant/Architect MCITP, MCPD, MCTS, MCT, TOGAF 9 Foundation

    Monday, September 29, 2014 8:36 AM
  • Hi,

    If you working SharePoint 2010 Client Object Model on an SSL enabled SharePoint 2010 site, here is a blog for your reference:

    http://blogs.msdn.com/b/sridhara/archive/2010/10/19/working-with-sharepoint-2010-client-object-model-on-an-ssl-enabled-site.aspx

    If you have a claims based site set up supporting both Windows and forms authentication, you can see the link below:

    http://sharepointdragons.com/2012/04/20/authentication-when-using-the-sharepoint-client-object-model/

    Best Regards


    Dennis Guo
    TechNet Community Support

    Monday, September 29, 2014 9:21 AM
    Moderator
  • Hi Himo,

    Thanks for posting your issue, Below is the correct code to access SharePoint 2013 Site using Client Object Model.

    ClientContext context = new ClientContext(lServerUrl);
            context.AuthenticationMode = ClientAuthenticationMode.Default;
            context.Credentials = new System.Net.NetworkCredential("user", "password", "domain");        

            Web web = context.Web;

    Another code snippet 

    using System.Net;
    using Microsoft.SharePoint.Client;

    using (ClientContext context = new ClientContext("http://yourserver/")) {
        context.Credentials = new NetworkCredential("user", "password", "domain");
        List list = context.Web.Lists.GetByTitle("Some List");
        context.ExecuteQuery();

        // Now update the list.
    }

    I hope this is helpful to you. If this works, Please mark it as Answered.

    Regards,

    Dharmendra Singh (MCPD-EA | MCTS)

    Blog : http://sharepoint-community.net/profile/DharmendraSingh

    Monday, September 29, 2014 10:09 AM

All replies

  • Hi Himo,

    if you are connecting to office 365 then use the following code:

     using (srcContext = new ClientContext(txtUrlFrom.Text))
                    {
    
                        SecureString passWord = new SecureString();
                        foreach (char c in txtPasswordFrom.Text.ToCharArray()) passWord.AppendChar(c);
                        srcContext.Credentials = new SharePointOnlineCredentials(txtUserNameFrom.Text, passWord);
                    }

    if you are using on premise SharePoint 2013 use this code:

     using (srcContext = new ClientContext(txtUrlFrom.Text))
                    {
    
                        NetworkCredential credentials = new NetworkCredential(txtUserNameFrom.Text, txtPasswordFrom.Text);
                    }

    example for retrieving 

     Web srcWeb = srcContext.Web;
                List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
                ListItemCollection col = srcList.GetItems(new CamlQuery());
                srcContext.Load(srcList.RootFolder);
                srcContext.Load(srcList.RootFolder.Folders);
    
                srcContext.Load(col);
                srcContext.ExecuteQuery();

    Let me know if this helps


    Kind Regards, John Naguib Technical Consultant/Architect MCITP, MCPD, MCTS, MCT, TOGAF 9 Foundation

    Monday, September 29, 2014 8:36 AM
  • Hi,

    If you working SharePoint 2010 Client Object Model on an SSL enabled SharePoint 2010 site, here is a blog for your reference:

    http://blogs.msdn.com/b/sridhara/archive/2010/10/19/working-with-sharepoint-2010-client-object-model-on-an-ssl-enabled-site.aspx

    If you have a claims based site set up supporting both Windows and forms authentication, you can see the link below:

    http://sharepointdragons.com/2012/04/20/authentication-when-using-the-sharepoint-client-object-model/

    Best Regards


    Dennis Guo
    TechNet Community Support

    Monday, September 29, 2014 9:21 AM
    Moderator
  • Hi Himo,

    Thanks for posting your issue, Below is the correct code to access SharePoint 2013 Site using Client Object Model.

    ClientContext context = new ClientContext(lServerUrl);
            context.AuthenticationMode = ClientAuthenticationMode.Default;
            context.Credentials = new System.Net.NetworkCredential("user", "password", "domain");        

            Web web = context.Web;

    Another code snippet 

    using System.Net;
    using Microsoft.SharePoint.Client;

    using (ClientContext context = new ClientContext("http://yourserver/")) {
        context.Credentials = new NetworkCredential("user", "password", "domain");
        List list = context.Web.Lists.GetByTitle("Some List");
        context.ExecuteQuery();

        // Now update the list.
    }

    I hope this is helpful to you. If this works, Please mark it as Answered.

    Regards,

    Dharmendra Singh (MCPD-EA | MCTS)

    Blog : http://sharepoint-community.net/profile/DharmendraSingh

    Monday, September 29, 2014 10:09 AM