I need to update a filed in the Site pages library using rest API. I tried with axios and spHttpClient & spHttpResponse, but it's not working. Is there any sharepoint rest API for updating an item in a site pages library?
1 Answer
If you are "updating a file" (overwriting the existing file in an item), use:
POST https://{site_url}/_api/web/GetFileByServerRelativeUrl('/Folder Name/{file_name})/$value
Authorization: "Bearer " + accessToken
Content-Length: {length of request body as integer}
X-HTTP-Method: "PUT"
X-RequestDigest: "{form_digest_value}"
"Contents of file"
See Working with folders and files with REST
If you are updating the metadata of that doc lib item (and not doing anything to the file of the item), use:
POST https://{site_url}/_api/web/lists/GetByTitle('Test')/items({item_id})
Authorization: "Bearer " + accessToken
Accept: "application/json;odata=verbose"
Content-Type: "application/json"
Content-Length: {length of request body as integer}
If-Match: "{etag or *}"
X-HTTP-Method: "MERGE"
X-RequestDigest: "{form_digest_value}"
{
"__metadata": {
"type": "SP.Data.TestListItem"
},
"Title": "TestUpdated"
}
See Working with lists and list items with REST
Note that "Contents of file" in the request body should be the file in a form readable by the SP server. Example JavaScript code using the FileReader API is here. For updating list/lib column values (metadata), the example above used the field name "Title" (internal name property) with value set as "TestUpdated". Add more fields as valid JSON and note the headers used in the request as well as ListItemEntityTypeFullName required in the valid JSON request body. I developed a primitive form to test REST requests which is like Postman, but the simplest thing for you to do is use Postman to test your REST requests.