SharePoint 2013 Online Changes The View programatically Using REST API

This blog helps you to change the view in SharePoint 2013, using REST API. The business process is evolving and the users want to see the additional fields in the default view. I was able to add the necessary columns via content type. Now, I need to change the default view (all have the same name) to assist the users in sorting and filtering the files.
  1. #######################################  
  2. # Change the View Using REST API in SharePoint 2013  
  3. # Applies To: SharePoint 2013,JSOM,List,Office 365  
  4. # Author: Gowtham Rajamanickam  
  5. # Date:18-March-2017  
  6. #Version:1.0   
  7.  
  8. ########################################  
  9. var newView='CustomView';  
  10. var ListName='TutorialList';  
  11. var defaultView='All Items';  
  12.   
  13. (function () {  
  14.     var payload = "{'__metadata': {'type': 'SP.View'}, 'Title': newView}";  
  15.     var xRequestDigest = $("#__REQUESTDIGEST").val();  
  16.     console.log(payload);  
  17.     console.log(xRequestDigest);  
  18.     $.ajax({  
  19.         url: "WEBSITE-URL/_api/web/lists/getbytitle(ListName)/views/getbytitle(defaultView)",  
  20.         method: "POST",  
  21.         data: payload,  
  22.         headers: {  
  23.             "content-type""application/json;odata=verbose",  
  24.             "X-Requestdigest": xRequestDigest,  
  25.             "accept""application/json; odata=verbose",  
  26.             "X-HTTP-Method""MERGE"  
  27.         },  
  28.        success: printInfo,  
  29.        error: logError  
  30.     });  
  31. })()  
  32.   
  33. function printInfo() {  
  34.     console.log('View Updated');  
  35. }  
  36.   
  37. function logError(error) {  
  38.     console.log(JSON.stringify(error));  
  39. }   
  40.    
Execute this code and check your SharePoint site and columns, which are removed successfully.

Was my blog helpful?

If so, please let us know at the bottom of this page. If not, let us know what was confusing or missing and I’ll use your feedback to double-check the facts, add info and update this blog.