0
votes

I'm attempting to write a console application to connect to one of our sharepoint application sites. I'm running a basic example to ensure connectivity. The program keeps failing with the following error: The remote server returned an error: (500) Internal Server Error.

Status: System.Net.WebExceptionStatus.ProtocolError

I don't have access to the IIS server to check the logs. My code is as follows:

        string siteUrl = "http://spsiteurl/";


        ClientContext clientContext = new ClientContext(siteUrl);
        clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
        clientContext.Credentials = new NetworkCredential("...", "...", "...");

        Web oWebsite = clientContext.Web;

        clientContext.Load(oWebsite,
            w => w.Title);

        clientContext.ExecuteQuery();

        Console.WriteLine(oWebsite.Title);

Any direction on where I could be going wrong?

1
In order for this to work, you must run this code on the SharePoint web front end that this site runs on. Is that what you're doing? - Robbert
No. I'm running this on my own development machine. I moved the dlls to my box and referenced them in Visual Studio. I take it that is not possible? Are there any other options to access the site remotely? Web Services perhaps? My ultimate goal is to add items to a SharePoint list programmatically from a piece of integration software we are using. - Diggs
Yes, web services are the only way to access sharepoint this way. - Robbert

1 Answers

1
votes

try below code

    ClientContext context = new ClientContext("https://test.com/");
    System.Net.ServicePointManager.ServerCertificateValidationCallback =
        ((sender, certificate, chain, sslPolicyErrors) => true);

    string strUserName = @"abc";
    string strPassword = "pwd";
    var credentials = new NetworkCredential(strUserName, strPassword);
    Web web = context.Web;
    context.Load(web, w => w.Title);
    context.Credentials = credentials;
    context.ExecuteQuery();


    // Now, the web's properties are available and we could display 
    // web properties, such as title. 
    System.Console.WriteLine("Web Title");
    System.Console.WriteLine(web.Title);

it will work fine in console