[C#.NET][Sharepoint 2013] 如何使用 REST API 上傳檔案 for X-RequestDigest header

[C#.NET][Sharepoint 2013] 如何使用 REST API 上傳檔案 for X-RequestDigest header

續上篇,http://www.dotblogs.com.tw/yc421206/archive/2014/06/12/145512.aspx

本文章節


準備工作

根據下圖,新增檔案需要:

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

2.使用 HttpMethod.Post

3.根據 http://msdn.microsoft.com/en-us/library/office/jj164022%28v=office.15%29.aspx#bk_requestElements 所述 ,使用 OAuth 選 Authentication header ;反之選 X-RequestDigest header,本篇使用 X-RequestDigest header

下圖出自,http://zoom.it/zdjx#full

image

 


 

取得 FormDigestValue for  Advanced REST client

URI 為 http://sps2013/_api/contextinfo

image

image

 

取得 FormDigestValue for  C#

整個資料取得的方式並不困難,這裡使用了Linq to Json (Json.NET from NuGet)將 FormDigestValue 拿出來


{
    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<JProperty>().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();
    }
}

 

本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/06/12/145518.aspx

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo