0

So I was working with a hosted SharePoint site before and everything was working fine when I authenticated to that site using the code below:

private static NetworkCredential credentials = new NetworkCredential(username, password, domain);
private static ClientContext clientContext = new ClientContext("https://thisonesite.com/hosting/151");
private static Web site = clientContext.Web;
private static List list = site.Lists.GetByTitle(listName);

private static void sharepointLogin()
{
    try
    {
       //Loads credentials needed for authentication
       clientContext.Credentials = credentials;
       //Loads the site
       clientContext.Load(site);
       //Loads the site list
       clientContext.Load(list);
       //Executes the previous queries on the server
       clientContext.ExecuteQuery();
       //Writes out the title of the SharePoint site to the console
       Console.WriteLine("Title: {0}", site.Title);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }
}

Now, I had a sandbox created for me and I can access (manually) the site just fine. However, when I try to authenticate to the site through my c# code, I get an error.

And other than the username, password, and domain, all I have really changed is private static ClientContext clientContext = new ClientContext("http://sandbox");.

The error I get in the console is:

System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at ConsoleApp1.SharePointAccess.sharepointLogin() in C:\directortypath

Does anyone know why suddenly this error is occurring on the new SharePoint site?

This 0ne Pr0grammer
  • 2,632
  • 14
  • 57
  • 81
  • Please don't put tags like "C# - " in your titles. You've already got the [tag:C#] tag, and that's better. – John Saunders Aug 09 '11 at 16:23
  • @John Sorry, I'll note that for future reference. – This 0ne Pr0grammer Aug 09 '11 at 16:28
  • I am assuming that you have stepped through this code to see which line is actually throwing the exception. Which is it? Also I don't see a definition for the "list" variable... – meccaneko Aug 09 '11 at 16:24
  • @meccaneko Sorry, I didn't include all the code. But that definition is in there, I added it to the OP. And the error occurs at line 130 or `clientContext.ExecuteQuery();`, but that doesn't help me much. :/ – This 0ne Pr0grammer Aug 09 '11 at 16:35

1 Answers1

0

You need a credential before every executeQuery()

and you need two executeQuery() for loading the site and list separately.

clientContext.Credentials = credentials;
       clientContext.Load(site);
       clientContext.ExecuteQuery();

clientContext.Credentials = credentials;
       clientContext.Load(list);
       clientContext.ExecuteQuery();
Tony Wu
  • 1,040
  • 18
  • 28