SharePoint REST API get list item by id

Usually SharePoint lists are updated like so:

#Get the List $List = $Ctx.Web.Lists.GetByTitle[$ListName] $Ctx.Load[$List] $Ctx.ExecuteQuery[] #Get All List items $ListItemsCAML = New-Object Microsoft.SharePoint.Client.CamlQuery $ListItemsCAML.ViewXml = "" $ListItems = $List.GetItems[$ListItemsCAML] $Ctx.Load[$ListItems] $Ctx.ExecuteQuery[] Foreach [$ListItem in $ListItems] { #Set New value for List column $ListItem["Reference"] = $ListItem["ID"] $ListItem.Update[] $Ctx.ExecuteQuery[] }

I am trying to update SharePoint list with values from JSON files, therefore I am looping through them, and not through ListItems variable [ListItemCollection]. So in case there is no record in the SP list for new JSON, add one. For that, I'd like to skip one loop, and update $ListItems directly:

ForEach [$json in $json_files] { $jsonSONitem = Get-Content $json.FullName | ConvertFrom-Json # somehow get ID of list item from $ListItems # $ID = $ListItems.FieldValues.Where{$_.Name -eq $json.Name}.ID $ListItems[$ID].FieldValues.Name="JSONitem.Name } $ListItem$.Update[] $Ctx.ExecuteQuery[]

However, I am struggling to get ID for child objects of $ListItems. Or maybe I am just trying to use CSOM in a wrong way and I have to loop 'ForEach [$ListItem in $ListItems]' no matter what ?

Blog address: //blog.csdn.net/FoxDave

This article focuses on how to use SharePoint REST to manipulate lists and list items. Read this article first to understand the introduction and basic operation of REST described earlier.

No more nonsense, let's start now.

Getting list attributes with REST

When you know the GUID of a list, you can use the following request to get the list object.

url: //site url/_api/web/lists[guid'list GUID'], method: GET Headers: Authorization: "Bearer " + accessToken accept: "application/json;odata=verbose" or "application/atom+xml"
If you know the title of a list, you can use the following request.
url: //site url/_api/web/lists/GetByTitle['Test'] method: GET Headers: Authorization: "Bearer " + accessToken accept: "application/json;odata=verbose" or "application/atom+xml"
The following is an example of the XML result returned by the request, which basically corresponds to the attributes of the API, without much explanation.
true 100 0 false 2012-06-26T23:15:58Z 00000000-0000-0000-0000-000000000000 A list created by Project Based Retention used to store Project Policy Items. none 0 true false false false false ProjectPolicyItemList false false true 74de3ff3-029c-42f9-bd2a-1e9463def69d /_layouts/15/images/itgen.gif false false false false false false 0 2012-06-26T23:15:58Z 2012-06-26T23:15:59Z SP.Data.ProjectPolicyItemListItem false true / true 00bfea71-de22-43b2-a848-c05709900100 Project Policy Item List
Note the property ListItemEntityTypeFullName, which is important because when you need to create or update a list item, you must pass this parameter as a type attribute in metadata.

Use REST operation list

The request to create a list is shown below.

url: //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
The following code demonstrates how to update the list through the MERGE method.
url: //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
The next code demonstrates how to add custom fields to a list.
Url: url: //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
Next is how to delete a list.
url: //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
Use REST to manipulate list itemsThe following example shows how to get all the list items in the list.
url: //site url/_api/web/lists/GetByTitle['Test']/items method: GET headers: Authorization: "Bearer " + accessToken accept: "application/json;odata=verbose" or "application/atom+xml"
Note that when querying list items, $skip needs to change to $skiptoken.

The following example shows how to get the specified list item through the list item ID.

url: //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"
The following XML is an example of a request returned.
0 1 1 0x010049564F321A0F0543BA8C6303316C8C0F an item 2012-07-24T22:47:26Z 2012-07-24T22:47:26Z 11 11 1.0 false eb6850c5-9a30-4636-b282-234eda8b1057
The following code demonstrates how to create a list item.
url: //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
Note here that you need to know the ListItemEntityTypeFullName value mentioned above and pass it as a type parameter.The following code shows how to update a list item.
url: //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
Next, delete the list item.
url: //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"
In addition, when using REST to manipulate list items, how to consider the version, you can use the ETag attribute, specifically you can go to Bing search to find out, here do not elaborate too much.

Added by bob_dole on Sat, 01 Jun 2019 02:37:33 +0300

Video liên quan

Chủ Đề