0

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

1

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

3
  • - Thank you for the response. So, if i have a collection of 5 fields to be added to the view, does that mean i have to iterate through the collection and add one field at a time to the view? – Yeshwanth J Nov 25 '20 at 16:36
  • Yes, you are right. – Michael Han_MSFT Nov 26 '20 at 2:45
  • Got it! Thank you for clarifying this. This helps. – Yeshwanth J Nov 30 '20 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.