1

This is the code that get me and display all my subsite. The problem is I need Display all subsites instead of one that I need to hide "Test3" using javascript.

<html>
<script>
ExecuteOrDelayUntilScriptLoaded(RetriveSites,"sp.js");

function RetriveSites()
{
    currentcontext = new SP.ClientContext.get_current();
    currentweb = currentcontext.get_web();
    //this.subsites = currentweb.get_webs();
    this.subsites = currentweb.getSubwebsForCurrentUser(null);
    currentcontext.load(this.subsites);
    currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
    Function.createDelegate(this, this.ExecuteOnFailure));
}
function ExecuteOnSuccess(sender, args) {
    var subsites = '';
    var enum1 = this.subsites.getEnumerator();
    var el = document.getElementById('LKMSubWebs');
    while (enum1.moveNext())
    {
        var Site = enum1.get_current();
        //alert('hi123');
        subsites += '\n' + '<a class=subsitecls href='+ Site.get_serverRelativeUrl()+'>'+Site.get_title()+'</a> </br>'
        //RootElement.append('<a class=subsitecls href='+ siteTitle+'>'+siteTitle+'</a> </br>');
        //alert(subsites);
    }
    el.innerHTML = subsites;
}
function ExecuteOnFailure(sender, args) {
    alert("error");
    //alert(args.get_message());
}
</script>

<div id="LKMSubWebs"></div>
</html>

thank you


enter image description here

when I Edit the page enter image description here When I save it That is very strange

3

Within your enumeration, check for site's title and if it matches 'Test3' then use continue keyword to skip.

while (enum1.moveNext()) {
 var Site = enum1.get_current();
 var obj = {
    title: Site.get_title(),
    url: Site.get_serverRelativeUrl()
 };

 if (obj.title == 'Test3')
    continue;

 subsites += '\n' + '<a class="subsitecls" href=' + obj.url + '>' + obj.title + '</a> </br>'
}

FYI: To get sub-sub sites (multi-level deep), you'd need to use recursive function.

  • Thank you Aveenav. It's works but when I save the page, the subsite name "Test3" is still there. Do you have any idea? – Raise Sep 7 '17 at 15:03
  • can you post a screenshot of your rendered html? – Aveenav Sep 7 '17 at 15:17
1

This is the final code. I'm using a small Css code for the style.

      <script>
        ExecuteOrDelayUntilScriptLoaded(RetriveSites, "sp.js");

        function RetriveSites() {
            currentcontext = new SP.ClientContext.get_current();
            currentweb = currentcontext.get_web();
            //this.subsites = currentweb.get_webs();
            this.subsites = currentweb.getSubwebsForCurrentUser(null);
            currentcontext.load(this.subsites);
            currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
                Function.createDelegate(this, this.ExecuteOnFailure));
        }
        function ExecuteOnSuccess(sender, args) {
            var subsites = '';
            var enum1 = this.subsites.getEnumerator();
            var el = document.getElementById('LKMSubWebs');
            while (enum1.moveNext()) {
                var Site = enum1.get_current();
                 var obj = {  title: Site.get_title(),
                   url: Site.get_serverRelativeUrl()};
     if (obj.title == 'Test3' )
    continue;
    if (obj.title=='Test1')
   continue;
   //alert('hi123');

subsites += '\n' + '<div class="alpha"> <a class=subsitecls            href=' + Site.get_serverRelativeUrl() + '>' + Site.get_title() + '</a>     </br></div>'

                //RootElement.append('<a class=subsitecls href='+    siteTitle+'>'+siteTitle+'</a> </br>');
                //alert(subsites);
            }
            el.innerHTML = subsites;

            if (Site.get_title() = "Test3")
            {
                document.getElementByName("Test3").style.display = 'none';
            }

        }

        function ExecuteOnFailure(sender, args) {
            alert("error");
            //alert(args.get_message());
        }
    </script>
    <div id="LKMSubWebs"></div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.