Saturday, June 14, 2008

Harnessing Post-Build-Events in Visual Studio

In class library development, we need to add an assembly to GAC. One way is to open Visual Studio command prompt. And type the command for gacutil /i "DLL" or gacutil /u "DLL".
The disadvantage of the approach is that after every successful build we have to do it manually uninstall the prev version and install the correct version.

In my todays post, we will harness Visual Studio Post-Buil Events to automate this process.

On every successful build the DLL will be automatically added to the GAC.

To begin with,
1. Open any existing class library project to be deployed to the GAC.
2. Right click on the project, add new item-> Text File. Rename the file as PostBuildActions.bat
3. Paste the following content:

@ECHO OFF
SET DEPLOY=%3
ECHO DEPLOY: %DEPLOY%
:: Extract the deploye variable value from the command line
SET DEPLOYVALUE=DEPLOY
ECHO DEPLOYVALUE: %DEPLOYVALUE%

IF NOT %DEPLOY%==%DEPLOYVALUE% (ECHO Skipping deployment & GOTO QUIT)
SET TARGETDIR=%1
:: Fix the extra slashes the the path
echo %TARGETDIR:~0,-2%
:: Fix the extra slashes the the path
SET TARGETDIR=%TARGETDIR:~1,-2%
ECHO TARGETDIR: %TARGETDIR%
SET TARGETNAME=%2
:: Fix the extra slashes the the path
SET TARGETNAME=%TARGETNAME:~1,-1%
ECHO TARGETNAME: %TARGETNAME%
IF EXIST "%programfiles%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" (SET GACUTIL="%programfiles%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" & GOTO DEPLOY)
IF EXIST "%programfiles% (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" (SET GACUTIL="%programfiles%\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe") ELSE (ECHO Gacutil.exe could not be found! & GOTO QUIT)

:DEPLOY
ECHO.
ECHO Adding assemblies to the GAC...
ECHO.
ECHO.
%GACUTIL% -uf %TARGETNAME%
ECHO.
ECHO.
%GACUTIL% -if "%TARGETDIR%\%TARGETNAME%.dll"
ECHO.
:QUIT
ECHO.
ECHO Assembly added to GAC
ECHO.

4. Right click on solution-> Go to properties -> Build Events -> Post-build events command line and
Paste the following
call "$(ProjectDir)\PostBuildActions.bat" "$(TargetDir)" "$(TargetName)" DEPLOY

(Replace DEPLOY with NODEPLOY to skip adding to GAC)

Monday, June 2, 2008

Multi-Tenant Architecture in SharePoint 2007

What is Multi-Tenant Architecture (MTA)?

MTA is similar to database views where same data is given different representations. As an example, consider a company portal. When an employee logs in, he will be interested more in company happening. On the other hand, when HR person logs in he will be keen to see new buddy referral or other relevant content. MTA implementation provides such dynamism with single portal.

MTA implementation options in SharePoint 2007:

Audience:

Audience is group of users for which the content can be target. SharePoint 2007 audience can consist of Active Directory users or other compatible domain users. We can target list, web part, and navigation for audience. Problem with Audience: Although provide customized content on the fly, Audience cannot be used with master pages.
So if the requirement is to provide different master pages depending on users it is not possible. In our example, if we want to provide a different look-and-feel for HR and employee then it would not be possible.

Solution:

Our problem can be reframed as switching master pages at run-time on the fly. SharePoint 2007 is built on top of ASP.NET 2.0 framework. Thus, we can harness the ASP.NETHTTP request pipeline and change the master page on the PreInit event of Page. For this, we need to create HTTP module and change the master page on the fly by reading page path from web.config.

Steps:

1. Define a class that implements IHttpModule

2. In the PreRequestHandlerExecute method provide a handler for page_PreInit

3. Set page.MasterPageFile property. This can be read from config. It could be similar to /_catalogs/masterpage/BlackBand.master

4. Add the Module to GAC

5. Add the entry to safe-controls section of web.config

6. Add entry to the httpModules section of web.config

That’s it. We are done with MTA implementation.

In my next post, I will provide a walkthrough of creating the MTA module with Visual Studio .NET 2005.