Identify Tree View is Enabled for the SharePoint Site Using REST API

In this blog, we’ll learn how to identify whether the tree view is enabled or not in the SharePoint site using REST API

Syntax
 
REST API Endpoint:

https://SharePointSiteURL/_api/web?$select=TreeViewEnabled
 
REST API endpoint to use in Add_ins:

<appweburl>/_api/SP.AppContextSite(@target)/web?$select= TreeViewEnabled&@target=<hostweburl>
 
Embed Code Snippet:

The following code snippet can be embed in SharePoint page or in content editor web part as a script. This example is used to check whether the tree view is enabled for the SharePoint website.
  1. <script type="text/javascript" src="/SiteAssets/Scripts/jquery.min.js"></script>  
  2.     
  3. <script type="text/javascript">    
  4. $.ajax({    
  5.    url: _spPageContextInfo.webAbsoluteUrl+"/_api/web?$select=treeviewenabled"//THE ENDPOINT    
  6.    method: "GET",    
  7.    headers: { "Accept""application/json; odata=verbose" },    
  8.    success: function (data) {   
  9.     //RESULTS HERE!!  
  10.     console.log(data.d.TreeViewEnabled)      
  11.     if(data.d.TreeViewEnabled)  
  12.       alert('Tree view enabled on this site.');      
  13.     else  
  14.       alert('Tree view disabled on this site.');               
  15.    }    
  16. });  
  17. </script>    
Add-in Code Snippet

The following code snippet is used in SharePoint Add-in to get the tree view enabled property of a SharePoint web. 
  1. // Load the js files and continue to the successHandler  
  2. $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);  

  3.   
  4. // Function to prepare and issue the request to get  
  5. //  SharePoint data  
  6. function execCrossDomainRequest() {  
  7.       
  8.     // Initialize the RequestExecutor with the add-in web URL.  
  9.     var executor = new SP.RequestExecutor(appweburl);  
  10.   
  11.     // Issue the call against the add-in web.  
  12.     // To get the TreeViewEnabled property using REST we can hit the endpoint:  
  13.     //      appweburl/_api/web?select=TreeViewEnabled&@target=hostweburl  
  14.     // The response formats the data in the JSON format.      
  15.     executor.executeAsync(  
  16.         {  
  17.             url: appweburl + "/_api/SP.AppContextSite(@target)/web?$select=TreeViewEnabled&@target='" + hostweburl + "'",  
  18.             method: "GET",  
  19.             headers: {  
  20.                 "Accept""application/json; odata=verbose"  
  21.             },  
  22.             success: successHandler,  
  23.             error: errorHandler  
  24.         }  
  25.     );  
  26. }  
  27.   
  28. // Function to handle the success event.       
  29. function successHandler(data) {  
  30.     var jsonObject = JSON.parse(data.body)    
  31.     //jsonObject.d.TreeViewEnabled returns true if enabled otherwise it returns false     
  32.     console.log('Tree view enabled on this site: ' + jsonObject.d.TreeViewEnabled);    
  33.     if(jsonObject.d.TreeViewEnabled)  
  34.     $('#message').html(‘Tree view enabled on this site.');      
  35.     else  
  36.     $('#message').html(‘Tree view disabled on this site.');  
  37.     
  38. }    
  39.     
  40. function errorHandler(data, errorCode, errorMessage) {    
  41.     console.log("Could not complete cross-domain call: " + errorMessage);    
  42. }   
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now