In Focus

SharePoint Online - Delete Custom Action Via Rest API

In this article, we will learn how to delete custom action via REST API.

In this article, we will learn how to delete SharePoint custom action via the REST API. While working on this solution, I had a requirement to delete custom action with a specific title in SPFx web part via REST API. 
 
Notes
  • We will first query the user custom actions via REST API.
  • Loop through each custom action and match the custom action title with a title which we wanted to delete.
  • Use REST API endpoint to delete based on Custom action ID.
API Reference
  • Get User Custom Actions -  /_api/site/UserCustomActions
  • Delete User Custom Action -  /_api/site/UserCustomActions(@v0)/deleteObject()?@v0=guid'abcd-efgh-ijkl-mnop'
Assuming we wanted to delete Custom Action with title 'SettingHeaderFooterwithAppCustomizer', the below method can be used.
 
Below is reference screenshot which returns custom action with title 'SettingHeaderFooterwithAppCustomizer' on My Site collection 
Get UserCustomActions API call using SPInsider chrome extension.
 
SharePoint Online - Delete Custom Action Via Rest API
  1. protected removeCustomAction() {  
  2.  try {  
  3.    var title = 'SettingHeaderFooterwithAppCustomizer';    
  4.      this._getdigest()  
  5.        .then((digrestJson) => {  
  6.          console.log(digrestJson);  
  7.          const digest = digrestJson.FormDigestValue;  
  8.          const headers = {  
  9.              'X-RequestDigest': digest,  
  10.              "content-type""application/json;odata=verbose",  
  11.          };  
  12.          const spOpts: ISPHttpClientOptions = {  
  13.          };  
  14.          this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + `/_api/site/UserCustomActions`, SPHttpClient.configurations.v1,spOpts)  
  15.          .then((response: SPHttpClientResponse) => {  
  16.              
  17.             response.json().then((responseJSON: any) => {   
  18.              console.log(responseJSON);   
  19.   
  20.              responseJSON.value.forEach(element => {  
  21.              //Match custom action title  
  22.                  if(element.Title == title)  
  23.                  {  
  24.                     
  25.                  //found custom action, call REST API to delete object  
  26.                  this.context.spHttpClient.post(this.context.pageContext.web.absoluteUrl + "/_api/site/UserCustomActions(@v0)/deleteObject()?@v0=guid'" + element.Id + "'", SPHttpClient.configurations.v1,spOpts)  
  27.                    .then((response: SPHttpClientResponse) => {  
  28.                      console.log("I think I just deleted a custom action via REST API---");  
  29.                    });  
  30.                  }  
  31.              });  
  32.            });   
  33.           });  
  34.      });  
  35.  } catch (error) {  
  36.      console.error(error);  
  37.  }  
Get digest method to retreive the digest value.
  1. private _getdigest(): Promise<any> {
  2.  const spOpts: ISPHttpClientOptions = {    
  3.  };    
  4.  return this.context.spHttpClient.post(this.context.pageContext.web.absoluteUrl + `/_api/contextinfo`, SPHttpClient.configurations.v1,spOpts)    
  5.    .then((response: SPHttpClientResponse) => {    
  6.      return response.json();    
  7.    });    
  8. }    
This can be done vis JSOM also but I could not find any article on removing via REST API, hence I  thought of sharing.
 
Execute above function, and it will delete custom action. Below is screenshot of Get API post running the function.
 
SharePoint Online - Delete Custom Action Via Rest API
 
Hope this helps...happy coding!!!