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.

 

kick it on DotNetKicks.com

42 thoughts on “Deploying a WebPart Solution in SharePoint 2007 the simple way

  1. Pingback: Alpesh Nakars’ Blogosphere » SharePoint Link: June 05 2007

  2. Thanks! Awesome explaination for installing a web part! I definitely think this will be useful when I get my pre-built web part installed 🙂

  3. Hey, what if I want to add more than one webpart together in one solution? Do I have to follow the same rules as above and just add the required cs files for the webParts and add an entry for each one in the manifest.xml?

  4. Thanks for the great article. I am able to deploy the web part successfully, however, I get the following error when I add it to any page.

    The “ProgramFilter” Web Part appears to be causing a problem. Request for the permission of type ‘Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ failed.

    I can fix this problem in my DEV environment by setting the trust level in the web.config to WSS_Medium. The live server is unfortunately is shared hosting. I have a funny feeling they won’t be changing the trust level for me.

    How should i proceed?

  5. Thanks for the great article. Just one question, how do you go about upgrading your solution? If I update the version number in the assembly and manifest and then upgrade using ‘upgradesolution’ then all the exiting webparts in my pages fall over and have to be manually replaced with the new version. Can this be done automatically?

  6. I’m having trouble getting this setup, the Visual Studio SharePoint extensions wont install without WSS 3.0 installed… I dont have WSS3.0 installed on my workstation and dont really want to, is there any way around this?

  7. Hi,
    I’m trying to develop a webpart with a GridView(SPGridview ) which displays information retrieved from a list.There is also a template field containing checkboxes to select the Grid’s rows.Can anyone tell me how the rows can be selected and the values in each column retrieved to perform such operations such as edit/delete.
    Thanx in advance

    • Hello,
      for “error:Object reference not set to an instance of an object.” in “Deploy Sharepoint Webpart”, please tell you solution. I have the same i cant solve this problem.
      Thanks,
      Ragims

  8. Pingback: My SharePoint Links « Troy McDaid’s Weblog

  9. Its fabulous post….. chanceless………
    I need to clarify one thing how to add user controls(.ascx,.. files) in the .wsp file

  10. I am getting an error:Object reference not set to an instance of an object.
    When i try to complie the solution. How do i over come it?

    • Hello,
      if you have a solution for “error:Object reference not set to an instance of an object.” in “Deploy Sharepoint Webpart”, please tell it me how you sold a problem. I have the same i cant find solution.
      Thanks,
      Ragims

  11. thank you! Great article and solved my problem. But sometimes the deployed webparts can not be seen in the solution management list. If this happens you should reset your IIS. Afterwards your webpart should be seen there. Note that, you can reset IIS by start menu/run iisreset command.

    Therefore i still have lots of problems in sharepoint!

  12. Pingback: Sharepoint: Create and deploy webpart « Paresh’s Blog

  13. I am getting the following error

    The “ClientViewerWebPart” Web Part appears to be causing a problem. Request for the permission of type ‘Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ failed

  14. imi,
    you need to change your trust level in web.config or copy the assembly dll to the GAC, and also, in two cases, add your webpart as SaveControl in web.config (normally, it have to be made during installation, just to check it).

  15. Did anyone get this error when they tried to add the webpart?
    “Value does not fall within the expected range. ”
    Is there anyway we can view/test the webpart before deploying into Sharepoint as I can’t figure out what I’m doing right!

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: Value does not fall within the expected range.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [ArgumentException: Value does not fall within the expected range.]
    Microsoft.SharePoint.SPFieldCollection.GetField(String strName, Boolean bThrowException) +471
    Microsoft.SharePoint.SPListItem.GetValue(String strName, Boolean bThrowException) +120
    Microsoft.SharePoint.SPListItem.GetValue(String strName) +54
    Microsoft.SharePoint.SPListItem.get_Item(String fieldName) +51
    ClientViewerWebPart.ClientViewerWebPart.Render(HtmlTextWriter writer) +636
    Microsoft.SharePoint.WebPartPages.SPChrome.RenderPartContents(HtmlTextWriter output, WebPart part) +80

    [WebPartException: Value does not fall within the expected range.]
    Microsoft.SharePoint.WebPartPages.SPChrome.RenderPartContents(HtmlTextWriter output, WebPart part) +238
    Microsoft.SharePoint.WebPartPages.SPChrome.RenderWebPart(HtmlTextWriter output, WebPart part) +100
    Microsoft.SharePoint.WebPartPages.WebPartZone.RenderZoneCell(HtmlTextWriter output, Boolean bMoreParts, WebPart part) +254
    Microsoft.SharePoint.WebPartPages.WebPartZone.RenderWebParts(HtmlTextWriter output, ArrayList webParts) +771
    Microsoft.SharePoint.WebPartPages.WebPartZone.Render(HtmlTextWriter output) +1449
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240
    System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +42
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240
    System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +253
    System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +87
    System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +53
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240
    System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +42
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240
    System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +240
    System.Web.UI.Page.Render(HtmlTextWriter writer) +38
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4240

  16. hi. i do not have this part.

    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’.

    any idea why?

  17. hi. i typed out the line in cmd. and there was an error.

    ‘stsadm’ is not recognised as an internal or external command, operable program or batch file.

    why is this so?

  18. I got Error after packing My Webpart Solutions.I got Error At stsadm add solution
    steps.I got,Error was The .wsp has unsupported extensions hence my soln was not deploy so if u have soln for this then tell me.Also i had got .osd file in .wsp file.

  19. Thank you very much for the info. The part where I need to deploy the web part through central administration really helped.

  20. Pingback: Aidan Garnish | Deploying a WebPart solution in MOSS 2007 the easy way

  21. Pingback: Name of the blog | Deploying a WebPart solution in MOSS 2007 the easy way

  22. Nice article. My first time building a web app.

    Can you explain what the writer.Write() function does, where does it write to?

    When I try to compile I get “The name ‘writer’ does not exist in the current context” error. Why? Can I use some other function?

    Cheers

  23. if you have a solution for “error:Object reference not set to an instance of an object.” in “Deploy Sharepoint Webpart”, please tell it me how you sold a problem. I have the same i cant find solution.

  24. Pingback: Deploying custom webparts in SharePoint 2007 | asphive

Leave a reply to sendil Cancel reply