[C # .NET] [Sharepoint 2013] how to use the REST API to upload files for X-RequestDigest header

[C # .NET] [Sharepoint 2013] how to use the REST API to upload files for X-RequestDigest header


Continued article, http: //www.dotblogs.com.tw/yc421206/archive/2014/06/12/145512.aspx

This article section

  • Ready to work
  • 取得 FormDigestValue for  Advanced REST client
  • Get FormDigestValue for C #
  • Upload files for C #

Ready to work

According to the figure below, the new documents:

1.URI 为 http://sps2013/CsomApi/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files/add(url='myDoc.docx',overwrite=true)

2. Use HttpMethod.Post

The http://msdn.microsoft.com/en-us/library/office/jj164022%28v=office.15%29.aspx#bk_requestElements the use OAuth selected from the Authentication header; conversely selected from X-RequestDigest header, Benpian using X-RequestDigest header

The following figure from, http: //zoom.it/zdjx#full

image


取得 FormDigestValue for  Advanced REST client

URI 为 http://sps2013/_api/contextinfo

image

image

Get FormDigestValue for C #

The way the entire data acquired is not difficult, here using Linq to Json (Json.NET from NuGet) will FormDigestValue out

{
    HttpClientHandler httpClientHandler = new HttpClientHandler()
    {
        Credentials = new NetworkCredential(YourId, YourPassword, YourDomain)
    };

    using (var httpClient = new HttpClient(httpClientHandler))
    {
        httpClient.BaseAddress = new Uri("http://sps2013");
        httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
        HttpResponseMessage response = httpClient.PostAsync("/_api/contextinfo", null).Result;
        HttpRequestMessage a = new HttpRequestMessage(); a.Method = HttpMethod.Post;
        if (response.StatusCode == HttpStatusCode.OK)
        {
            var result = response.Content.ReadAsStringAsync().Result;

            JObject jsonObject = JObject.Parse(result);
            var formDigestValue = jsonObject.Descendants()
                .OfType
 
 
  
  ().First(p => p.Name == "FormDigestValue")
            .Value;
            return formDigestValue.ToString();
        }

        return null;
    }
}
 
 

上传文件 for C#

最重要的是要带入 X-RequestDigest header

{
    HttpClientHandler httpClientHandler = new HttpClientHandler()
    {
        Credentials = new NetworkCredential(YourId, YourPassword, YourDomain)
    };
    using (var httpClient = new HttpClient(httpClientHandler))
    {
        httpClient.BaseAddress = new Uri("http://sps2013/CsomApi");
        httpClient.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
        httpClient.DefaultRequestHeaders.Add("X-RequestDigest", GetFormDigestValue());

        FileStream uploadStream = new FileStream("myDoc.docx", FileMode.Open, FileAccess.Read);
        HttpContent uploadContent = new StreamContent(uploadStream);
        HttpResponseMessage response = httpClient.PostAsync(
            "/_api/Web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files/add(url='myDoc.docx',overwrite=true)",
            uploadContent).Result;
        if (response.StatusCode == HttpStatusCode.OK)
        {
            var result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
        }
        Console.WriteLine("successful down file,please press any key contiune");
        Console.ReadKey();
    }
}

This article comes from: http: //www.dotblogs.com.tw/yc421206/archive/2014/06/12/145518.aspx

If any error, please notify the novice posting him to bear

2010 ~ 2017 C # in the fourth quarter

Original: Large column  [C # .NET] [Sharepoint 2013 ] how to use the REST API to upload files for X-RequestDigest header


Guess you like

Origin www.cnblogs.com/petewell/p/11457854.html