There has been some talk about SharePoint site directories at work recently. As you may or may not know the old SharePoint 2007 site directory template was dropped. This isn’t that big of a deal since that feature was pretty limited in it’s usefulness (in my opinion). However with a little powershell magic it’s possible to create a site directory and have it updated automatically.

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$SPAssignment = Start-SPAssignment

#moddify these variables for the directory location
$URL = ""
$title = "Site List"
$description = "This is a list of all sites that have been created."

#delete the previous list
$SPWeb = Get-SPWeb $URL
$list = $SPWeb.Lists[$title]
$list.AllowDeletion = $true

#create list
$SPTemplate = $SPWeb.ListTemplates["Custom List"]

#add fields to list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$list = (Get-SPWeb -identity $URL).Lists[$Title]
$List.Fields.Add("Secondary Contact",$spFieldType,$false)

#create default view with added columns
$SPWeb = Get-SPWeb $URL
$spView = $SPWeb.GetViewFromUrl("/Lists/Site%20List/AllItems.aspx")
$spView.ViewFields.Add("Secondary Contact")

foreach ($site in get-spsite* -limit all | where {$_ -notlike "*Office_Viewing_Service_Cache"}) {
	$newItem = $list.Items.Add()
	$newItem["Title"] = $site.RootWeb
	$newItem["URL"] = $site.Url
    $newItem["Owner"] = $site.Owner.displayname
    $newItem["Secondary Contact"] = $site.SecondaryContact.DisplayName

Stop-SPAssignment $SPAssignment

This script is actually pretty simple. The flow is as follows.

  1. set variables.
  2. delete the old list if it exists.
  3. create a new custom list.
  4. add fields to the list.
  5. create a default view for the list
  6. add each site collection to the list (rootweb name, URL, primary site collection admin, and secondary site collection admin)

Since the script destroys the previous list during each it’s not very flexible, but is certainly simple. In the next post I’ll show how to create a site directory of just one site type.