Saturday, January 1, 2011

Deploying webpart in SharePoint


We can deploy webpart to SharePoint site either deploying assembly to BIN or GAC.

Some steps are mandatory for this

Steps
1: Put assembly in web application BIN or WFE's (Web Front End Server) GAC
2: Register assembly as safe control in web.config of web application
3: Add .webpart file into webpart gallery

Let's see master's(Andrew Cornell) quote on this 3rd point
In order to make the Web Part discoverable, or enable users to pick the Web Part from a list of available Web Parts, a Web Part definition file must exist in one of two places: the Web Part Gallery in a top – level site of a site collection or the wpcatalog folder within the Web root of a site ’ s hosting Web application. If the Web Part definition is deployed to the wpcatalog folder, all sites within all site collections within the Web application will have access to the Web Part. However, if the Web Part definition is added to the Web Part Gallery, a special document library in the top - level site of a site collection, only the sites within that site collection will be able to add the Web Part to their pages.
In this post we will concentrate on deploying webpart only to BIN.

A) Manual Deployment

A.1: Add webpart assembly (<WebPartName>.dll) to Bin directory of web application
Web application path would be C:inetpubwwwrootwssVirtualDirectoriesPort_No
A.2: Add Safe control entry to web.config file of web application

Example.

<SafeControl Assembly="<WebPartName>" Namespace="<WebPartName>" TypeName="*" Safe="True" />
OR

<SafeControl Assembly="<WebPartName>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Namespace="<WebPartName>" TypeName="*" Safe="True" />

A.3: Add webpart to webpart gallery
Here we will not add any .webpart file to webpart gallery but this task will done by SharePoint itself. See how this is done.

Go to webpart gallery from "Site Actions". Click "New" and select your webpart assembly and populate it. This action actually create .webpart file for you webpart.
You can see this file content too. Click on "Edit" button in front of webpart in galley, then click "View XML" and save .webpart file. Open this file in notepad and see the xml markup.

A.4: Use webpart
Now open webpart page, edit it and put webpart in webpart zone.

B) Deployment by WSP package

B.1: Create solution for deployment
Create separate class library project in same solution which is totally a dummy project, we will not use it's assembly for deployment but it will help us to make deployment structure. Add one folder named "Defination" then copy your webpart assembly(DLL) there.

B.2: Create one text file and save it as manifest.xml, manifest file is actually a instruction file to STSADM utility which tells him where to place the files on server.
manifest.xml

<Solution xmlns='http://schemas.microsoft.com/sharepoint/' SolutionId='A54EF786-3131-4f6c-AF90-A7116DF2B814'>
<Assemblies>
<Assembly DeploymentTarget='WebApplication' Location='sandip.helloworldwebpart.dll'>
<SafeControls>
<SafeControl Assembly="sandip.helloworldwebpart" Namespace="sandip.helloworldwebpart" TypeName="*" Safe="True" />
</SafeControls>
</Assembly>
</Assemblies>
</Solution>
Above schema tells to STSADM that put my webpart assembly (sandip.helloworldwebpart.dll) into BIN folder of respective application and add safe control entry into web.config file of that application.

B.3: Create one text file and save it as inst.ddf, ddf(data definition file or diamond directive file) file is actually a instruction file to makecab utility which tells him how to create a package.
inst.ddf
 .Set CabinetNameTemplate="sandiphellowworldwebpart.wsp"
 .set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
 .Set CompressionType=MSZIP;** All files are compressed in cabinet files
 .Set MaxDiskFileCount=1000 ; Limit file count per cabinet
 .Set UniqueFiles='OFF'
 .Set Cabinet=on
 .Set DiskDirectory1="../Defination"
 manifest.xml
 sandip.helloworldwebpart.dll
Above schema tells to makebcab that create package of name “sandiphellowworldwebpart.wsp", put manifest file and assembly to root of
WSP package and save it into current directory.

B.4: Create WSP package
Open command line and go to Defination folder of your deployment project
C:Documents and Settingssandip.patilMy DocumentsVisual Studio 2008ProjectsWSSsandip.helloworldwebpartDeploymentDefination>
and run this command

makecab /f inst.ddf

Now you will get package in same folder. To confirm files in WSP package, copy/paste package(.wsp) file on same directory and change extension to .cab. Open cab file and see 2 files (manifest.xml, sandip.helloworldwebpart.dll) should be there.

B.5: Create deployment/retraction file
Deployment file will add package to solution store and deploy to respective application.
Retraction file will retract solution (delete webpart from BIN and safe control from your web application) and delete solution from solution store.

DeploySolution.cmd
 :begin
 @echo off
 set solutionName=sandiphellowworldwebpart
 set url=http://sandip:6666
 @set PATH=C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12BIN;%PATH%
 echo --- Adding solution %solutionName% to solution store...
 stsadm -o addsolution -filename %solutionName%.wsp
 echo --- Deploying solution %solutionName%...
 stsadm -o deploysolution -name %solutionName%.wsp -url %url% -immediate -allowCasPolicies -force
 stsadm -o execadmsvcjobs
 if errorlevel == 0
 echo ### Error deploying solution %solutionName%
 echo .
 goto end
 
RetractSolution.cmd

 :begin
 @echo off
 set solutionName=sandiphellowworldwebpart
 set url=http://sandip:6666
 @set PATH=C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12BIN;%PATH%
 echo --- Attempting to deactivate/retract existing solution...
 stsadm -o retractsolution -name %solutionName%.wsp -url %url% -immediate
 stsadm -o execadmsvcjobs
 stsadm -o deletesolution -name %solutionName%.wsp -override
 rem stsadm -o execadmsvcjobs
B.6: Deploy solution package and confirm it
Double click DeploySolution.cmd and see webpart assembly(DLL) in your web application BIN and safe control entry in web.config too. You can see there is minor difference in safe control entry which is placed by there by STSADM and our manifest file entry.

<SafeControl Assembly="sandip.helloworldwebpart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=48cc2196eaf8dbe7" Namespace="sandip.helloworldwebpart" TypeName="*" Safe="True" />

Don't worry about this change.

B.7: Add webpart to webpart gallery and use it.
When we deploy webpart using WSP package without feature, our .webpart file is placed into wpcatalog directory in the application.
Path could be C:inetpubwwwrootwssVirtualDirectoriesPort_No
This insure that our webpart will available in all site collections of the application and ready to use, Follow A.4 steps to use webpart on the pages.

B.8: (Optional)Retract solution package and confirm it
If you don't need this solution then retract it by running RetractSolution.cmd file.
Double click RetractSolution.cmd and see webpart assembly(DLL) in your web application BIN and safe control entry in web.config has been removed.

1 comment:

  1. Thank you for your energy to have put these things together on this blog. Emily and I very much appreciated your insight through your articles over certain things.

    ReplyDelete