Powershell to update quick launch in SharePoint 2013

Below is the powershell, which is being used to

1- update the title and URL of existing heading in left navigation.
2- add all the sibling subsites (title and URL) as a node under the heading.

#SharePoint site url
$MainSiteURL = "Site URL for which you want to change the quick launch"
$ParentSiteURL = "Site URL of the parrent site of above site as you want to add the sibling subsites"
$ParentSiteName = "Name of a parent site"

           
#Get the SPWeb object for the site url           
$MainWeb = get-spweb $MainSiteURL
$ParentWeb = get-spweb $ParentSiteURL

#Get the quick launch menu           
$ql = $MainWeb.Navigation.QuickLaunch;           

#change the heading title and URL
$qlHeading = $ql | where { $_.Title -eq "subsites" }
$qlHeading.Title = $ParentSiteName
$qlHeading.Url = $ParentSiteURL
$qlHeading.Update()

# get all subsites
if($ParentWeb.Webs -ne $null)
{
    foreach($sitesUnderParentWeb in $ParentWeb.Webs)
    {
        # to create a new node
        $newnode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($sitesUnderParentWeb.Title, $sitesUnderParentWeb.Url, $true)
        # add this node to heading
        $qlHeading.Children.AddAsLast($newnode)
       
    }
}
$MainWeb.Update()           

No comments:

Post a Comment