0

enter image description here This is what I have so far to basically get all of the subsites but I cannot find a way to link each subsite to it's own subsite url.

  • try it as subsiteValue += "<span>" + site.Title + "</span><a href=" + site.ServerRelativeUrl + "></a><br/>"; – Gautam Sheth Sep 13 '18 at 17:58
  • Tried it but no luck. But I was able to get the full url this time. I am trying to now link create a "a href" link around it. subsiteValue += site.Title + "</br>" + "mysite"+ site.ServerRelativeUrl + "<br/>"; – user68133 Sep 13 '18 at 18:42
  • mysite is just the intranet site link such as mysite.com – user68133 Sep 13 '18 at 18:43
2

As per my understanding you need a URL of all Sub Sites. may be this will be helpful.

$.ajax({
    url:_spPageContextInfo.webAbsoluteUrl + "/_api/web/GetSubwebsFilteredForCurrentUser(nWebTemplateFilter=-1)?filter=WebTemplate",
        method:"GET", headers:{
        "Accept":"application/json;odata=verbose"
    },
    success:function(data){
        if(data.d.results.length>0)
        {
            var subSites = data.d.results;

            var subisteurlArray=$.map(subSites,function(o){return window.location.origin + o.ServerRelativeUrl})
            console.log(subisteurlArray);
            var subsiteValue = "<h2> Subsites </h2><br/>";
            $.each(subisteurlArray,function(i,o){
                subsiteValue+= o + " <br/>";
            });
            if($('#outputInfo').length>0)
                $('#outputInfo').html(subsiteValue);
            console.log(subsiteValue);
        }

        console.log(subSites);
    },
    error:function(data){
        console.log(data);
    }
});
1

Check below code snippet. It lists all the subsites and links each subsite to its own url:

<div id="outputInfo"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>​​<script>
$(document).ready(function(){

    $.ajax({
        url:_spPageContextInfo.webAbsoluteUrl + "/_api/web/GetSubwebsFilteredForCurrentUser(nWebTemplateFilter=-1)?filter=WebTemplate",
            method:"GET", headers:{
            "Accept":"application/json;odata=verbose"
        },
        success:function(data){
            var subSites = data.d.results;
            console.log(subSites);
            var subsiteValue = "<h2> Subsites</h2><br/>";
            subSites.forEach(function(site){
                subsiteValue += "<a target='_blank' href=" + site.ServerRelativeUrl + ">" + site.Title + "</a><br/>";
            });
            $('#outputInfo').html(subsiteValue);
        },
        error:function(data){
            console.log(data);
        }
    });

});
</script>
  • Thanks! Exactly what I was looking for. I had a hard time generating the link but yeah this is it! – user68133 Sep 14 '18 at 17:47
  • Is there a way to only display these subsites based on user access? I thought GetSubwebsFilteredForCurrentUser would display only subsites based on if a user has access to that subsite. – user68133 Sep 19 '18 at 20:39

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.