This article will describe how to setup a basic sharepoint wsp solution project, build it and deploy it into Sharepoint. It included example code and instructions on building the Sharepoint 2007 feature project, the required directory structure, code and a basic description of each part of the project.
Installing sharepoint features can have several challenges, but with a few well placed steps you can be off and running. This article will walk you through some basic steps to proper creation of a Sharepoint 2007 feature project in Visual Studio 2008 that can be built into a WSPBuilder deploy solution file, and includes some handy scripts to deploy into the Sharepoint site of your choice.
You will need to open this project in Visual Studio 2008 or modify the project and solution files for an earlier version. If this worked for you vote me a 5!
You will need to open project in Visual Studio 2008 and have the plug in for WSPBuilder to use the example code provided and follow the article. The next step to creating a project in Visual Studio for a feature or other code on your site is to set up your Visual Studio Project correctly. Below is a typical project hierarchy for a feature with some custom pages in Visual Studio:
Notice the main content files in a feature deploy are located under the TEMPLATE directory. This allows WSPBuilder to build a .wsp solution file that understands once deployed where to put everything. To build your feature correctly you need to have a few directories in place.
The main directories for this project are:
For this example we will create "FEATURES/SampleFeature" to store the feature.xml and elements.xml for a feature. The feature for this example has functionality that will add a menu item to the edit control block menu (the right clicked popup menu on a document in a list in Sharepoint) and include this new menu item for all documents in site lists in the Sharepoint site collection for which this feature will be deployed.
The first part of the project we will examine is the feature.xml. This file contains the code which will tell Sharepoint what code to use for the feature, the "scope" of the code or where in the Sharepoint hierarchy the code will be availiable, and assign a Guid as an Id for use in installation. In the xml example below we see several attributes:
Each attribute has a purpose and they are as follows:
The elements.xml holds specific functional actions for the feature.
The "Module" node has certain attributes like "List", "Url", and "Path" that determine what type of Sharepoint list this feature will be activated for. Since Sharepoint is a hierarichal collection of objects (or lists) these attributes determine which type and area to apply the feature to. The example below applies the feature to lists of type 113 which refers to a web part gallery. The "File" xml node gives us code that helps the solution to be deployable.
In the code below we see the xml node "Module" and "CustomAction".
The attributes on the "CustomAction" node have the following useage:
The "CustomAction" xml node applies the feature to some sharepoint action. In this case it defines a menu item in the "EditControlBlock" which is an identifier for a Sharepoint object type indicating an editing menu for a document. It defines a "UrlAction" node which gives the element an action of going to a specified url. The url points back to the page we defined in the project (LAYOUTS/SampleFeaturePages/HellowWorld.aspx).
Here is how it will look once deployed:
A few other parts of the solution are interesting.
The "LAYOUTS/SampleFeaturePages/HellowWorld.aspx" is the page that is called from the features CustomAction/UrlAction and displays some basic information on the list item in question passed to this page (in this case the ItemId and ListId of the document as it is assigned in the Sharepoint site).
The FeatureReciever code contains events which programatically sets a top link bar item on the global navigation bar on top of the site, and applies a cascading style sheet when the feature is activated. Notice the FeatureDeactivated event completely uninstalls all the feature components that the FeatureActivating event installs. This kind of cleanup is advised, since old feature components left after activation can cause issues.
The final component of interest is the web part feature code. For this example the code in the wpSiteList.webpart files is a simple web part that gives a list of sites. The feature project solution build requires some feature code for deployment and I put this in the "FEATURES/SampleFeature/WebParts" directory using a basic web part with some sample code to accomplish this.
The main method in the web part is the CreateChildControls() method. In this method the visual components and datasource for the web part are initialized. Notice that this methods internal code is setup similiar to a .aspx page_load event combined with a designer class initialize event, so that controls are created and added to the web part, and then a datasource is queried if the page is not a post back from the original page post.
To build your project once you have completed development you must have installed the Visual Studio plugin for WSPBuilder (located here). To create your solution file you will need to right click on your project, select WSPBuilder and the menu item "Build WSP". This builds a Sharepoint solution file in the root directory of your project.
Once you have built your solution file you will need to run some sort of installation script to install it to Sharepoint. Included in the downloadable example is a handy one I created. To use it you only need to change the variables thus:
The FEATUREGUID varaiable is taken from the feature.xml id attribute:
Once you have changed the variables in the script you can run this script and look in the Central Administration/Operations/Solution Management Page to see if errors occured or if the solution was deployed correctly.
If you ever want to uninstall the solution there is a script in the download for that as well, set the varaibles as you did in the install.cmd script:
Now lets see the feature in action. Once the solution is deployed, go to the "Site Collection Features" page on the site collection you deployed the site to and deactivate and the activate the feature.
A note here - to get this to work you have to go in after the install script has deployed and activated the feature and de-activate and re-activate the feature to get the "FeatureActivated" event to fire. Doesn't seem to work through activating the feature through the script.
Notice the "Send to Hello World" menu item. Click on this and the page HelloWorld.aspx is displayed with the path of the file: