ShareThis

Generate XML Sitemap using PowerShell – SharePoint 2013 Search Engine Sitemap

From SEO perspective there are a number of things that we can do to improve the ranking of our site content. One of them is XML sitemaps. By submitting an XML Sitemap to search engines we can help them discover content on our website.

Unlike SharePoint 2010, SharePoint 2013 provides native support for generating XML Sitemaps. In order to generate an XML Sitemap for the site:
  1. First we have to enable the “Search Engine Sitemap” feature from Site Collection Features. This will register the Site Collection with the “Search Engine Sitemap job” Timer Job.
  2. Run “Search Engine Sitemap job” Timer Job, which anyway by default runs daily and generates an XML Sitemap for every Site Collection that has been registered with it.

In addition to generate XML Sitemap, Search Engine Sitemap job also modifies the robots.txt file. In there, it adds the link for the XML Sitemap file so that it can be easily discovered by search engines.

We can automate the generation of XML sitemap in deployment using PowerShell as follows:

function GenerateXmlSitemap([string] $siteCollectionUrl)
{
 try
 {
   $site = Get-SPSite $siteCollectionUrl
   $rootWeb = $site.RootWeb
   $id = [Guid](“7310C4BB-1407-4B73-96A1-2A88D034E039”);

   #1. Activate “Search Engine Sitemap” feature from Site Collection Features    
   Enable-SPFeature –identity “77fc9e13-e99a-4bd3-9438-a3f69670ed97” –URL $siteCollectionUrl –EA SilentlyContinue
             
   #2. Run “Search Engine Sitemap job”         
   $site.AddWorkItem([System.Guid]::Empty, [System.DateTime]::Now.ToUniversalTime(), $id, $rootWeb.ID, $site.ID, 1, $false,[System.Guid]::Empty, [System.Guid]::Empty, $rootWeb.CurrentUser.ID, $null, [System.String]::Empty, [System.Guid]::Empty, $false);
                    
   $webApplication = $site.WebApplication;
   $searchEngineSitemapJob = $webApplication.JobDefinitions |where{ $_.Name –match “SPSitemapJobDefinition” };

   runjob($searchEngineSitemapJob);
 }
 catch
 {
   write-host “Error: “+ $_.Exception.MessageForegroundColor Red
 }
}

function runJob($Job)
{
  write-host “Last execution time was:” $Job.LastRunTime

  [Array] $JobHistoryList=$Job.HistoryEntries

  Start-SPTimerJob $Job
  Write-Host “Running Timer Job:” –NoNewline
  Write-Host $Job.DisplayNameNoNewlineForegroundColor Yellow
        
  $count = $JobHistoryList.count
  $countNext = $count+1

  while ( $countNext –ne $count)
  {
    [Array] $JobHistoryList=$Job.HistoryEntries
    $count = $JobHistoryList.count
    Write-Host “=” –NoNewline
    sleep 2
  }
  Write-Host “”

  if ($Job.status –ne “Succeeded”)
  {
    Write-Host –f yellow $JobHistoryList[0].status
    Write-Host –f yellow $JobHistoryList[0].ErrorMessage
  }
  else
  {
    Write-Host –f Green $JobHistoryList[0].status
  }
}

GenerateXmlSitemapsiteCollectionUrl http://prasadsp2013/


Important:
  • For the XML Sitemap to be generated, anonymous access must be enabled on the Default Zone of the Site Collection for which it’s generating the XML Sitemap.
  • Also the XML Sitemap generation process uses Search to generate the XML Sitemap. Therefore crawl the site and verify that the pages have been indexed is important.

Find JobDefinition Name to use in PowerShell:

Go to the Central Administration -> Monitoring -> Review job definitions and find required job definition. Then by hover the mouse pointer over the job name we can get the job id.


Use URL decoder to decode the job id and using following PowerShell script we can get the job name.