Answered by:
Failed to connect to SharePoint by Client Object Model

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 respondHow Can I solve it ?
ASk
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
- Marked as answer by Patrick_LiangModerator Sunday, October 5, 2014 11:45 PM
-
Hi,
If you working SharePoint 2010 Client Object Model on an SSL enabled SharePoint 2010 site, here is a blog for your reference:
If you have a claims based site set up supporting both Windows and forms authentication, you can see the link below:
Dennis Guo
TechNet Community Support- Marked as answer by Patrick_LiangModerator Sunday, October 5, 2014 11:46 PM
-
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
- Marked as answer by Patrick_LiangModerator Sunday, October 5, 2014 11:46 PM
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
- Marked as answer by Patrick_LiangModerator Sunday, October 5, 2014 11:45 PM
-
Hi,
If you working SharePoint 2010 Client Object Model on an SSL enabled SharePoint 2010 site, here is a blog for your reference:
If you have a claims based site set up supporting both Windows and forms authentication, you can see the link below:
Dennis Guo
TechNet Community Support- Marked as answer by Patrick_LiangModerator Sunday, October 5, 2014 11:46 PM
-
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
- Marked as answer by Patrick_LiangModerator Sunday, October 5, 2014 11:46 PM