SharePoint REST API - 列表和列表項
部落格地址:http://blog.csdn.net/FoxDave
本篇主要講述如何用SharePoint REST操作列表和列表項。閱讀本篇時請先了解前面講述的REST介紹和基本操作。
廢話不多說了,下面開始。
用REST獲取列表屬性
當你知道一個列表的GUID時,你可以用下面的請求來獲取這個列表物件。
url: http://site url/_api/web/lists(guid'list GUID'),
method: GET
Headers:
Authorization: "Bearer " + accessToken
accept: "application/json;odata=verbose" or "application/atom+xml"
如果你知道一個列表的標題,那麼可以用下面的請求。url: http://site url/_api/web/lists/GetByTitle('Test')
method: GET
Headers:
Authorization: "Bearer " + accessToken
accept: "application/json;odata=verbose" or "application/atom+xml"
下面是一個通過請求返回的XML結果示例,基本是跟API的屬性對應的,就不做過多講解了。<content type="application/xml">
<m:properties>
<d:AllowContentTypes m:type="Edm.Boolean">true</d:AllowContentTypes>
<d:BaseTemplate m:type="Edm.Int32">100</d:BaseTemplate>
<d:BaseType m:type="Edm.Int32">0</d:BaseType>
<d:ContentTypesEnabled m:type="Edm.Boolean">false</d:ContentTypesEnabled>
<d:Created m:type="Edm.DateTime">2012-06-26T23:15:58Z</d:Created>
<d:DefaultContentApprovalWorkflowId m:type="Edm.Guid">00000000-0000-0000-0000-000000000000</d:DefaultContentApprovalWorkflowId>
<d:Description>A list created by Project Based Retention used to store Project Policy Items.</d:Description>
<d:Direction>none</d:Direction>
<d:DocumentTemplateUrl m:null="true" />
<d:DraftVersionVisibility m:type="Edm.Int32">0</d:DraftVersionVisibility>
<d:EnableAttachments m:type="Edm.Boolean">true</d:EnableAttachments>
<d:EnableFolderCreation m:type="Edm.Boolean">false</d:EnableFolderCreation>
<d:EnableMinorVersions m:type="Edm.Boolean">false</d:EnableMinorVersions>
<d:EnableModeration m:type="Edm.Boolean">false</d:EnableModeration>
<d:EnableVersioning m:type="Edm.Boolean">false</d:EnableVersioning>
<d:EntityTypeName>ProjectPolicyItemList</d:EntityTypeName>
<d:ForceCheckout m:type="Edm.Boolean">false</d:ForceCheckout>
<d:HasExternalDataSource m:type="Edm.Boolean">false</d:HasExternalDataSource>
<d:Hidden m:type="Edm.Boolean">true</d:Hidden>
<d:Id m:type="Edm.Guid">74de3ff3-029c-42f9-bd2a-1e9463def69d</d:Id>
<d:ImageUrl>/_layouts/15/images/itgen.gif</d:ImageUrl>
<d:IrmEnabled m:type="Edm.Boolean">false</d:IrmEnabled>
<d:IrmExpire m:type="Edm.Boolean">false</d:IrmExpire>
<d:IrmReject m:type="Edm.Boolean">false</d:IrmReject>
<d:IsApplicationList m:type="Edm.Boolean">false</d:IsApplicationList>
<d:IsCatalog m:type="Edm.Boolean">false</d:IsCatalog>
<d:IsPrivate m:type="Edm.Boolean">false</d:IsPrivate>
<d:ItemCount m:type="Edm.Int32">0</d:ItemCount>
<d:LastItemDeletedDate m:type="Edm.DateTime">2012-06-26T23:15:58Z</d:LastItemDeletedDate>
<d:LastItemModifiedDate m:type="Edm.DateTime">2012-06-26T23:15:59Z</d:LastItemModifiedDate>
<d:ListItemEntityTypeFullName>SP.Data.ProjectPolicyItemListItem</d:ListItemEntityTypeFullName>
<d:MultipleDataList m:type="Edm.Boolean">false</d:MultipleDataList>
<d:NoCrawl m:type="Edm.Boolean">true</d:NoCrawl>
<d:ParentWebUrl>/</d:ParentWebUrl>
<d:ServerTemplateCanCreateFolders m:type="Edm.Boolean">true</d:ServerTemplateCanCreateFolders>
<d:TemplateFeatureId m:type="Edm.Guid">00bfea71-de22-43b2-a848-c05709900100</d:TemplateFeatureId>
<d:Title>Project Policy Item List</d:Title>
</m:properties>
</content>
注意ListItemEntityTypeFullName這個屬性,它重要的原因是當你需要建立或更新列表項時,必須傳遞該引數作為後設資料metadata中的type屬性。使用REST操作列表
建立一個列表的請求如下所示。
url: http://site url/_api/web/lists
method: POST
body: { '__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true, 'BaseTemplate': 100,
'ContentTypesEnabled': true, 'Description': 'My list description', 'Title': 'Test' }
Headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
accept: "application/json;odata=verbose"
content-type: "application/json;odata=verbose"
content-length:length of post body
下面的程式碼演示瞭如何通過MERGE方法更新列表。url: http://site url/_api/web/lists(guid'list GUID')
method: POST
body: { '__metadata': { 'type': 'SP.List' }, 'Title': 'New title' }
Headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
IF-MATCH": etag or "*"
X-HTTP-Method: MERGE,
accept: "application/json;odata=verbose"
content-type: "application/json;odata=verbose"
content-length:length of post body
接下來的程式碼演示瞭如何向一個列表中新增自定義欄位。Url: url: http://site url/_api/web/lists(guid'list GUID')/Fields
Method:POST
Body: { '__metadata': { 'type': 'SP.Field' }, 'Title': 'field title', 'FieldTypeKind': FieldType value,'Required': 'true/false', 'EnforceUniqueValues': 'true/false','StaticName': 'field name'}
Headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
content-type: "application/json;odata=verbose"
content-length:length of post body
接下來是如何刪除一個列表。url: http://site url/_api/web/lists(guid'list GUID')
method: POST
Headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
IF-MATCH: etag or "*"
X-HTTP-Method: DELETE
使用REST操作列表項下面的示例展示瞭如何獲取列表中所有的列表項。
url: http://site url/_api/web/lists/GetByTitle('Test')/items
method: GET
headers:
Authorization: "Bearer " + accessToken
accept: "application/json;odata=verbose" or "application/atom+xml"
注意在查詢列表項時,$skip需要改用$skiptoken。下面的示例展示瞭如何通過列表項ID獲取指定的列表項。
url: http://site url/_api/web/lists/GetByTitle('Test')/items(item id)
method: GET
headers:
Authorization: "Bearer " + accessToken
accept: "application/json;odata=verbose" or "application/atom+xml"
下面的XML是一個請求返回的示例。<content type="application/xml">
<m:properties>
<d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
<d:Id m:type="Edm.Int32">1</d:Id>
<d:ID m:type="Edm.Int32">1</d:ID>
<d:ContentTypeId>0x010049564F321A0F0543BA8C6303316C8C0F</d:ContentTypeId>
<d:Title>an item</d:Title>
<d:Modified m:type="Edm.DateTime">2012-07-24T22:47:26Z</d:Modified>
<d:Created m:type="Edm.DateTime">2012-07-24T22:47:26Z</d:Created>
<d:AuthorId m:type="Edm.Int32">11</d:AuthorId>
<d:EditorId m:type="Edm.Int32">11</d:EditorId>
<d:OData__UIVersionString>1.0</d:OData__UIVersionString>
<d:Attachments m:type="Edm.Boolean">false</d:Attachments>
<d:GUID m:type="Edm.Guid">eb6850c5-9a30-4636-b282-234eda8b1057</d:GUID>
</m:properties>
</content>
下面的程式碼演示如何建立一個列表項。url: http://site url/_api/web/lists/GetByTitle('Test')/items
method: POST
body: { '__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'Test'}
headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
accept: "application/json;odata=verbose"
content-type: "application/json;odata=verbose"
content-length:length of post body
注意這裡你需要知道上面提到的ListItemEntityTypeFullName值並把它作為type引數傳遞。下面的程式碼展示瞭如何更新一個列表項。
url: http://site url/_api/web/lists/GetByTitle('Test')/items(item id)
method: POST
body: { '__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'TestUpdated'}
headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
"IF-MATCH": etag or "*"
"X-HTTP-Method":"MERGE",
accept: "application/json;odata=verbose"
content-type: "application/json;odata=verbose"
content-length:length of post body
接下來是刪除列表項。url: http://site url/_api/web/lists/GetByTitle('Test')/items(item id)
method: POST
headers:
Authorization: "Bearer " + accessToken
X-RequestDigest: form digest value
"IF-MATCH": etag or "*"
"X-HTTP-Method":"DELETE"
另外說明一點,在用REST操作列表項時,如何需要考慮版本,可以使用ETag屬性,具體可以到必應搜尋瞭解一下,這裡不做過多闡述了。相關文章
- 利用SSIS進行SharePoint 列表資料的ETL
- HTML <datalist> 選項列表HTML
- HTML datalist 選項列表HTML
- vue 列表渲染例項Vue
- CSS 列表項佈局技巧CSS
- Python中基於匹配項的子列表列表串聯Python
- Python基礎-列表操作(2):列表的遍歷和數字列表Python
- PbootCMS內容和列表頁呼叫tags列表boot
- 【Azure 環境】用 PowerShell 呼叫 AAD Token, 以及呼叫Azure REST API(如資源組列表)RESTAPI
- React-列表元件(通知列表、私信列表、虛擬列表)React元件
- 列表和元組
- 【Redis系列3】Redis列表物件之linkedlist(雙端列表)和ziplist(壓縮列表)及quicklick(快速列表)實現原理分析Redis物件UI
- python元組和列表Python
- 列表和字典的操作
- Nacos - 客戶端例項列表獲取客戶端
- 列表
- CSS 背景樣式和列表CSS
- 列表和元組的方法
- list列表運算子,列表元素的遍歷,列表的方法,生成列表,巢狀的列表|python自學筆記(四)巢狀Python筆記
- laravel-admin 列表展示外部api的資料LaravelAPI
- rest apiRESTAPI
- HTML5中有序列表和無序列表的寫法HTML
- 28.qt quick-ListView高仿微信好友列表和聊天列表QTUIView
- 【Qt6】列表模型——樹形列表QT模型
- 字串形式的列表,字典轉列表,字典字串
- C++--Win32--列表編輯--獲取列表內容--獲取列表行數--修改列表內容C++Win32
- 拼多多API:拼多多根據關鍵詞取商品列表 APIAPI
- 03-python函式和列表Python函式
- python 字典和列表巢狀用法Python巢狀
- python學習-數字和列表Python
- 商品列表
- python列表Python
- 二、列表
- 使用Jquery和JSON的州和城市列表jQueryJSON
- Nacos - 服務端處理例項列表請求服務端
- GraphQL API vs REST APIAPIREST
- 淘寶/天貓API分享:搜尋店鋪列表 API介面呼叫示例API
- Python資料型別(數字,字串,[列表],(元組),{字典:字典值},{列表,列表2})Python資料型別字串
- Python元組、列表、集合及列表去重操作Python