Thursday, September 27, 2007

Create and deploy a Web Part solution package

To create and deploy a solution package is never easy. I tried a whole day to figure it out the whole sucessful process and had to do a brunch of solution retraction. Now, I will try to put the whole process in details:

1. Create the web part derived from System.Web.UI.WebControls.WebParts.WebPart

Add Reference System.Web and Microsoft.SharePoint

using System;
using System.Collections.Generic;
using System.Text;
 
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
 
namespace CusLib
{
 public class CusViewer : System.Web.UI.WebControls.WebParts.WebPart
 {
 public CusViewer()
 {
 this.ExportMode = WebPartExportMode.All;
 }
 
 protected override void Render(HtmlTextWriter writer)
 {
 SPSite site= new SPSite("http://wcm.litwareinc.com");
 SPWeb web = site.OpenWeb();
 SPListCollection collection = web.Lists;
 foreach (SPList list in collection)
 { 
 writer.Write("<table>");
 writer.Write("<tr><td>List Name</td>");
 writer.Write("<td>" + list.Title+ " " + list.ID + "</td></tr>");
 writer.Write("</table>");
 }
 }
 }
}
 

2. Add the following line of code to the end of the AssemblyInfo.cs file to tell the .NET Framework that assemblies that aren't fully trusted can call & execute the Web Part assembly:

[assembly: System.Security.AllowPartiallyTrustedCallers()]

3. package.ddf: defines content and structure in the wsp (solution package) file

.OPTION Explicit
.Set DiskDirectoryTemplate=CDROM
.Set CompressionType=MSZIP
.Set UniqueFiles=Off
.Set Cabinet=On
.Set DiskDirectory1=Package
;**************************************************
manifest.xml

bin\debug\CusViewer.dll

.Set DestinationDir=CusViewerSolution
TEMPLATE\FEATURES\CusViewerSolution\feature.xml
TEMPLATE\FEATURES\CusViewerSolution\elements.xml
TEMPLATE\FEATURES\CusViewerSolution\cusWebPart.webpart

;***End

4. manifest.xml: Defines the list of features, site definitions, resource files, web part files, and assemblies to be included in the solution package. In this case, it defines the location of feature.xml and the safe control line which is going to be put in the web.config file.

<?xml version="1.0" encoding="utf-8" ?>
<Solution SolutionId="F174BCD9-F149-4583-859C-55DE086DD0D7" 
 xmlns="http://schemas.microsoft.com/sharepoint/">
 
 <FeatureManifests>
 <FeatureManifest Location="CusViewerSolution\feature.xml" />
 </FeatureManifests>
 
 <Assemblies>
 <Assembly DeploymentTarget="WebApplication" Location="CusViewer.dll">
 <SafeControls>
 <SafeControl Assembly="CusViewer" Namespace="CusLib" TypeName="*"/>
 </SafeControls>
 </Assembly>
 </Assemblies>
</Solution>
 

5. feature.xml: Defines feature details like the title, scope and the files that support this feature. In this case, the elements and web part definition file location.

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
 Id="490A5E8A-A639-4743-B5AA-D21B485E3C9F"
 Title="Custom Viewer"
 Hidden="FALSE"
 Scope="Site"
 Version="1.0.0.0">
 <ElementManifests>
 <ElementManifest Location="elements.xml" />
 <ElementFile Location="cusWebPart.webpart" />
 </ElementManifests>
</Feature>
 

6. elements.xml: Contains definitions to the feature's elements.

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
 <Module Url="_catalogs/wp" RootWebOnly="TRUE">
 <File Url="cusWebPart.webpart" Type="GhostableInLibrary">
 <Property Name="Group" Value="CusLib" />
 <Property Name="Title" Value="Cus View Web Part" />
 </File>
 </Module>
</Elements>

7. cusWebPart.webpart: web part definition. The 2nd parameter in type name is confusing. It should be the namespace from the source of the Internet. But I check the existing definitions in the web parts library, it uses the assembly name instead. The Title property is useless. SharePoint uses the Title defined in the elements.xml instead.

<?xml version="1.0" encoding="utf-8" ?>
<webParts>
 <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
 <metaData>
 <type name="CusLib.CusViewer, CusViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
 <importErrorMessage>Error importing the Web Part.</importErrorMessage>
 </metaData>
 <data>
 <properties>
 <property name="Title" type="string">Custom Viewer Web Part</property>
 </properties>
 </data>
 </webPart>
</webParts>

8. build.targets: Defines the makecab details which includes the location of ddf file and where to put the wsp file.

<?xml version="1.0" encoding="utf-8" ?>
<Project DefaultTargets="BuildSharePointPackage" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <Target Name="BuildSharePointPackage">
 <Copy SourceFiles="$(TargetPath)" DestinationFolder="bin" ContinueOnError="false"/>
 <Exec Command="makecab.exe /F package.ddf /D CabinetNameTemplate=$(TargetName).wsp" />
 </Target>
</Project>

9. unload the project and edit the project property. Add the following code at the bottom. It tells the project the location of build.targets and which target to call.

 <Import Project="build.targets" />
 <Target Name="AfterBuild">
 <CallTarget Targets="BuildSharePointPackage" />
 </Target>
 

10. build the solution and you will find the CusViewer.wsp generated under Package folder

11. add the solution package to SharePoint:

stsadm -o addsolution -filename C:\DevProjects\CusViewer\CusViewer\Package\CusViewer.wsp

12. open SharePoint 3.0 Central Administration. goto Central Administration > Operations > Solution Management to deploy the solution.

13. open the portal you want to use this web part. click Site Actions > Site Settings > Modify All Site Settings > Site Collection Features. Activate the web part solution with the Title defined in the feature.xml

14. edit a page and add the web part with the group and title defined in the elements.xml

15. if for somehow the web part solution package is not working properly and you want to redeploy it, you will need to do the following:

  • delete the web part in the web parts library
  • deactivate the feature in features library
  • detract the solution in central administration
  • remove the solution
  • follow the steps starting from #10

16. If you want to places the files in the file system other than in the DB, you will need to define it in the package.ddf file and remove them from feature.xml and elements.xml.

Reference:
  Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0
  Creating a Solution Package in Windows SharePoint Services 3.0
  Anatomy of a SharePoint WSS v3 Feature Project in Visual Studio 2005

No comments: