Fixing the ‘Failed generating sitemap for site’ error in SharePoint 2013
XML Sitemaps for public-facing websites
One of the new features for public-facing websites in SharePoint 2013 is XML Sitemap. Using XML Sitemap you can build a list of all pages on your website. By submitting this list to Internet search engines, such as Bing or Google, you can help them discover and index the content of your website which will help you increase the search ranking of your website.
SharePoint 2013 XML Sitemap
The XML Sitemap mechanism provided with SharePoint 2013 consists of a few components – all of which need to be enabled and running properly for the XML Sitemap to be generated.
First of all there is the Search Engine Sitemap Site Collection Feature which determines whether or not XML Sitemap should be generated for this particular Site Collection. This Feature should be activated for the XML Sitemap to be generated.
Next there is the Search Engine Sitemap job Timer Job which, by default, is scheduled to run daily. Every time it executes, it will iterate over all Site Collections to check whether an XML Sitemap should be generated for that particular Site Collection or not. This is determined by checking if the Search Engine Sitemap Site Collection Feature is activated. One important thing to keep in mind is, that the Search Engine Sitemap Job uses SharePoint 2013 Search to gather the contents for the XML Sitemap.
Where things can go wrong: gathering the contents of XML Sitemap
The Search Engine Sitemap Job Timer Job uses SharePoint 2013 Search to retrieve all pages that should be included in the XML Sitemap for the particular Site Collection. Although this might seem trivial – after all how hard can executing a search query be, it isn’t. In fact, considering all the different content publishing models that we can use in SharePoint 2013 to publish content on our website, it is rather complex.
One of the steps in the process of gathering the contents of XML Sitemap for the particular Site Collection is retrieving content from the connected catalogs. Should this step fail the XML Sitemap will not be created and the only way for you to find out about the failure is to either verify the modified date of the sitemap.xml file or monitor the ULS log.
One of the reasons, for which retrieving content from connected catalogs might fail, is an invalid reference to the catalog’s Search Result Source. When connecting to a catalog, a catalog connection settings entry is created containing information about how the connected content should be displayed within the Site Collection.
One of the properties of a catalog connection information entry is ResultSourceId, which points to a Search Result Source used for retrieving catalog content. Whenever you’re connecting to a catalog this is all configured correctly, and works just as expected. However if you needed to rebuild your Search Service Application or restored the website to another Farm, there is a chance that this reference needs to be updated.
If a catalog connection information entry contains a reference to an invalid Search Result Source the XML Sitemap will not be created and in the ULS log you will see a rather cryptic error message:
SitemapJobDefinition.GenerateSitemap(): Failed generating sitemap for site:https://www.mavention.com , due to: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.SharePoint.Publishing.Internal.SearchResultSet.BuildCatalogQueryString(StringBuilder queryString)
at Microsoft.SharePoint.Publishing.Internal.SearchResultSet.BuildAndSetQueryString(KeywordQuery searchQuery)
at Microsoft.SharePoint.Publishing.Administration.SPSitemapJobDefinition.GenerateSitemap(SPSite site, SeoSettings seoSettings)
Fixing this issue is simple and comes down to updating the ID stored in the ResultSourceId property to a valid one. This can be done using the following PowerShell code snippet:
$site = Get-SPSite https://www.mavention.com $mgr = New-Object Microsoft.SharePoint.Publishing.CatalogConnectionManager($site) $cat = $mgr.ConnectedPublishingCatalogs $cat.ResultSourceId = '<valid result source id>' $mgr.UpdateCatalogConnection($cat) $mgr.Update()
We begin with retrieving a reference to the Site Collection for which we want to fix the catalog connection (line 1). Next we instantiate the Catalog Connection Manager for this Site Collection (line 2) . Using the Catalog Connection Manager we retrieve the information about the first catalog connection (line 3). Then we change its Search Result Source ID (line 4) and update it (line 5). Finally, by calling the Update method on the Catalog Connection Manager (line 6) we ensure that our changes are persisted in the Site Collection.
With the above changes applied, the next time the Search Engine Sitemap Job Timer Job will run it will generate the XML Sitemap for your site just as expected.
One of the new capabilities of SharePoint 2013 for building public-facing websites is generating XML Sitemaps. The contents of XML Sitemaps are gathered using SharePoint 2013 Search. In some scenarios, broken search settings can prevent XML Sitemaps from being generated. Luckily those settings can be easily fixed using PowerShell.