SharePoint 2016 – Improve Your Business

SharePoint 2016 – How to activate a feature for a deployed custom solution using PowerShell.

On a recent project I was given a large number SharePoint 2016 solutions files to deploy and features to activate using PowerShell. So this would normally involve examining each solution wsp file and finding the ID or name of the feature to activate. With so many solution files this was going to be a bit long winded affair. For those who don’t know, that’s renaming the wsp to a cab file, then looking inside the feature.xml file within the cab file and finally getting the id or name to activate! I thought I’ll just use PowerShell to do the whole biz.

The stumbling block was the Get-SPFeature cmdlet, which was fussy on the Identity parameter. After trial and error I finally cracked it. It seemed obvious looking back, but being SharePoint that’s always the case.

Below is the script, just change the $SiteURL and $packageName variables at the top of the script. Happy SharePointing!

#Variables
$SiteURL=”http://siteurlhere”
$packageName = “MySolution.wsp”

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq ‘Microsoft.SharePoint.Powershell’}
if ($snapin -eq $null) { Add-PSSnapin “Microsoft.SharePoint.Powershell” }

[System.GUID]$solId = (Get-SPSolution $packageName).Id
#Get the Feature name or you can use the Feature Id
$FeatureName = Get-SPFeature | where {$_.solutionId -eq $solId}|select DisplayName
$FeatureId = Get-SPFeature | where {$_.solutionId -eq $solId}

#Check if feature is already activated in the site collection using Feature Name
$Feature = Get-SPFeature -site $siteURL | Where-object {$_.DisplayName -eq $FeatureName.DisplayName}
#Check if feature is already activated in the site collection using Feature id
#$Feature = Get-SPFeature -site $siteURL | Where-object {$_.Id -eq $FeatureId .Id}

if($Feature -eq $null)
{
#Enable the custom feature
Enable-SPFeature -Identity $FeatureId.DisplayName -url $SiteURL -Confirm:$False
Write-host “Feature $($FeatureName.DisplayName) activated on site $($SiteURL)”
}
else
{
Write-host “Feature $($FeatureName.DisplayName) is already active on site $($SiteURL)” -ForegroundColor Cyan
}

Contact risual today to ensure that your business challenges are overcome or follow us on Twitter for the latest risual updates. 

About the author