简体   繁体   中英

Microsoft.SharePoint.Client.ClientRequestException Error

I'm trying to upload a text file to a sharepoint 2010 site. I'm using the following code:

 Using fs As New FileStream(outputfile, FileMode.Open)
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContextLoad, filename, fs, True)
 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:

/Team_Documents/Output.txt

The second parameter filename of SaveBinaryDirect() should be a server-relative url, not site-relative. This means it should include the path of the site, eg /site/subsite/library/filename.txt

Additionally, you may want to confirm that your ClientContext object is being created from the root site of the site collection, not from a subsite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM