Saturday, March 22, 2014

SharePoint 2013 - Windows Solution Package (.WSP) Deployment

Overview
In the previous post I had highlighted on how to create and deploy a SharePoint 2013 feature using visual studio 2012. Usually for development environments, this is the usual practice so that the developer can easily deploy solutions and test\debug it.

But, what if you want to deploy the feature to another server? In this case you might need to provide an installer or deployable solution that deploys all the supporting files to the respective places and also installs and activates the feature.

In SharePoint 2013 we can use solution packages for this purpose and deploy it using STSADM.EXE command which resides in 15 Hive at: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\BIN\STSADM.EXE

Solution Package
A Solution Package is a cabinet file (.cab compressed file) with an extension of .WSP—which stands for Windows SharePoint Services Solution Package, provided to automate the process of installing features and customizations.
Through a .WSP package, you can deploy a set of one or more features, automatically copying the files and folders to every front-end server from a centralized management console.
A .wsp contains a solution-specific manifest file, called solution manifest, which is yet another XML file that defines a set of information through some attributes and some child elements.

Creating a solution Package
Create a feature using the steps mentioned in the previous post (SharePoint 2013 - Developing a feature using visual studio 2012). Instead of deploying directly to the site we will create a solution package and use STSADM.EXE to deploy it.

Steps for creating a solution package
Following are the steps for creating a solution package for this feature:

1) Open the project in visual studio 2012. Go the Package folder and open Package.package file. You can see the package name and view/add/remove its contents in the designer view.

2) Click on the Manifest tab and you can see the contents of solution package in XML format which mainly consist of the Assembly location and the Manifest location as shown in the figure below.

3) Now select Build->Publish option in VS 2012. Choose a suitable target location for publishing the solution package.

4) The solution package (.wsp file ) will be published at the specified location.


Solution Deployment and Feature activation using STSADM
Move the WSP file to a convenient location in the file system (E\WSP in my case). Run command prompt as administrator and change directory to 15Hive/bin folder as follows: cd C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\BIN\

Steps for deploying the feature
Following are the steps for deploying the feature:

1) Adding the wsp solution
STSADM.EXE -o addsolution -filename E:\SolutionPackage\SharePoint2013FeatureDemo.wsp

2) Deploying the solution
STSADM.EXE -o deploysolution -name SharePoint2013FeatureDemo.wsp -immediate -allowGacDeployment -force

3) Execute Time Job
stsadm -o execadmsvcjobs

4) Installing the feature 
STSADM.EXE -o installfeature -name SharePoint2013FeatureDemo_Feature1 -force

5) Activating the feature
STSADM.EXE -o activatefeature -name SharePoint2013FeatureDemo_Feature1 -url http://sp2013home:10001/ -force

6) Custom page deployed
Once the feature is activated you can visit the newly deployed page as shown in the figure below.


Feature De-activation and Solution Retractions using STSADM

1) De-activating the feature 
STSADM.EXE -o deactivatefeature -name SharePoint2013FeatureDemo_Feature1 -url http://sp2013home:10001/ -force

2) Un-installing the feature
STSADM.EXE -o uninstallfeature -name SharePoint2013FeatureDemo_Feature1 -force

3) Retracting the solution
STSADM.EXE -o retractsolution -name SharePoint2013FeatureDemo.wsp -immediate

4)  Execute Time Job
stsadm -o execadmsvcjobs

5) Deleting the solution
STSADM.EXE -o deletesolution -name SharePoint2013FeatureDemo.wsp -override

6) Custom Page Removed
Once the feature is de-activated, you will no longer be able to view the custom page deployed along with the feature.

Hope this post was helpful to you!

Saturday, March 8, 2014

SharePoint 2013 - Feature Receivers

Feature Receivers
A feature receiver is a class which can execute custom code through SharePoint object model whenever a particular feature life cycle related events occurs.

Feature Receiver Events
A feature receiver can trap the following events:
1) Feature Activation: Occurs when a feature has been activated
2) Feature Deactivating: Occurs while a feature is deactivating
3) Feature Installation: Occurs when a feature has been installed
4) Feature Uninstalling: Occurs while a feature is un-installing
5) Feature Upgrading: Occurs while a feature is upgrading

Feature Receiver Event Classes

1) Microsoft.SharePoint.SPFeatureReceiver
To implement feature receivers, you need to define a new class that inherits from the base abstract class, SPFeatureReceiver, defined in the Microsoft.SharePoint namespace.
public abstract class SPFeatureReceiver {
public SPFeatureReceiver();
public virtual void FeatureActivated(SPFeatureReceiverProperties properties);
public virtual void FeatureDeactivating(SPFeatureReceiverProperties properties);
public virtual void FeatureInstalled(SPFeatureReceiverProperties properties);
public virtual void FeatureUninstalling(SPFeatureReceiverProperties properties);
public virtual void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, IDictionary<string, string> parameters);}

2) Microsoft.SharePoint.SPFeatureReceiverProperties
Each virtual method accepts an argument of type SPFeatureReceiverProperties, which allows accessing information about the target feature, its definition, and the current site.
public sealed class SPFeatureReceiverProperties : Idisposable {
public SPFeatureDefinition Definition { get; internal set; }
public SPFeature Feature { get; }
public SPSite UserCodeSite { get; }}

Feature.Parent property
We can access the context of the feature through the Feature.Parent property of the current SPFeatureReceiverProperties argument.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
  SPSite site = properties.Feature.Parent as SPSite//Depending on the scope of your feature
}

Feature.Parent Scope
Depending on the scope of your feature, the Parent property could be:-
1) The whole farm (SPFarm),
2) A single web application (SPWebApplication),
3) A Site Collection (SPSite),
4) A single website (SPWeb).

Feature Receiver Reference
To create a feature receiver, you need to implement the receiver class, build its assembly, put it into the GAC, and declare the ReceiverAssembly and ReceiverClass attributes in a feature manifest XML file.

<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Version="1.0.0.0"
Title="My Web Part"
Description="This feature deploys a sample Web Part."
Id="c46c270e-e722-4aa0-82ba-b66c8dd61f4e"
ReceiverAssembly="MyApp.SP2010.MyFeature, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=b001133e0647953d"
ReceiverClass="MyApp.SP2010.MyFeature.MyWebPartEventReceiver"
Scope="Site">
    <ElementManifests>
        <ElementManifest Location="MyWebPart\Elements.xml" />
        <ElementFile Location="MyWebPart\MyWebPart.webpart" />
    </ElementManifests>
</Feature>

Adding a Feature Receiver
In Visual Studio 2012, go to the Solution Explorer and right-click a feature item within the Features folder of your SharePoint project to open the contextual menu. There, you can select the Add Event Receiver menu item, which will create all the plumbing code for you.

Hope this gives a brief overview about SharePoint 2013 Feature Receivers.  In the next post we will create a sample feature receiver in order to take a deeper look.