I need to iterate through a site to list all it's top level sites then each sites subsite, then each subsites subsite, etc, until I end up with a tree of my entire site collection. I'm not sure how to loop my function to accomplish this.
This is my initial html:
<div id="treeviewDiv" style="width:200px;height:150px;overflow:scroll">
<ui id="treeviewList"></ui>
</div>
This is the Javascript I'm using:
So the way I tried to do it was that after I find my first site, run the $().SPServices to give me a list of the subsites based on the $(this).attr(url)
function getSiteTree(){
var tree = $('#treeviewList');
var rootsite = window.location.protocol + "//" + window.location.hostname;
$().SPServices({
operation: "GetWebCollection",
webURL: rootsite,
async: true,
completefunc(xData, Status){
$(xData.responseXML).find("Web").each(function(){
tree.append("<li value='" + $(this).attr("Url") + "'>" + $(this).attr("Title") + "</li>");
$().SPServices({
operation: "GetWebCollection",
webURL: $(this).attr("Url"),
async: true,
completefunc: function(xData, Status){
if($(xDate.responseXML.find("Web"))){
$(xData.responseXML).find("Web").each(function(){
strHTMLTopSites += "<li value='" + $(this).attr("Url") + "'>" + $(this).attr("Title") + "</li>";
*** I kind of gave up at this point, it seemed like I'd just be copying the amount of times I run this function arbitrarily which seemed like a bad way to do it.
});
}
});
}
});
}
This is the XML response you get when running $().SPServices:
<Webs xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<Web Title="Subsite1_Name" Url="http://Server_Name/[sites]/
[Site_Name]/[Subsite1]" />
<Web Title="Subsite2_Name" Url="http://Server_Name/[sites]/
[Site_Name]/[Subsite2]" />
</Webs>
This is the XML when there are no subsites:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetWebCollectionResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><GetWebCollectionResult>`<Webs />`</GetWebCollectionResult></GetWebCollectionResponse></soap:Body></soap:Envelope>
Something like this is what I'm trying to create:
<div class="demo-section">
<ul id="treeview">
<li>Furniture
<ul>
<li>Tables & Chairs</li>
<li>Sofas</li>
<li>Occasional Furniture</li>
<li>Childerns Furniture</li>
<li>Beds</li>
</ul>
</li>
<li>Decor
<ul>
<li>Bed Linen</li>
<li>Throws</li>
<li>Curtains & Blinds</li>
<li>Rugs</li>
<li>Carpets</li>
</ul>
</li>
<li>Storage
<ul>
<li>Wall Shelving</li>
<li>Kids Storage</li>
<li>Baskets</li>
<li>Multimedia Storage</li>
<li>Floor Shelving</li>
<li>Toilet Roll Holders</li>
<li>Storage Jars</li>
<li>Drawers</li>
<li>Boxes</li>
</ul>
</li>
</ul>
</div>
walk(branch)
that evaluates the given branch. Within walk(), you can call walk() for each sub-branch. Imagine you passed the function your hips (key word "imagine"). It would look at your hips and see that you (probably) have two legs. So, from within the function, you might have a loop which effectively doeswalk(left leg); walk( right leg)
. As it evaluates your left leg, it sees you (probably) have 5 toes. it loops and calls the walk() function 5 times, passing a single toe each time... [continued]