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