官术网_书友最值得收藏!

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

  1. Let's see how you can provision site hierarchy using the following steps:
  2. On the target Virtual Machine, ensure you are logged in with an administrator's role.
  3. Click Start | All Programs | PowerGUI | PowerGUI Script Editor.
  4. 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
    
  5. Click File | Save to save the script to your development machine's desktop. Set the filename of the script to setup.ps1.
  6. Click File | New in the PowerGUI application.
  7. 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>
    
  8. Save the file on your desktop by clicking File | Save, providing the following filename: SetupStructure.xml.
  9. Switch back to your browser and navigate to our SharePoint test site:http://intranet.contoso.com/sites/rootsite.
  10. Click Site Actions | Site Settings | Site Collection Administration | Site collection features.
  11. Take note that the In Place Records Management feature, seen below, should not be set to Active.
  12. Open the PowerShell console window and call setup.ps1 using the following command:
    PS C:\Users\Administrator\Desktop> .\setup.ps1 
    
  13. 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:
  14. From the SharePoint site, click Site Actions | Site Settings | Site Collection Administration | Site collection features.
  15. 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.

主站蜘蛛池模板: 得荣县| 承德市| 离岛区| 柘城县| 彭州市| 武穴市| 潜山县| 邮箱| 万宁市| 和平县| 加查县| 灵丘县| 博爱县| 乐业县| 太保市| 慈溪市| 汉源县| 广平县| 九江市| 宁强县| 沛县| 晋城| 中阳县| 谷城县| 怀来县| 东乡| 丰台区| 高邑县| 棋牌| 富平县| 大姚县| 封开县| 清丰县| 西乌珠穆沁旗| 文登市| 阿城市| 吉水县| 车险| 临武县| 广州市| 金坛市|