全て
| カテゴリ未分類
| 通常
| 数学
| 投資
| 副業
| 転職・就職
| ブログ
| 株Stock
| VBA
| SharePoint
| REST API
| RenderListDataAsStream
| PowerAutomateDesktop
| Graph API
| PowerApps
| PowerAutomate
| PowerFx
| OfficeScriptオフィススクリプト
| PowerAutomate日時Excel日時相互関係
| PowerShell
| Dataverse
| UiPath
| OneDrive
| LINQ
| VB.NET
| C#.NET
| WPFC#.NET
| Git
カテゴリ:REST API
using System; using System.Data; using System.IO; using System.Net; using System.Text; using Newtonsoft.Json.Linq; namespace SharePointRESTExample { class Program { static void Main(string[] args) { // SharePoint サイトの URL string siteUrl = "https://your-sharepoint-site-url.com"; // リストの名前 string listName = "YourListName"; // SharePoint サイトのユーザー名とパスワード string username = "your_username"; string password = "your_password"; // リストアイテムを取得する REST API URL string getListItemsUrl = siteUrl + "/_api/web/lists/getByTitle('" + listName + "')/items"; // SharePoint REST API へのリクエストの準備 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getListItemsUrl); 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(); // レスポンスから JSON データをパースして DataTable に変換 DataTable dataTable = ParseJsonToDataTable(responseString); // DataTable の内容を表示 foreach (DataRow row in dataTable.Rows) { foreach (DataColumn col in dataTable.Columns) { Console.WriteLine(col.ColumnName + ": " + row[col]); } Console.WriteLine("--------------------"); } } catch (WebException ex) { // エラー処理 Console.WriteLine("Error: " + ex.Message); } } // JSON データを DataTable に変換するメソッド static DataTable ParseJsonToDataTable(string json) { JObject jsonObject = JObject.Parse(json); JArray itemsArray = (JArray)jsonObject["d"]["results"]; DataTable dataTable = new DataTable(); foreach (JProperty property in itemsArray[0]) { dataTable.Columns.Add(property.Name, typeof(string)); } foreach (JObject item in itemsArray) { DataRow row = dataTable.NewRow(); foreach (JProperty property in item.Properties()) { row[property.Name] = property.Value.ToString(); } dataTable.Rows.Add(row); } return dataTable; } } } お気に入りの記事を「いいね!」で応援しよう
Last updated
2024.02.27 07:22:16
[REST API] カテゴリの最新記事
|