I'm trying to write a C# application that can be executed automatically to upload a 2 GB .zip file to a Office365 SharePoint Online site. However, I can't seem to get any approach I try to work. After many fruitless attempts, I've been diving into using the "Lists.asmx" service reference, which appears promising, but I keep getting exceptions no matter how I configure things. It seems like there should be a solution out there for this, how can I upload a single 2 GB .zip file to SharePoint Online using C#?
For credentials, I've simply been using the credentials I'd use to manually log in to SharePoint online.
The code below throws an exception caught by the last catch block with the message:
SOAP header Security was not understood
If I use ListsSoapClient(b, ea)
rather than the no arg constructor, a different exception is thrown caught by the last catch block stating:
the content type (windows-1252) does not match the binding content type (utf-8)
Also, as a side note: I'm assuming the ListsSoapClient() no-arg constructor uses the binding I've specified in the App.config file?
Program.cs
string srcUrl = @"testingBACKUP.zip";
FileStream fStream = File.OpenRead(srcUrl);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
EndpointAddress ea = new EndpointAddress("https://companyname.sharepoint.com/");
WSHttpBinding b = new WSHttpBinding();
b.Security.Mode = SecurityMode.TransportWithMessageCredential;
//b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
ListsSoapClient listService = new ListsSoapClient();//b, ea);
listService.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
listService.ClientCredentials.UserName.UserName = "<username>";
listService.ClientCredentials.UserName.Password = "<password>";
try
{
var testing = listService.GetListContentType("Company", "windows-1252");
string addAttach = listService.AddAttachment("Company", "1", fileName, contents);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
// catch error
}
catch (Exception e)
{
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ListsSoap"
closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2000000" maxBufferPoolSize="2000000" maxReceivedMessageSize="2000000"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://companyname.sharepoint.com/_vti_bin/Lists.asmx"
binding="basicHttpBinding" bindingConfiguration="ListsSoap"
contract="SharePointListsReference.ListsSoap" name="ListsSoap" />
</client>
</system.serviceModel>
</configuration>