Toggle list visibility (hide from all site content)

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);
    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

5 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. 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.