Friday, April 15, 2011

Creating a SharePoint Solution Package (.wsp)

Step 1. Folders
First, create the folder structure for the solution. I used MySolution but you can give a name whatever you want. Two folders were created beneath MySolution - source and bin. First is for the compiled package, second – to keep the feature and the customized list. I use the same file structure for the feature as in SharePoint - one folder per feature.  Check out your SharePoint features hive at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\DiscussionsList and see how it’s organized.
 In the source folder I made the feature folder called MyList and 2 subfolders inside - List Templates and Messages.  In the end you will have something like this:
Step 2. Building a Feature.
Here we create our feature based on the SharePoint discussions list. Go to MyList folder and create a file feature.xml, where we reference 2 files, first – list manifest  MyListManifest.xml  and the second – schema.xml  which is describing the list metadata. And don’t forget to change the GUID of feature Id with your own!
<?xml  version="1.0" encoding="utf-8"?>
<Feature Id="A8E6B85F-D81A-4cc1-9708-D15FEF359DE2"
    Title="My Feature"
    Description="This is my feature containing a list"
      <ElementManifest Location="ListTemplates\MyListManifest.xml" />
      <ElementFile Location="Messages\schema.xml"/>
Copy the original schema.xml file from the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\DiscussionsList\Discuss folder to the Messages folder, and you have a list schema to start from, MyListManifest.xml we’ll create manually in the ListTemplates folder –
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="">
    DisplayName="My List"
    Description="This is my custom list based on the discussions list"
What is important here is that the Name attribute must have the same name as the folder where schema.xml is placed, Messages, as SharePoint will look for the schema.xml file at that location!
After the end of this step you have 3 new files as seen on the picture
Step 3. Building a Solution Package (.wsp)
SharePoint Solution Packages (wsp ) provide a great way to distribute all your customization in just one flask and the creation of the .wsp  ain’t so painful operation as you may think, I would say it is easy as hell! Everything starts from the traditional solution manifest .xml file in the Source folder:
manifest .xml
<?xml version="1.0" encoding="utf-8"?>
<Solution xmlns=""
          SolutionId="EC2EFD73-DBA2-4c0e-9C18-C8FC43F72E6C" >
    <FeatureManifest Location="MyList\Feature.xml"/>
Here, we reference our Feature.xml file that we had created before at the Step 2, and as you may guess it’s a good way to replace the the SolutionId GUID with something brand new J.  The next that should be created is the data definition file (.ddf). It’s a simple text file with the building instructions for the makecab utility because the .wsp package is nothing more than a cabinet file with the .wsp extension. (If you rename .wsp or .stp file to .cab you’ll be able to see its contents).  Let’s create a wsp.ddf in the Source folder:
.OPTION Explicit
.Set DiskDirectory1="..\bin"
.Set CabinetNameTemplate="MyListSolution.wsp"
; These directory names (DestinationDir) are used for the folders creation under 12\TEMPLATE\Features
.Set DestinationDir="MyList\ListTemplates"
.Set DestinationDir="MyList\Messages"
.Set DestinationDir="MyList"
In this file we set an output folder for the compiled package..\bin, its name MyListSolution.wsp and we tell makecab to include 4 files (marked in red) and create 3 folders at the deployment phase (in blue). Now it’s time to build everything into a single file, but before we do that I create a build.cmd file in the Source folder with some lines to facilitate the building process
 @cd %~dp0
 makecab /f wsp.ddf
Here I reference the wsp.ddf file with processing instructions which I created before. After you run it your solution package will appear in the bin folder:
Step 4. Solution deployment.
So, the solution package has just been created and the last thing that is left to do is to deploy it to our production server. I always like then the script is doing my job, so I put 2 .cmd files into the bin folder to deploy and retract my.wsp package so I don’t have to deal with stsadm utility from command line.
@set PATH=C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN;%PATH% @cd %~dp0
 stsadm.exe -o addsolution -filename MyListSolution.wsp
 stsadm.exe -o deploysolution -name MyListSolution.wsp -local
@set PATH=C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN;%PATH% @cd %~dp0
 stsadm.exe -o retractsolution -name MyListSolution.wsp –local
 stsadm.exe -o deletesolution -name MyListSolution.wsp

After the solution is deployed you can activate the feature in the Site features menu of you site and start using the new list!
Step 5. Using the feature.
Before our custom list can be used the feature containing it must be activated within the SPWeb scope.
After activation is done your custom list will appear on the site’s create page and you can create list instances!

No comments:

Post a Comment