Tuesday, June 19, 2007

Deploying a Webpart

This article will take you through three ways of deploying a webpart:
METHOD 1: Using a CAB File
METHOD 2: Placing DLL in Site's BIN
METHOD 3: Placing DLL in GAC
So, if we want to deploy a webpart, we first need to create one. For the sake of an example I will explain how to create a terribly simple webpart in Visual Studio 2005.
(I) Create a WebPart
We will create 3 WebPart Libraries for the sake of explination- WebPartLibraryMethod1, WebPartLibraryMethod2, WebPartLibraryMethod3. Each having only one webpart.
1)Create a WebPart Library Project (File->New Project->select Visual C#->select WebPart Library) Lets name the project and solution by the deploment method you chose to demonstrate, so if you are trying method 1, then the name WebPartLibraryMethod1. A WebPart is automatically created at the creation of the project. A WebPart is essentially a pair of files - a class file(.cs) and a Dashboard Web Part file(.dwp). Delete WebPart1.cs and WebPart1.dwp. Right click on the project and Add a Webpart. Name them as per the deployment method you would like to try, for eg, WebPartMethod1.cs . A .cs and a .dwp files of the same name will be created.
Before we proceed any further, ensure that:
i) .cs and .dwp files have the same name
ii) view .dwp file's properties, set 'Build Action' property to 'Content'.
iii) Manifest.xml file has the correct DWP file mentioned in the tag
2)Strong name your assembly. To do this, view the Project's Properties. In the Signing tab, choose a new strong name for the assembly.
3)Build the project.
4)Find the PublicKeyToken of the DLL created. You may use one of the following ways to find it:
METHOD 1:Open Visual Studio Command Prompt and type
sn -T DLL's path
Eg: sn -T "c:\Document and Settings\tingo\Visual Studio 2005\Projects\WebPartLibrary1\WebPartLibrary1\bin\debug\WebPartLibrary1.dll"
OR
METHOD 2:Use Lutz Roeder's Reflector. Install it, then, drag and drop the DLL into it to get the details of the DLL.


(II) Deploy the Webpart
METHOD 1: Using a CAB File
1)Edit the tag in the DWP file
2)Add a CAB Project to the Project's solution. (File->Add->New Project->Select Other Project Types->select Setup and Deployement-> select CAB Project)
3) Right click on the CAB Project, select Add,select Project Output. From the Project Output Group dialogue box, select both, Primary Output and Content Files. Click OK.
4)Rebuild the whole solution. This creates a CAB file in the CAB project's BIN.
5)Copy the CAB file to a location on the Sharepoint server, say, C:/
6)In the Command Prompt, navigate to the folder where stsadm.exe is located, which is, c:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/12/BIN. Now, type:
ststadm -o addwppack -globalinstall -force -filename CAB file's pathc
Eg: ststadm -o addwppack -globalinstall -force -filename "c:\WebPartLibCab.CA"

-END OF METHOD 1- Now, you may use this webpart in Sharepoint as you would any other. Check out for your self- Modify a site, Add a Webpart, you will find your webpart in the Webpart Dialogue box under Miscellaneous!

METHOD 2: Placing DLL in Site's BIN
1)Place the DLL in the _app_bin of the site.(To find its location, use the inetmgr)
2)Update the web.config file of the site with a tag for this DLL, as follows:
3)Reset IIS
4)Go to the Sharepoint site, go to Site Setting, under Galleries, select Webparts. Click New, select the webpart we just created and Add.
-END OF METHOD 2- Now you can use the webpart

METHOD 3: Placing DLL in GAC
1)Place the DLL in the GAC (c:\windows\assembly\)
2)Update the web.config file of the site with a tag for this DLL, as follows:
3)Reset IIS
4)Go to the Sharepoint site, go to Site Setting, under Galleries, select Webparts. Click New, select the webpart we just created and Add.
-END OF METHOD 3- Now you can use the webpart