2

I am trying to use SaveBinaryDirect to upload a file to a Sharepoint Online library. Everything works perfectly on two different workstations I tried on (Windows 10), but once deployed to our UAT server, SaveBinaryDirect throws an exception with the following error message:

"Unable to connect to the remote server".

For reference, here is the code:

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using Microsoft.SharePoint.Client;



namespace testSP
{
    class Program
     {


    static class Constants
    {
        public const string SharepointURL = "https://***.sharepoint.com/";
        public const string Username = "***@***.com";
        public const string Password = "***";
    }



    static private ICredentials GetSharePointOnlineCredential()
    {
        var loginName = Constants.Username;
        var password = Constants.Password;



        var securePassword = new SecureString();
        password.ToCharArray().ToList().ForEach(c => securePassword.AppendChar(c));



        var credential = new SharePointOnlineCredentials(loginName, securePassword);



        return credential;
    }



    static void Main(string[] args)
    {
        try
        {
            ClientContext context = new ClientContext(Constants.SharepointURL);
            System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;
            context.Credentials = GetSharePointOnlineCredential();
            context.RequestTimeout = 60000000; // Time in milliseconds



            Console.WriteLine("logged in successfully!");



            string fName = Path.GetFileName("1234.txt");               
            string url = "/***/";



            string fnUrl = url + fName;
            using (FileStream fs = new FileStream("1234.txt", FileMode.Open, FileAccess.Read))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);
            }



            Console.WriteLine("uploaded '1234.txt' successfully!");
        }
        catch(Exception ex)
        {
            Console.WriteLine("Failed:" + ex.Message);
            Console.ReadLine();
            return;
        }

        Console.ReadLine();
    }
 }
}

Server information:

Windows Server 2012 R2, IIS Version 8.5.9600.16384

There are no firewall rules enforced (exception made for server). We can login and upload a file to Sharepoint successfuly through the browser.

Any help or suggestions would be thoroughly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.