Monday 19 September 2011

Deploying a WebPart Solution in SharePoint 2007 the simple way

Deploying web parts into MOSS 2007 isn’t exactly straight forward and after looking around the web for a while it became apparent that there is no ‘standard’ way for deploying them. I looked at a few different options and found that creating a Solution file using Visual Studio’s CAB Setup Project was the easiest and most reusuable way of accomplishing this task.
Below is a step by step guide to developing and deploying a simple WebPart.
First we need to create a WebPart to deploy, the easiest way I have found of doing this is to download the Visual Studio SharePoint extensions from Microsoft which can be located here.
After installing the extensions open Visual Studio and create a new project. You should now have some extra project templates under the SharePoint section.



Select ‘Web Part’ project, give it a name and click OK.
Visual studio will now create a basic Web Part class for you to edit.
My Web Part was called ClientViewerWebPart and I inserted some code into the overridden Render method which basically outputs data from a SharePoint list. This code looks like this so far:

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;


namespace ClientViewerWebPart
{
    [Guid("6bf05a67-118d-4cc4-80f7-b923be02773a")]
    public class ClientViewerWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        public ClientViewerWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }


        protected override void Render(HtmlTextWriter writer)
        {
            SPSite site = new SPSite(“http://leesbs/”);
            SPWeb web = site.OpenWeb();
            SPListCollection collection = web.Lists;
            SPList list = collection.GetList(new Guid(“{936518FF-CC2E-4BD9-ABB9-0580EA04BCD6}”),                         false);

            for (int index = 0; index < list.Items.Count; index++)
            {
                SPListItem item = list.Items[index];
                writer.Write(“<table>”);
                writer.Write(“<tr><td>Client Name</td>”);
                writer.Write(“<td>” + item["Client Name"].ToString() + “</td></tr>”);
                writer.Write(“</table>”);
            }
        }
    }
}
Next we need to tell SharePoint to allow this web part to be executed from a partially trusted location. We do this by making an entry into the web parts AssemblyInfo file.
In Solution Explorer Expand Properties and open up the AssemblyInfo.cs file. At the bottom of the file insert the following:
[assembly: System.Security.AllowPartiallyTrustedCallers()]
Now we need to add a manifest file to out Web Part project. This manifest file is what defines our solution and tells SharePoint everything it needs to know about our Web Part.
Add a new XML file to tthe project and rename it manifest.xml. Insert the following XML into the file:
<?xml version=1.0 encoding=utf-8 ?>
    <Solution xmlns=http://schemas.microsoft.com/sharepoint/
                    SolutionId={E3CF88A3-0EC3-49b9-B09E-5F84417EC6ED}>
        <Assemblies>
            <Assembly DeploymentTarget=WebApplication
                                  Location=ClientViewerWebPart.dll>
                <SafeControls>
                    <SafeControl Assembly=ClientViewerWebPart, Version=1.0.0.0, Culture=neutral,                                                                                         PublicKeyToken=3858ebd08dca7ee0
                                            Namespace=ClientViewerWebPart TypeName=*/>

                </SafeControls>
        </Assembly>
    </Assemblies>
</Solution>


You need to provide your own GUID value for the SolutionId attribute, you can do this within Visual Studio by using the GUID Generator under the Tools menu. You also need to enter your assemblies PublicKeyToken value which can be found by either using ILDASM.exe or Reflector.

The last thing we need to do to our Web Part project is make sure it has a strong name when it’s compiled, you can do this either using the SN.exe command line tool, or opening the project properties from within Visual Studio and navigating to the Signing section. From here you can create a new key file which will be used to strong name the assembly at compile time.

Now we have a fully working Web Part which can be compiled, what we need to do now is deploy this Web Part into our SharePoint site.

We are going to use a Visual Studio setup project to accomplish this, so add a new project to the same solution your Web part project is in, and create a CAB setup project. I called my setup project ClientViewerWebPartSetup.

Right click on the project and goto Add -> Project Output, from the ‘Add Project Output Group’ dialog box, select your Web Part project and then select ‘Primary Output’.

Repeat the step above but this time instead of selecting ‘Primary Output’ select ‘Content Files’.

After this is done you should have a solution that looks something like this:


Now you can compile your setup project, this will create a cab file containing the WebPart assembly and the manifest.xml file so all we need to do now to create our SharePoint solution file is rename our .cab file to .wsp.

We are now ready to deploy this solution file into SharePoint and we do this by using the STSADM.exe tool. This tool is located in your SharePoint installation directory under the bin folder mine was located here C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN. I strongly suggest you add this path to you PATH environment variable for ease of use.

You need to use the addsolution argument to the STSADM tool the first time you deploy a Web Part into your SharePoint site. When updating the Web Part you can use the upgradesolution argument.

The following is how I deployed my Web Part into my SharePoint site for the first time.


Great! Your Web Part is now deployed into your SharePoint site, or is it?

We need to do one more thing to make out Web Part available to our SharePoint site.

Open up Central Administration and navigate to ‘Operations’ under ‘Global Configuration’ click ‘Solution Management’. You should see your Web Part Solution sitting in the list with ‘Not Deployed’ as it’s status. Click on the Solution and click the ‘Deploy Solution’ button.

You should now be able to add your Web Part to any sites Web Part gallery.

Hope this guide has been useful, it’s quite a lengthly process do first time, but should get easier the more times you do it.

No comments:

Post a Comment