LoginSignup
2
7

More than 5 years have passed since last update.

Microsoft Graph を使ってみよう : サイト、リスト、リストアイテムリソース (SharePoint)

Last updated at Posted at 2018-05-22

Microsoft Graph における SharePoint 操作

CSOM などでの開発と比べると Microsoft Graph で行えることは限定的です。主にファイルやリストの操作に限定されているため、引き続き用途に応じて開発を使い分ける必要があります。

SharePoint サイト

SharePoint は企業内で複数のサイトコレクションやサイトがあります。リストを取得するには、まずサイトにアクセスする必要があります。ドキュメントについては前回の記事をご覧ください。

主なシナリオ

  • SharePoint サイトの取得、サブサイトの一覧取得
  • リストの一覧取得

SharePoint サイトリソースの操作

いくつか代表的な操作を以下に紹介します。

サイトの取得や検索
root を指定すると既定のサイトが取得でき、search パラメーターを使うと検索ができます。

GET: https://graph.microsoft.com/v1.0/sites/root
GET: https://graph.microsoft.com/v1.0/sites/{id}
GET: https://graph.microsoft.com/v1.0/sites?search='search_word'

C#
await graphClient.Sites["root"].Request().GetAsync();
await graphClient.Sites["id"].Request().GetAsync();
await graphClient.Sites.Request
(
    new List<Option>() { new QueryOption("search", "'de:code'") }
).GetAsync();

サイト内のリスト一覧

GET: https://graph.microsoft.com/v1.0/sites/root/lists

C#
await graphClient.Sites["root"].Sites.Request().GetAsync();

SharePoint リストリソース

SharePoint サイト内にあるリストについてはリストリソースとして表現されます。

主なシナリオ

  • リストの取得、作成
  • リスト アイテムの一覧表示、作成、更新、削除

SharePoint リストリソースの操作

いくつか代表的な操作を以下に紹介します。

リストの作成

POST: https://graph.microsoft.com/v1.0/sites/{id}/lists

body
{
  "displayName": "保有機材リスト",
  "columns": [
    {
      "name": "AssetName",
      "displayName": "機材名",
      "text": { }
    },
    {
      "name": "Amount",
      "displayName": "台数",
      "number": { }
    }
  ],
  "list": {
    "template": "genericList"
  }
}
C#
var list = new List() { DisplayName = "保有機材リスト" };
list.Columns = new ListColumnsCollectionPage();
list.Columns.Add(new ColumnDefinition() { Name = "AssetName", DisplayName = "機材名", Text = new TextColumn() });
list.Columns.Add(new ColumnDefinition() { Name = "Amount", DisplayName = "台数", Number = new NumberColumn() });
await graphClient.Sites["root"].Lists.Request().AddAsync(list);

リストと列設定の取得

GET: https://graph.microsoft.com/v1.0/sites/root/lists/{id}?$&expand=columns

C#
await graphClient.Sites["root"].Lists["id"].Request().Expand("columns").GetAsync();

リスト内のアイテムおよび詳細取得

GET: https://graph.microsoft.com/v1.0/sites/root/lists/{id}/items?expand=fields
csharp:C#
await graphClient.Sites["root"].Lists["id"].Items.Request().Expand("fields").GetAsync();

リストアイテムリソース

最後に SharePoint リスト内に含まれる各種アイテムは、リストアイテムとして表現されています。リストがドキュメントライブラリの場合はファイルやフォルダがアイテムとして含まれますが、通常のリストの場合は、リストに対するアイテムが格納されています。

主なシナリオ

  • リストアイテムの取得、作成、更新、削除
  • バージョンの取得

SharePoint リストアイテムリソースの操作

いくつか代表的な操作を以下に紹介します。

リストアイテムの追加

POST: https://graph.microsoft.com/v1.0/sites/root/lists/{id}/items

body
{
  "fields": {
    "AssetName": "Surface Pro",
    "Amount": 5
  }
}
C#
var listItem = new ListItem();
listItem.Fields = new FieldValueSet() { AdditionalData = new Dictionary<string, object>()
{
    { "AssetName", "Surface Pro"},
    { "Amount", 5}
}};
await graphClient.Sites["root"].Lists["id"].Items.Request().AddAsync(listItem);

リストアイテムの取得

GET: https://graph.microsoft.com/v1.0/sites/root/lists/{list_id}/items/{item_id}

C#
await graphClient.Sites["root"].Lists["list_id"].Items["item_id"].Request().GetAsync();

SharePoint 開発者向けのメモ

重要な情報であるため、以下に 既存の SharePoint 開発者向けのメモ にある情報を抜粋します。

Microsoft Graph の SharePoint API は、CSOM API と大きく異なる点がいくつかあります。 サイト リソースは SPWeb にマッピングされます。 サイト コレクションのルート サイト (SPWeb) には、SPSite に関する情報を含む siteCollection ファセットが含まれています。 サイトの ID はそのサイト コレクション内でのみ一意であり、ID でサイトを指定する場合、サイト コレクション識別子とサイト識別子の両方を指定する必要があります。

GET https://graph.microsoft.com/v1.0/sites/{hostname},{spsite-id},{spweb-id}/
ホスト名のみで作成された URL は、既定のサイト コレクションのルート サイト (SPWeb) をポイントします。

GET https://graph.microsoft.com/v1.0/sites/{hostname}
ホスト名と siteCollection (SPSite) ID のみで作成された URL は、指定したサイト コレクションのルート サイト (SPWeb) をポイントします。

GET https://graph.microsoft.com/v1.0/sites/{hostname},{spsite-id}

まとめ

SharePoint には社内の情報が多く集積されていることから、より重要な情報をうまく共有したり、変換することが求められます。Graph API で自動化することを検討してください。

目次へ戻る

参照

Microsoft Graph での SharePoint 操作
Microsoft Graph でのサイト操作
Microsoft Graph でのリスト操作
Microsoft Graph でのリストアイテム操作
既存の SharePoint 開発者向けのメモ

2
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
7