What is the payload to be sent for “ViewFields” parameter as part of consuming the SPO REST API?
-
18-02-2021 - |
Question
I am trying to create a SharePoint list view through the SharePoint REST API, with a defined set of columns to be part of the view. The endpoint i am using is below:
POSTMAN API Request:
HTTP METHOD: POST
URL: https://tenantname.sharepoint.com/sites/SPSite/_api/web/lists/getbytitle('ListName')/views Headers:
'Accept' - 'application/json;odata=verbose' 'Content-Type' - 'application/json;odata=verbose' Body (JSON):
{
"__metadata":{
"type":"SP.View"
},
"Title":"TestViewTHI",
"ViewQuery":["Name","Title"]
}
I get a JSON error, since this payload does not seem to be right. Need help in understanding how to create a view with specific fields through the SharePoint REST API.
Thanks, Yesh
Solution
In the body you should pass CAML Query for attribute "ViewQuery"
. Reference:https://www.codesharepoint.com/rest-api/create-view-in-sharepoint-using-rest-api
If you don't want to add filter/order in the view. You could just ignore it. For example:
{
"__metadata":{
"type":"SP.View"
},
"Title":"TestView",
"PersonalView": false,
"ViewType": "HTML"
}
After you create the view, you could add/remove fields with the endpoint:
https://tenant.sharepoint.com/sites/michael/_api/Web/Lists/getbytitle('TestList')/Views/getbytitle('TestView')/ViewFields/addviewfield('FieldName')
https://tenant.sharepoint.com/sites/michael/_api/Web/Lists/getbytitle('TestList')/Views/getbytitle('TestView')/ViewFields/RemoveViewField('FieldName')
Read here for more: https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-rest-reference/dn531433(v=office.15)#viewfieldcollection-resource