- Microsoft SharePoint 2010 and Windows PowerShell 2.0: Expert Cookbook
- Yaroslav Pentsarskyy
- 973字
- 2021-04-02 18:19:28
Installing features on the site and managing existing site features
In the last recipe, we became familiar with how to create a script to provision your projects' site hierarchy. It's quite common for any site template to use custom or out-of-the-box SharePoint features. Those features give site templates consistent functionality once the instance of the site has been created.
In this recipe, we'll take a look at what's involved in activating site features on sites using PowerShell.
We'll also see how the functionality from this recipe can be incorporated in the script we created in last recipe.
Getting ready
In this recipe, we'll use PowerGUI to add extra functionality to the script we discussed in the Provisioning site hierarchy during solution deployment recipe.
How to do it...
- Let's see how you can provision site hierarchy using the following steps:
- On the target Virtual Machine, ensure you are logged in with an administrator's role.
- Click Start | All Programs | PowerGUI | PowerGUI Script Editor.
- In the main script editing window of PowerGUI add the following script:
# Defining script variables [xml]$SiteStructure = get-content SiteStructure.xml $WebAppUrl = $SiteStructure.Setup.Attributes.Item(0).Value $SiteCollectionUrl = $SiteStructure.Setup.SiteCollection.Attributes.Item(1).Value $SiteUrl = $WebAppUrl + $SiteCollectionUrl # Loading Microsoft.SharePoint.PowerShell $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} if ($snapin -eq $null) { Write-Host "Loading SharePoint Powershell Snapin" Add-PSSnapin "Microsoft.SharePoint.Powershell" } # Deleting existing site found at target URL $targetUrl = Get-SPSite | Where-Object {$_.Url -eq $SiteUrl} if ($targetUrl.Url.Length -gt 0) { Write-Host "Deleting existing site at" $SiteUrl Remove-SPSite -Identity $SiteUrl -Confirm:$false } # Creating site structure $SiteCollectionName = $SiteStructure.Setup.SiteCollection.Attributes.Item(0).Value; $SiteCollectionOwner = $SiteStructure.Setup.SiteCollection.Attributes.Item(2).Value; $SiteCollectionTemplate = $SiteStructure.Setup.SiteCollection.Attributes.Item(3).Value; Write-Host "Creating new site collection at" $SiteUrl $NewSite = New-SPSite -URL $WebAppUrl$SiteCollectionUrl -OwnerAlias $SiteCollectionOwner -Template $SiteCollectionTemplate -Name $SiteCollectionName $RootWeb = $NewSite.RootWeb $features = $SiteStructure.Setup.SiteCollection.Features if($features.Feature.Length -gt 0) { foreach ($SiteColFeature in $features.Feature) { $ActivatedFeature = Enable-SPFeature $SiteColFeature -url $RootWeb.Url Write-Host "Enabled Feature:" $SiteColFeature -foregroundcolor Green } } Write-Host "Site collection created successfully" Write-Host "Title:" $RootWeb.Title -foregroundcolor Green Write-Host "URL:" $RootWeb.Url -foregroundcolor Green Write-Host "-------------------------------------" for ($i=1; $i -lt $SiteStructure.Setup.SiteCollection.ChildNodes.Count; $i++ ) { $childsite = $SiteStructure.Setup.SiteCollection.ChildNodes.Item($i); $WebName = $childsite.Attributes.Item(0).Value $WebUrl = $childsite.Attributes.Item(1).Value $WebTemplate = $childsite.Attributes.Item(2).Value Write-Host "Creating new web at" $SiteUrl/$WebUrl $NewWeb = New-SPWeb $SiteUrl/$WebUrl -Template $WebTemplate -Addtotopnav -Useparenttopnav -Name $WebName Write-Host "Web created successfully" Write-Host "Title:" $NewWeb.Title -foregroundcolor Green Write-Host "URL:" $NewWeb.Url -foregroundcolor Green $features = $SiteStructure.Setup.SiteCollection.ChildNodes.Item($i) if($features.Feature.Length -gt 0) { foreach ($WebFeature in $features.Feature) { $ActivatedFeature = Enable-SPFeature $WebFeature -url $NewWeb.Url Write-Host "Enabled Feature:" $WebFeature -foregroundcolor Green } } Write-Host "-------------------------------------" } start-process -filepath iexplore -argumentlist $SiteUrl
- Click File | Save to save the script to your development machine's desktop. Set the filename of the script to
setup.ps1
. - Click File | New in the PowerGUI application.
- Add the following code in the newly opened file window:
<Setup WebAppUrl="http://intranet.contoso.com"> <SiteCollection Name="Root Site" Url="/sites/rootsite" OwnerAlias="contoso\administrator" Template="STS#0"> <Features> <Feature>ContentTypeSyndication</Feature> </Features> <Site Name="Child 1" Url="child1" Template="STS#0"/> <Site Name="Child 2" Url="child2" Template="STS#0"/> <Site Name="Child 3" Url="child3" Template="STS#0"/> </SiteCollection> </Setup>
- Save the file on your desktop by clicking File | Save, providing the following filename:
SetupStructure.xml
. - Switch back to your browser and navigate to our SharePoint test site:http://intranet.contoso.com/sites/rootsite.
- Click Site Actions | Site Settings | Site Collection Administration | Site collection features.
- Take note that the In Place Records Management feature, seen below, should not be set to Active.
- Open the PowerShell console window and call
setup.ps1
using the following command:PS C:\Users\Administrator\Desktop> .\setup.ps1
- As a result, your PowerShell script will create a site structure and activate the In Place Records Management feature as shown in the following screenshot:
- From the SharePoint site, click Site Actions | Site Settings | Site Collection Administration | Site collection features.
- Take note that the In Place Records Management feature is now set to Active.
How it works...
Similar to the Provisioning site hierarchy during solution deployment recipe, the provisioning process consists of two parts: the PowerShell script executing the provisioning, and the XML defining our site structure and other parameters.
The additional node,<Features/>
, specifies any of the feature names that will be activated on the site collection.
If there are features to be activated at the site collection level, the<Features/>
node will contain each feature folder name defined in the following format:
<Features> <Feature>Feature 1 Folder Name</Feature> <Feature>Feature 2 Folder Name</Feature> </Features>
To find out the name of the folder for a particular installed feature on your site, from the development machine, navigate to: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES
. This is where SharePoint holds all of the installed features, each of which has its own respective folder. In our example, we have activated the In Place Records Management feature by calling out the InPlaceRecords feature, as shown in the following screenshot:

Similar to the<SiteCollection/>
node, the<Site/>
node defines sites to be created under the site collection. Here, we can also define SharePoint features to be enabled.
Note
The features for the site need to be scoped for the site level and not the site collection.
Let's take a look at our PowerShell provisioning script.
At first, the script gets a hold of the XML file which defines common provisioning variables.
Once the PowerShell snap-in has been loaded, the script proceeds to create the site collection with details defined in the<SiteCollection/>
node. If features are to be enabled on the site collection level, the following script will enumerate all of the nodes representing features and enable them sequentially:
$features = $SiteStructure.Setup.SiteCollection.Features if($features.Feature.Length -gt 0) { foreach ($SiteColFeature in $features.Feature) { $ActivatedFeature = Enable-SPFeature $SiteColFeature -url $RootWeb.Url Write-Host "Enabled Feature:" $SiteColFeature -foregroundcolor Green } }
Once site collection has been provisioned, any associated sites under the site collection are now provisioned. If site nodes have features specified on them, those are also enabled after the site has been created.
The following portion of the script enables site features associated to the site:
$features = $SiteStructure.Setup.SiteCollection.ChildNodes.Item($i) if($features.Feature.Length -gt 0) { foreach ($WebFeature in $features.Feature) { $ActivatedFeature = Enable-SPFeature $WebFeature -url $NewWeb.Url Write-Host "Enabled Feature:" $WebFeature -foregroundcolor Green } }
When the provisioning is complete, the site structure with associated features gets provisioned to your SharePoint system.
- DotNetNuke 5.4 Cookbook
- Alfresco Developer Guide
- Final Cut Pro X 影視包裝剪輯完全自學教程(培訓教材版)
- Photoshop CS5平面設計入門與提高
- Solid Works 2021產品設計標準教程
- 圖解Word 2007圖文排版與辦公應用
- Drupal 6 Panels Cookbook
- Unity 3D\2D手機游戲開發:從學習到產品(第4版)
- Asterisk 1.4 : The Professional's Guide
- 虛擬現實:沉浸于VR夢境
- AutoCAD 2016中文版基礎教程(全圖解視頻版)
- 跨境電商:速賣通搜索排名規則解析與SEO技術
- Revit技巧精選應用教程
- Cinema 4D R20完全學習手冊
- Maya Paint Effect 特效應用手冊