Monday, October 20, 2008

Setup Batch File To Simplify Feature Deployment

Have you ever wanted to activate or install a feature on your server, but hated working with the STSADM commands? I know I do...so I create a batch file that will automate most of it for you.

To use it, just copy the code below and place it into a file called "setup.bat" (or you can call it something else as long as it has the ".bat" at the end.) Copy this batch file into all of your custom feature folders in your 12 hive (for example, 12\TEMPLATE\FEATURES\MyCustomFeature), then double-click to run it (note: you must be on the server--either logged in directly or remoted in for this to work.)

Internally, the batch file will analyze the folder it's in and use that as the feature that it runs operations against. Since STSADM's operations involving features have the ability to use the folder name instead of the feature name, this gives us a quick way to manipulate features in the 12 hive.

The batch file supports the following operations: install, uninstall, activate, deactivate, repair.

Install - Installs the feature to your SharePoint server. Uses the InstallFeature operation of STSADM
Uninstall - Uninstalls the feature from your SharePoint server. Note: the feature must be deactivated from all webs before uninstall works.
Activate - Activates an installed feature to a web on your server. If the feature is not installed, this command will error. You also must specify the URL of the web you want the feature activated on (for example: http://localhost, http://localhost:81, or http://myWSS)
Deactivate - Deactivates a previously activated feature on a web on your server. If the feature is not activated on the web specified, a message stating such will be displayed. You must specify the web URL that you want to deactivate.
Repair - Use the repair feature when you want to update a feature. It will deactivate, uninstall, reinstall, then reactivate the feature on your server. You must specify the web URL that you want the feature repaired on (If you have the feature activated on multiple web URLs, you must first deactivate the other URLs and then reactivate them after repairing.)

Below is the code for your batch file:

@ECHO OFF
REM -GET THE NAME OF THE FEATURE THAT THE BAT FILE RESIDES IN
echo ===================================================
cd %~dp0
set BatDir=%CD%
REM echo BatDir="%BatDir%"
pushd ..
call set FEATURENAME=%%BatDir:%CD%\=%%
popd
ECHO FEATURE: %FEATURENAME%
echo ===================================================

:SETMODE
SET /p SETUPMODE="SELECT MODE [INSTALL, UNINSTALL, ACTIVATE, DEACTIVATE, REPAIR, EXIT]: "

REM * DETERMINE SETUP MODE BASED ON USER INPUT
IF "%SETUPMODE%"=="INSTALL" (goto INSTALL)
IF "%SETUPMODE%"=="install" (goto INSTALL)
IF "%SETUPMODE%"=="UNINSTALL" (goto UNINSTALL)
IF "%SETUPMODE%"=="uninstall" (goto UNINSTALL)
IF "%SETUPMODE%"=="ACTIVATE" (goto ACTIVATE)
IF "%SETUPMODE%"=="activate" (goto ACTIVATE)
IF "%SETUPMODE%"=="DEACTIVATE" (goto DEACTIVATE)
IF "%SETUPMODE%"=="deactivate" (goto DEACTIVATE)
IF "%SETUPMODE%"=="REPAIR" (goto REPAIR)
IF "%SETUPMODE%"=="repair" (goto REPAIR)
IF "%SETUPMODE%"=="EXIT" (goto EXITBAT)
IF "%SETUPMODE%"=="exit" (goto EXITBAT)
ECHO ERROR: Setup mode "%SETUPMODE%" not recognized. Type "EXIT" to quit.
goto SETMODE

:REPAIR
set /p featureUrl="What url should this feature be activated on? "
ECHO Deactivating feature on %featureUrl%...
stsadm -o deactivatefeature -name %FEATURENAME% -url "%featureUrl%"
ECHO Uninstalling feature...
stsadm -o uninstallfeature -name %FEATURENAME%
ECHO Reinstalling feature...
stsadm -o installfeature -name %FEATURENAME%
ECHO Reactivating feature on %featureUrl% and restarting IIS...
iisreset
stsadm -o activatefeature -name %FEATURENAME% -url "%featureUrl%"
goto FINISHED

:INSTALL
stsadm -o installfeature -name %FEATURENAME%
REM * Add the template STP files
REM stsadm -o addtemplate -filename "ListTemplates/DMXList.stp" -title "DMX List" -description "A base list for DMX-related lists"
iisreset
goto FINISHED

:UNINSTALL
stsadm -o uninstallfeature -name %FEATURENAME%
iisreset
goto FINISHED

:ACTIVATE
set /p featureUrl="What url should this feature be activated on? "
stsadm -o activatefeature -name %FEATURENAME% -url "%featureUrl%"
goto FINISHED

:DEACTIVATE
set /p featureUrl="What url should this feature be deactivated on? "
stsadm -o deactivatefeature -name %FEATURENAME% -url "%featureUrl%"
goto FINISHED

:FINISHED
set featureUrl=
set SETUPMODE=
ECHO FINISHED!
goto SETMODE

:EXITBAT
SET FEATURENAME=


Happy Coding!!! I hope this save you time, too!

No comments: