Toggle list visibility (hide from all site content)

Updated August 05, 2019 to fix an error when used on the root site and the BaseUrl field was set to /. To load the lists on the root site you must leave the field empty.

Here is a tool to toggle the visibility of all custom lists (BaseTemplate 100) and document libraries (BaseTemplate 101) – and also the Site Pages library (BaseTemplate 119). It is used to toggle the Hidden property on the list – which determines whether or not to show the list in Site Contents.

Please note that you must have the necessary rights (manage lists) to use it, and if you don’t you will get an error message. Also note that this snippet loads jQuery from code.jquery.com – if your site does not have Internet access you must change this script tag to refer a local copy of jQuery.

Add this snippet to a Script Editor Web Part in a Classic Web Part Page to use it.

<div class="pageHeader">BaseUrl to the site</div>
<input type="text" style="width:200px;" id="baseUrlInput" onkeydown="if(event.keyCode === 13){getAllLists()}"><input
    type="button" onclick="getAllLists()" value="Load all lists" />
<div id="allListsTable"></div>
<style type="text/css">
    .pageHeader {
        font-size: 1.5em;
        font-weight: 300;
    }

    .headerRow td {
        font-size: 1.5em;
        border-bottom: 1px silver solid;
    }

    .bodyRow td {
        padding: 3px 5px;
    }

    .bodyRow:hover td {
        background-color: #00ad56;
        color: #ffffff;
    }
</style>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script type="text/javascript">
    jQuery("#baseUrlInput").val(_spPageContextInfo.webServerRelativeUrl !== "/" ? _spPageContextInfo.webServerRelativeUrl : "");
    function getAllLists() {
        var baseUrl = jQuery("#baseUrlInput").val();
        jQuery.ajax({
            "url": baseUrl + "/_api/web/lists?$select=Id,Title,BaseTemplate,RootFolder,Hidden&$expand=RootFolder",
            "method": "GET",
            "headers": {
                "accept": "application/json; odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": getFormDigest()
            },
            "success": function (data) {
                var arr = [], b = [];
                jQuery.each(data.d.results, function (i, list) {
                    if (list.BaseTemplate !== 100 && list.BaseTemplate !== 101 && list.BaseTemplate !== 119) {
                        return;
                    }
                    arr.push({
                        "title": list.Title,
                        "rootfolder": list.RootFolder !== undefined ? list.RootFolder.Name : "",
                        "id": list.Id,
                        "hidden": list.Hidden
                    });
                });
                arr.sort(function (a, b) {
                    if (a.title < b.title) {
                        return -1;
                    }
                    if (a.title > b.title) {
                        return 1;
                    }
                    return 0;
                });
                b.push("<table>");
                b.push("<tr class='headerRow'>");
                b.push("<td>");
                b.push("Title");
                b.push("</td>");
                b.push("<td>");
                b.push("URL-name");
                b.push("</td>");
                b.push("<td>");
                b.push("Hidden");
                b.push("</td>");
                b.push("</tr>");
                jQuery.each(arr, function (i, list) {
                    if (list.rootfolder === "") {
                        return;
                    }
                    b.push("<tr class='bodyRow'>");
                    b.push("<td>");
                    b.push(list.title);
                    b.push("</td>");
                    b.push("<td>");
                    b.push(list.rootfolder);
                    b.push("</td>");
                    b.push("<td>");
                    b.push("<input listguid='" + list.id + "' type='checkbox'" + (list.hidden ? " checked='checked'" : "") + " onclick='toggleVisible(this);'/>");
                    b.push("</td>");
                    b.push("</tr>");
                });
                b.push("</table>");
                jQuery("#allListsTable").html(b.join(""));
            },
            "error": function (err) {
                jQuery("#allListsTable").html(err.statusText + ": " + err.status);
            }
        });
    }

    function toggleVisible(elm) {
        var baseUrl = jQuery("#baseUrlInput").val();
        var hidden = jQuery(elm).prop("checked");
        var guid = jQuery(elm).attr("listguid");
        jQuery("#status_" + guid).remove();
        var itemPayload = {
            "__metadata": { "type": "SP.List" },
            "Hidden": hidden,
        };
        jQuery.ajax({
            "url": baseUrl + "/_api/Web/Lists/GetById('" + guid + "')",
            "type": "POST",
            "data": JSON.stringify(itemPayload),
            "headers": {
                "accept": "application/json; odata=verbose",
                "content-type": "application/json;odata=verbose",
                "IF-MATCH": "*",
                "X-HTTP-Method": "MERGE",
                "X-RequestDigest": getFormDigest()
            },
            success: function (data) {
                // Success
                jQuery(elm).after("<span id='status_" + guid + "' style='margin-left:4px;'>OK</span>");
            },
            error: function (err) {
                jQuery(elm).after("<span id='status_" + guid + "' style='margin-left:4px;color:red;'>Error! - Open the console for details (F12 > Console).</span>");
                console.log(err);
            }
        });
    }

    function getFormDigest() {
        var r = _spPageContextInfo.formDigestValue;
        if (r === undefined) {
            r = jQuery("#__REQUESTDIGEST").val();
        }
        return r;
    }
</script> 

Hope you find this tool useful.

Alexander

9 thoughts on “Toggle list visibility (hide from all site content)”

  1. This is great Alexander.

    I wish I knew the reasons behind the reasons why Microsoft did not add this option by default.

    I’ve just tried it and I am getting error. I am site admin so I assumed I have the “necessary rights (manage lists)”.

    Is it something I need to enable somewhere?

      1. Hi Alexander,
        sorry for the delay in replying. I tried the updated version and it works well now.

        Also, I noticed that when you click on Site Settings whether you are in Classic or New experience, the Site Contents loads in New Experience mode, but your code works with that too, which is great.

        I was getting worried since Microsoft is trying to force users to use the New Experience.

        As usual, many thanks for your great work. It helps our organisation a lot keeping lists private.

      2. I just realised that when I ticked all the lists, they were also removed from my quick menu on the left.
        I had to restore it from memory, including the permissions πŸ™

      3. I don’t think I’ll be adding a select all because you should have fine control of which lists you hide or show.

        Regarding the removal of the lists from the left menu: If you used the default “Show in quick launch” it might be removed when you choose to hide it from the browser (I guess this is by default – is it hidden you should not have a link to it in the quick launch).

        I don’t understand that it could mess with your list permissions though – did you have a broken permission inheritance that was reset to inherit permissions?

        Alexander

    1. You can easily let it toggle all types of lists by removing this snippet:

      if (list.BaseTemplate !== 100 && list.BaseTemplate !== 101 && list.BaseTemplate !== 119) {
        return;
      }

      This will however let you show some system lists that are not supposed to be visible so alternatively you can add the list types you want to control by BaseTemplate ID – look here for a list of different IDs: https://docs.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.splisttemplatetype?view=sharepoint-server

      Alexander

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.