Upgrading SharePoint feature in Visual Studio with CKS Dev extensions

by Kai 4. February 2012 10:09

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, which should look like follows:

image

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. Consider screenshot below:

up1

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”. If at least one feature has been upgraded, your output window should looks like this one:

up2

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!

Tags: , , , , ,

SharePoint 2010

Comments (2) -

Ripu
Ripu
2/29/2012 6:53:50 PM #

when i am deploying the following error occurred which is :

"Error occurred in deployment step 'Upgrade Solution (CKSDev)': Cannot upgrade the solution. The IsSolutionDeployed command cannot find the following solution: upgradefeaturevs.wsp."

how can to put IsSolutionDeployed Command in solution.can anyone give me idea about ?

Reply

Kai
Kai
3/1/2012 4:01:35 AM #

Before upgrade solution, you should deploy it.
Switch to Default deployment configuration and deploy solution. Once deployed, it can be upgraded. So, you can switch to UpgradeFeatures deployment configuration now and run deploy again (and also, to make sure that upgrade action will be executed, you should increase feature version in feature xml file - TestUpgradeFeature.Template.xml).

Reply

Pingbacks and trackbacks (1)+

Add comment

biuquote
  • Comment
  • Preview
Loading