I'm trying to upload a text file to a sharepoint 2010 site. I'm using the following code:
Using stream As New MemoryStream() Dim writer As New StreamWriter(stream) writer.Write(line) writer.Flush() stream.Position = 0 Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContextLoad, filename, stream, True) stream.Close() End Using
I'm getting the following error and do not know how to get around it.
Microsoft.SharePoint.Client.ClientRequestException was unhandled by user code
HResult=-2146233088
Message=Unexpected response from the server. The content type of the response is "text/html; charset=utf-8". The status code is "Redirect".
Source=Microsoft.SharePoint.Client
StackTrace:
at Microsoft.SharePoint.Client.File.SaveBinary(ClientContext context, String serverRelativeUrl, Stream stream, String etag, Boolean overwriteIfExists, SaveBinaryCheckMode checkMode)
at Microsoft.SharePoint.Client.File.SaveBinaryDirect(ClientContext context, String serverRelativeUrl, Stream stream, Boolean overwriteIfExists)
at Text_Replacer.Form1.wrk_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e) in C:\Users\Me\Documents\Visual Studio 2010\Projects\SWSPTools\SWSPTools\Editor\Form1.vb:line 502
InnerException:
One other piece of info. We are using fedauth so I have to use the following to authenticate and generate the PUT method.
public void ClientContext_ExecutingWebRequestLoad(object sender, Microsoft.SharePoint.Client.WebRequestEventArgs e) { CookieContainer cc = new CookieContainer(); foreach (var c in SharepointTrustCookies) cc.Add(c); e.WebRequestExecutor.WebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; e.WebRequestExecutor.WebRequest.CookieContainer = cc; e.WebRequestExecutor.WebRequest.Method = "PUT"; e.WebRequestExecutor.WebRequest.Accept = "*/*"; e.WebRequestExecutor.WebRequest.ContentType = "multipart/form-data; charset=utf-8"; e.WebRequestExecutor.WebRequest.AllowAutoRedirect = false; e.WebRequestExecutor.WebRequest.Headers.Add("Accept-Language", "en-us"); e.WebRequestExecutor.WebRequest.Headers.Add("Translate", "F"); e.WebRequestExecutor.WebRequest.Headers.Add("Cache-Control", "no-cache"); // e.WebRequestExecutor.WebRequest.ContentLength = line.Length; }
Here is how I create the clientContextLoad:
Dim clientContextLoad As ClientContext = New ClientContext(MY_SITE) Dim spAuth As SharepointClaimsAuthentication = New SharepointClaimsAuthentication(MY_USER_NAME, MY_PWD, INTERNAL_AUTH_SITE, EXTERNAL_AUTH_SITE, MY_SITE) spAuth.Authenticate() AddHandler clientContextLoad.ExecutingWebRequest, AddressOf spAuth.ClientContext_ExecutingWebRequestLoad clientContextLoad.Credentials = CredentialCache.DefaultCredentials
And here is the filename I am passing: /sites/my_Site/Team_Documents/Output.txt
I can successfully authenticate and iterate the sharepoint site because prior to writing the file I read the file and make a regex change. The issue is when I try to save the file.