Pages Menu
Categories Menu

Posted by on 1st November, 2008

SharePoint Feature Deployment Tip.

SharePoint Feature Deployment Tip.

In a previous article, I have shown how to build a SharePoint Feature and here is just a trick that could save you quite some time…

The issues is that when developing a SharePoint features (or any other component), you end up writing, compiling and testing your code many times over. The code you are writing is a separate component that, essentially, has to be taken from Visual Studio, registered in the GAC, plugged into SharePoint and then manually activated.

Then you realize something isn’t working, you make a change to your code, compile and you end up with a new component, but you can’t try it, before turning off and “unplugging” the component that is currently plugged into SharePoint. And that is just the class…if you make changes to anything inside the Features folder, you have to remember to upload that as well…

In other words, it requires several manual steps outside of the development environment that could consume a lot of time.

To make it easier for myself, I have included all those steps as command line commands into Visual Studio, so that when I build my code, it automagically does all of it behind the scenes and out of my way.

Like it? follow me:

Double click on your “Properties” folder in your Solutions Explores (or My Project in VB.NET)

SharePoint Feature

The corresponding dialog comes up (in a VB.NET project you have to click one more button)…

SharePoint Feature

So, the first thing we want to do is to run some commands to deactivate and uninstall the feature from SharePoint and then unregister it from the GAC (not strictly necessary, but just in case…).

Click on the Edit Pre-Build button and you should be presented with a dialog.

SharePoint Feature

You can use the Macros button to insert the variables or you can just copy the code below (pay attention to substitute the -url parameter with your own, everything else, should be pretty much the same).

Pre-build event command

cd "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN"

stsadm -o deactivatefeature -name $(ProjectName) -url http://sharepoint/sites/sandbox/
stsadm -o uninstallfeature -name $(ProjectName) -force

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\GacUtil.exe" /u $(TargetName)

Your dialog should look like this… and you can click OK when done.

SharePoint Feature

Again, we are doing three things here, before we compile any code, we:
[list style=”arrow”]

  • Deactivate the Feature
  • Unistall the Feature
  • Unregister from GAC
[/list] Ok, now let’s look at the Post Build event… click on the Edit Post Build…

SharePoint Feature

And fill in the following code, substituting the -url parameter and the appPool name you want to recycle:

Post-build event command

"C:\[...]\Microsoft Visual Studio 8\SDK\v2.0\Bin\GacUtil.exe" -i "$(TargetPath)"

cd "$(ProjectDir)"
xcopy "12" "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\" /ys

cd "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\BIN"

stsadm -o installfeature -name $(ProjectName) -force
stsadm -o activatefeature -name $(ProjectName) -url http://sharepoint/sites/sandbox/

%windir%\system32\cscript.exe c:\windows\system32\iisapp.vbs /a "[SPAppPool]" /r

Your Editor should look like this:

SharePoint Feature

So, here we are doing a few more things, all happen after we compile our code; we:
[list style=”arrow”]

  • Copy (overwrite) our Feature folder to the 12 Hive
  • Register our dll in the GAC
  • Install the Feature
  • Activate the Feature
  • Recycle the SharePoint Application Pool
[/list] Now, when you build you can view the progress of any of these commands, by nesuring you have the Output View (View > Output) open in Visual Studio 2008.

SharePoint Feature

…build your code and view the output window….

SharePoint Feature

In essence, and to conclude, we can make any change anywhere; to our feature, to our classes and then build as usual and Visual Studio 2008 handles everything for us.

Once finished building, all you will need to do is to refresh your browser and test your newly compiled and installed SharePoint Feature…