Skip to content

Instantly share code, notes, and snippets.

@devendrasv
Last active August 29, 2015 14:03
Show Gist options
  • Save devendrasv/410e6169b89c72366bec to your computer and use it in GitHub Desktop.
Save devendrasv/410e6169b89c72366bec to your computer and use it in GitHub Desktop.
Sitemap for SharePoint
param($Site,$FilePath)
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
function New-SPSiteMap ($SavePath, $Url)
{
try
{
$site=Get-SPSite $Url
$items = $site.Allwebs | ForEach { $_.Lists } | ForEach-Object -Process {$_.Items}
$items | New-Xml -RootTag urlset -ItemTag url -ChildItems loc -ChildItems2 lastmod -ChildItems3 changefreq -ChildItems4 priority -SavePath $SavePath
}
catch
{
$_Exception.Message
break
}
}
function New-Xml
{
param($RootTag="urlset",$ItemTag="url",$ChildItems="*",$ChildItems2="*",$ChildItems3="*",$ChildItems4="*",$SavePath)
Begin {
$xml="<?xml version=""1.0"" encoding=""UTF-8""?><urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">"
}
Process
{
foreach ($item in $_)
{
$itemUrl = $item.web.url.Replace(" ","%20") + "/" + $_.url.Replace(" ","%20")
#excludes directories you don’t want in sitemap. you can put multiple lines here:
$itemUrl= $itemUrl | ? {$_ -notmatch "_catalogs"}
$itemUrl= $itemUrl | ? {$_ -notmatch "Cache%20Profiles"}
$itemUrl= $itemUrl | ? {$_ -notmatch "Reports%20List"}
$itemUrl= $itemUrl | ? {$_ -notmatch "Long%20Running%20Operation%20Status"}
$itemUrl= $itemUrl | ? {$_ -notmatch "Relationships%20List"}
$itemUrl= $itemUrl | ? {$_ -notmatch "ReusableContent"}
$itemUrl= $itemUrl | ? {$_ -notmatch "Style%20Library"}
$itemUrl= $itemUrl | ? {$_ -notmatch "PublishingImages"}
$itemUrl= $itemUrl | ? {$_ -notmatch "SiteCollectionDocuments"}
$itemUrl= $itemUrl | ? {$_ -notmatch "Lists/"}
$itemUrl= $itemUrl | ? {$_ -notmatch "Documents/"}
$itemUrl= $itemUrl | ? {$_ -notmatch "PublishedLinks"}
$itemUrl= $itemUrl | ? {$_ -notmatch "WorkflowHistory"}
$itemUrl= $itemUrl | ? {$_ -notmatch "WorkflowTasks"}
$itemUrl= $itemUrl | ? {$_ -notmatch "Scripts/"}
if ($itemUrl)
{
$xml += "<$ItemTag>"
$lastMod = $item["Modified"].ToString("yyyy-MM-dd")
$xml += " <$ChildItems>$itemUrl</$ChildItems><$ChildItems2>$lastmod</$ChildItems2><$ChildItems3>daily</$ChildItems3><$ChildItems4>0.5</$ChildItems4>"
$xml += "</$ItemTag>"
}
}
}
End {
$xml += "</$RootTag>"
$xmltext=[xml]$xml
$xmltext.Save($SavePath)
}
}
New-SPSiteMap -Url $Site -SavePath $FilePath
write-host "SiteMap for $Site saved to $FilePath" -foregroundcolor green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment