Archive

PowerShell

In many scenarios where you have to build up a complex Information Architecture in SharePoint, you will also have to set the Search Settings for each site or site collection to point to your search center and your custom results page.

Search Settings Page

Search Settings for a site in SharePoint 2013 UI

These settings are stored in the property bag of the site. For SharePoint 2010, this blog post shows the property names, and how they can be set using Powershell. In SharePoint 2013 the search settings have become a little bit more complex, therefore we need a new script to set them. Below you will find two powershell functions: one that sets the search settings for the entire site collection and one just for a specific web.

function SetSearchSettingsSite($siteUrl,$searchCenterUrl,$resultPagesUrl)
{
 $site = Get-SPSite $siteUrl
 $site.AllWebs | ForEach-Object {
 $web = $_
 $web.AllProperties["SRCH_SB_SET_WEB"] = '{"Inherit":false,"ResultsPageAddress":"'+$resultPagesUrl+'","ShowNavigation":false}'
 $web.AllProperties["SRCH_ENH_FTR_URL_WEB"] = $searchCenterUrl
 $web.Update()
 }
}
function SetSearchSettingsWeb($siteUrl,$searchCenterUrl,$resultPagesUrl)
{
 $web = Get-SPWeb $siteUrl
 $web.AllProperties["SRCH_SB_SET_WEB"] = '{"Inherit":false,"ResultsPageAddress":"'+$resultPagesUrl+'","ShowNavigation":false}'
 $web.AllProperties["SRCH_ENH_FTR_URL_WEB"] = $searchCenterUrl
 $web.Update()
}
SetSearchSettingsSite http://spdevel.portal.com/ http://spdevel.portal.com/search http://spdevel.portal.com/search/Pages/Results.aspx

The second part of the search settings has been integrated in a single web property – “SRCH_SB_SET_WEB”, as a representation of a SharedSearchBoxSettings object. This script only sets the search settings at site-level. If you want to configure the search settings at site-collection level or at farm-level, it would be slightly different. At site-collection level, you would need to set the root web property “SRCH_SB_SET_SITE”, and for farm-level, you would need to set the SharedSearchBoxSettings and SearchCenterUrl properties of the Search Service Application. For more details, please see this post.

In a different blog post I have explained the basics of working with publishing navigation in SharePoint 2013. The OOB navigation offers two options: “structural navigation” and “managed metadata navigation”.

Managed metadata navigation is bound to a term set, that provides the navigation nodes for your site (more details on how to configure it here). If you want to change this, or you are simply looking for a script that will set this up in new environments, here it is:

Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$assembly = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

function UpdateNavigation($url,$termStoreName,$termGroupName,$termSetName){
 $web= Get-SPWeb $url
 $site = $web.Site
 $navSettings = New-Object Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings($web)
 $taxSession = Get-SPTaxonomySession -Site $site
 $termStore = $taxSession.TermStores[$termStoreName]
 $termGroup = $termStore.Groups[$termGroupName]
 $termSet = $termGroup.TermSets[$termSetName]
 #Quick Launch
 $navSettings.CurrentNavigation.Source = 2
 $navSettings.CurrentNavigation.TermStoreId = $termStore.Id
 $navSettings.CurrentNavigation.TermSetId = $termSet.Id
 #Global Navigation
 $navSettings.GlobalNavigation.Source = 2
 $navSettings.GlobalNavigation.TermStoreId = $termStore.Id
 $navSettings.GlobalNavigation.TermSetId = $termSet.Id

$navSettings.AddNewPagesToNavigation = $false
 $navSettings.CreateFriendlyUrlsForNewPages = $false
 $navSettings.Update()
 Write-Host "Navigation updated succesfully for site $url"
}
#Sample usage: UpdateNavigation http://spdevel.portal.com/ "Managed Metadata Service" "My Navigation Group" "Navigation"

An important limitation of this managed metadata navigation is the fact that a term set can only be bound to a single site. You can find more on this topic post.

Here is a very handy PowerShell script that can speed up your web part development in SharePoint. It is a common scenario when you configure an OOB web part, and then you want to export it to XML  in order to integrate it in your custom solution for future deployments. This can occur in various scenarios, and with the arrival of the SharePoint 2013, it will become even more frequent. It works great in SharePoint 2010 also. I find it very helpful especially when working with search verticals, but it can be used in many other situations.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function EnsureDirectory($exportFolderPath)
 {
 if ( -not (Test-Path $exportFolderPath) ) {New-Item $exportFolderPath -Type Directory | Out-Null}
 }

function ExportAllWebParts($siteUrl,$pageUrl,$exportFolderPath)
 {
 $web = Get-SPWeb $siteUrl
 $wpm = $web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

EnsureDirectory $exportFolderPath

foreach($wp in $wpm.WebParts)
 {
 $wp.ExportMode="All";
 $exportPath = $exportFolderPath + "\" + $wp.Title + ".xml"
 $xwTmp = new-object System.Xml.XmlTextWriter($exportPath,$null);
 $xwTmp.Formatting = 1;#Indent
 $wpm.ExportWebPart($wp, $xwTmp);
 $xwTmp.Flush();
 $xwTmp.Close();

}
 }
ExportAllWebParts $args[0] $args[1] $args[2]

# I use this script as a file, ExportWebParts.ps1. The required arguments for this script should be:
#The site absolute URL
#The page site-relative URL
#The folder path for export

#example usage ./ExportWebParts.ps1 http://spdevel.portal.com/ Pages/default.aspx C:\temp\Export