List SharePoint Subsites within a Site Collection using Content Editor Web Part and JavaScript

List SharePoint Subsites within a Site Collection using Content Editor Web Part and JavaScript


Step 1: Kindly follow my intital post to understand how java script can be used in Content Editor Web Part.

Step 2: Consider a list like "Announcement"  with custom "Yes\No" type column.

Step 3: Add Content Editor Web Part.

Step 4: Write the following code in a text. save as html. upload into SharePoint.Link this html page to content editor web part or Write code in Content Editor Web Part's text editor directly.

<html>
<head>


<SCRIPT type=text/javascript src="/mysites/myDocLibrary/JsFiles/jquery-1.4.2.min.js"></SCRIPT>
<SCRIPT type=text/javascript src="/mysites/myDocLibrary/JsFiles/JsFiles/jquery.SPServices-0.5.6.min.js"></SCRIPT> 
<SCRIPT type=text/javascript>


<title>Sites Within Site Collection</title></head>
<body>
<div id="divId"/> </div>
 

<SCRIPT type=text/javascript>

 var thisSite = $().SPServices.SPGetCurrentSite();
 get() ;

 function get()
 {

  $().SPServices
  ({
     operation: "GetWebCollection",
     webURL:
http://sample.com/sites/mySiteCol
,                              
     completefunc: function (xData, status)
     {

       $(xData.responseXML).find("Web").each(function()
       {
          var webtitle = $(this).attr("Title");
          var weburlvar = $(this).attr("Url");
        
  $("#divId").append("<img     src=http://sample.com/sites/BaisJs/Pics/stsicon.gif>    <a href='"+ weburlvar + "' target='_blank'>" + " " + webtitle + "</a><br/>");
        });
      }
   });
}

</script>
</body>
</html>


Step 5: In Content Editor Web Part, within the Web Part Setting, Under Appearance, For Chrome Type select "Title only". Click OK. Click Apply. Publish the Page. Now you can see that this web part shows all Sites within a Site Collection.



Leave your comments below.

No comments: