Upgrading SharePoint feature in Visual Studio with CKS Dev extensions

Hi all! This is my revisited post from this one. I am going to show how we can call and test feature upgrade methods directly in visual studio. Lets start.

We need visual studio and CKS Dev extensions. CKS Dev adds additional deployment steps into your sharepoint project. Among them Upgrade Solution. This command simply run Update-SPSolutioncmdlet(or may be stsadm analog). To upgrade feature we need to call Upgrade explicit on the feature. CKS Dev hasn’t step “Upgrade feature”, that’s why we will use “Run PowerShell Script” step to upgrade particular feature. We start from creating custom deployment configuration.

Make this deployment configuration active. Create new folder under solution, for example “Scripts” and add new .ps1 file that will contain upgrade logic. As you remember, the first command in our deployment configuration is Upgrade Solution. It is significant, that this operation is asynchronous, it takes some time to complete, so we can’t upgrade particular feature while .wsp file, that contains this feature is still updating in central admin. We need to wait, until update solution operation will be completed, then we can call feature upgrade method. Here is a code, that wait while solution is being upgrading:

function WaitForUpgrading($solutionName)
{
	$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
	$isExecuted = $true

	$solution = $null
	foreach ($farmSolution in $farm.Solutions)
	{
		if ($farmSolution.Name.Equals($solutionName, [System.StringComparison]::InvariantCultureIgnoreCase))
		{
			$solution = $farmSolution
			break;
		}
	}
	if ($solution -ne $null)
	{
		while ($solution.JobExists)
		{
			write-host    ***Waiting "while" $solutionName is being upgraded...***
			[System.Threading.Thread]::Sleep(5000)			
		}
		$result = $solution.LastOperationResult
		if ($result -ne [Microsoft.SharePoint.Administration.SPSolutionOperationResult]::DeploymentSucceeded -and
            $result -ne [Microsoft.SharePoint.Administration.SPSolutionOperationResult]::DeploymentWarningsOccurred)
		{
			write-host "EXCEPTION:" $solutionName has been upgraded with ERRORS
		}
		else
		{
			write-host $solutionName has been successfully upgraded!
		}
	}
}

$solutionName is a wsp file name (including .wsp extenstion). When solution is updated, we can call feature upgrade method:

function UpgradeFeatures($featureId)
{
	write-host Start upgrading features at $siteUrl 
	$featureGuid = New-Object System.Guid($featureId)
	$features = $site.QueryFeatures($featureGuid, $true)
	foreach($feature in $features){
		$feature.Upgrade($false)
		write-host " "
		write-host ---- $feature.Definition.DisplayName was upgraded. Parent: $feature.Parent----
	}
    write-host Finish upgrading
}

As you can see, I upgrade only site collection features, you of course, can modify script to upgrade all web application or web service or content db features. The rest of code in ps1 file:

param([string]$siteUrl, [string]$solutionName, [string]$featureId)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

<UpgradeFeatures and WaitForUpgrading functions omitted for brevity >

$site = Get-SPSite $siteUrl
write-host " "

WaitForUpgrading $solutionName
UpgradeFeatures $featureId

Next we need to add call to this script in visual studio.Left click on the sharepoint project in solution explorer and then  View->Properties Window. Here you can specify which script to run and pass parameters to it. 

I pass solution name, site collection url and upgraded feature id to my script. Active deployment configuration is “UpgradeFeatures” created above. Now all ready to upgrade. Make required modifications in your feature xml files and you can test upgrade. You can run it by right clicking on the sharepoint project and then click “Deploy”.

That’s it. In next feature upgrade related post I am going to show architecture that we use with feature upgrade actions.

Sample project you can download here (9.32 kb). Hope this helps and good luck in sharepoint development!