Home > .Net, SharePoint > Deploying a WebPart Solution in SharePoint 2007 the simple way

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

Categories: .Net, SharePoint
  1. Jackie H
    July 22, 2007 at 2:02 pm | #1

    Thank you very much for the information. It clarify a couple of things for me :-)

  2. July 22, 2007 at 2:08 pm | #2

    Glad I could help :)

  3. Sub
    July 26, 2007 at 12:44 pm | #3

    Dude thanks for the info, that sure did help me :)

  4. July 31, 2007 at 9:26 am | #4

    good one.it helped me a lot.

  5. Kim
    July 31, 2007 at 5:06 pm | #5

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

  6. Yash Desai
    August 3, 2007 at 12:32 pm | #6

    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?

  7. jibran
    August 18, 2007 at 1:07 am | #7

    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?

  8. Hemnath.S
    August 21, 2007 at 10:00 am | #8

    Its awesome

  9. November 21, 2007 at 3:05 am | #9

    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?

  10. December 7, 2007 at 6:39 pm | #10

    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?

  11. Meera
    December 11, 2007 at 3:29 am | #11

    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

  12. January 9, 2008 at 10:52 am | #12

    I am getting this error

    Object Reference not set to an instance of an object

    The solution installtion failed.

    Please anyone help me in this matter

  13. sendil
    February 14, 2008 at 1:12 pm | #13

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

  14. February 23, 2008 at 10:01 pm | #14

    If you add this

    ren $(BuiltOuputPath) *.wsp
    del *.cab

    to the post build events you don’t have to rename it manually

    Great Article anyway :)

    Greetz
    Stefan

  15. Punit
    February 26, 2008 at 10:23 pm | #15

    Thanks. This helped! :)

  16. arpitha. P
    March 25, 2008 at 7:10 am | #16

    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?

  17. Sarah
    July 10, 2008 at 8:55 pm | #17

    Thank you so much for this article–it’s clear instructions were just the help I needed. Thanks!

  18. October 10, 2008 at 11:04 am | #18

    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!

  19. Mohammad Hijjawi
    October 13, 2008 at 1:27 pm | #19

    Thanks a lot for you useful info. :)

  20. imi
    November 19, 2008 at 11:31 pm | #20

    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

  21. Oliver
    December 16, 2008 at 8:12 pm | #21

    Wow! I never thought I would live to see the day where “Sharepoint” and “simple” would be in the same sentence! :|

  22. Ilia
    January 7, 2009 at 3:05 pm | #22

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

  23. Cacmd
    February 24, 2009 at 12:59 pm | #23

    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

  24. kevin
    February 25, 2009 at 8:11 am | #24

    Cacmd,I have the same error with you,
    how can resolve it?

  25. caohuydan
    September 11, 2009 at 9:00 am | #25

    after we add a solution to SharePoint, how do we add a webpart to SP? Anyone can help me about this? Thanks

  26. donna
    September 22, 2009 at 7:22 am | #26

    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?

  27. donna
    September 22, 2009 at 7:47 am | #27

    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?

  1. June 5, 2007 at 8:01 am | #1
  2. February 1, 2008 at 2:47 am | #2
  3. November 19, 2008 at 9:02 am | #3