Posts Tagged ‘Site Collection List’

As a SharePoint developer or an administrator first thing that comes into our mind when we think of getting the list of site collection and subsites will be using stsadm command line utility.  You can use the command

stsadm.exe -o enumsites -url <root url>

to get the list  of site collections and

stsadm.exe -o enumsubwebs -url <root url>

to get the list of sub sites.

It will be great if we combine both of these. Here is the VB script code to  do this which will create an XML file that you can open in an excel file.

Option Explicit
Const STSADM_PATH =”C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm”
Const ROOT_URL = “
Const FILE_NAME = “G:\listofsites.xml”
Dim objShell, objExec, objXml, objXml2,objXml3, objSc,objFso, objFile, objWeb
Dim strResult, strSubResult, strUrl, strCmd, strOwner, strXML, returnValue, returnValue1 
‘Retrieves all site collections in XML format.
WScript.Echo “Creating shell object and calling root enumsites command”
Set objShell = CreateObject(“WScript.Shell”)
Set objExec = objShell.Exec(STSADM_PATH & ” -o enumsites -url ” & ROOT_URL)
strResult = objExec.StdOut.ReadAll
‘Load XML in DOM document so it can be processed.
WScript.Echo “Loading XML File”
Set objXml = CreateObject(“MSXML2.DOMDocument”)
Set objXml2 = CreateObject(“MSXML2.DOMDocument”)
Set objXml3 = CreateObject(“MSXML2.DOMDocument”)

WScript.Echo “Creating File System Object”
‘Create the FileSystemObject and write to file.
Set objFso = CreateObject(“Scripting.FileSystemObject”)
if objFso.FileExists(FILE_NAME) then
    objFso.DeleteFile FILE_NAME, True
end if
set objFile = objFso.CreateTextFile(FILE_NAME, True)
‘Loop through each site collection and call enumsubwebs to get the child URL’s.
For Each objSc in objXml.DocumentElement.ChildNodes
    strUrl = objSc.Attributes.GetNamedItem(“Url”).Text
    strOwner = objSc.Attributes.GetNamedItem(“Owner”).Text
    strCmd = STSADM_PATH & ” -o enumsubwebs -url “”” + strUrl + “”””
    Set objExec = objShell.Exec(strCmd)
    strResult = objExec.StdOut.ReadAll
    objFile.WriteLine(“<SITECOLLECTION SiteCollectionURL='” & strUrl & “‘ Owner = ‘” & strOwner & “‘>”)
    WScript.Echo “Traversing the sub Webs…”
    call GetSubSites(strResult)
set objFile = nothing
set objFso = nothing
set objXml = nothing
set objXml2 = nothing
set objXml3 = nothing
set objExec = nothing
WScript.Echo “File created”
sub GetSubSites(strResult)
returnValue1 = objXml2.LoadXML(strResult)
if returnValue1 = true then
for Each objWeb in objXml2.DocumentElement.ChildNodes
    strCmd = STSADM_PATH & ” -o enumsubwebs -url “”” + objWeb.text + “”””
    Set objExec = objShell.Exec(strCmd)
    strResult = objExec.StdOut.ReadAll
    returnValue = objXml3.LoadXML(strResult)
    if returnValue = true then
 if objXml3.DocumentElement.Attributes.GetNamedItem(“Count”).Text <> “0” Then
 WScript.Echo strResult
 call GetSubSites(strResult)
 end if
    end if

end if
end sub


ROOT_URL  will be your farm’s root URL.

FILE_NAME will be the path for output XML .

STSADM_PATH will be the path where yout stsadm.exe resides.

After creating the script file run it in command prompt using the command

cscript.exe ListAllMossSites.vbs

This will create a XML file. Open this in an excel. You will have all the details.