2024/02/27(火)07:17
C#でSharePointRestAPIを使用するサンプルコード
using System;
using System.IO;
using System.Net;
using System.Text;
namespace SharePointRESTExample
{
class Program
{
static void Main(string[] args)
{
// SharePoint サイトの URL
string siteUrl = "https://your-sharepoint-site-url.com";
// SharePoint ドキュメント ライブラリの URL
string documentLibraryUrl = siteUrl + "/_api/web/lists/getByTitle('Documents')/items";
// SharePoint サイトのユーザー名とパスワード
string username = "your_username";
string password = "your_password";
// SharePoint REST API へのリクエストの準備
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(documentLibraryUrl);
request.Method = "GET";
request.Accept = "application/json;odata=verbose";
request.ContentType = "application/json;odata=verbose";
// ベーシック認証のヘッダーを追加
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
request.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
try
{
// REST API からのレスポンスを取得
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string responseString = reader.ReadToEnd();
// レスポンスの処理
Console.WriteLine(responseString);
}
catch (WebException ex)
{
// エラー処理
Console.WriteLine("Error: " + ex.Message);
}
}
}
}