I have a list view in which I need to change the column order every quarter of the year. Is there a method using rest API to set all my visible columns to a specific order, and then add a new one and drop an old one each quarter?
1 Answer
Yes, you can edit your view to add, move or remove fields with SharePoint REST API. It's working for both on-premise, including SharePoint Server 2013 and later, and SharePoint Online in Microsoft 365.
Sample of requests:
Get current view fields (GET)
GET https://your-webapp/sites/your-site/_api/web/lists/getByTitle('YourList')/views/getByTitle('All%20Items')/viewFields
Add a field from a view (POST)
POST https://your-webapp/sites/your-site/_api/web/lists/getByTitle('YourList')/views/getByTitle('All%20Items')/viewFields/addViewField
'Content-Type': 'application/json;odata=verbose'
'Accept': 'application/json;odata=verbose'
{ "strField": "Status" }
Remove a field from a view (POST)
POST https://your-webapp/sites/your-site/_api/web/lists/getByTitle('YourList')/views/getByTitle('All%20Items')/viewFields/removeViewField
'Content-Type': 'application/json;odata=verbose'
'Accept': 'application/json;odata=verbose'
{ "strField": "Status" }
Remove all fields from a view (POST)
POST https://your-webapp/sites/your-site/_api/web/lists/getByTitle('YourList')/views/getByTitle('All%20Items')/viewFields/removeAllViewFields
'Content-Type': 'application/json;odata=verbose'
'Accept': 'application/json;odata=verbose'
Change field order from a view (POST)
POST https://your-webapp/sites/your-site/_api/web/lists/getByTitle('YourList')/views/getByTitle('All%20Items')/viewFields/moveViewFieldTo
'Content-Type': 'application/json;odata=verbose'
'Accept': 'application/json;odata=verbose'
{ "field": "Status", "index": 2 }
Screenshots (with SharePoint Online & Power Automate):
Sources:
-
1
-
Sorry. Correction. Can you tell me what the headers are for this Post?– ChasEpesCommented Dec 6, 2023 at 19:02
-
1