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 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:
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:
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: