TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint

Assuming I have a SharePoint site ready to go, I will be using My Site for Office SharePoint Portal Server 2003 for my example. (NOTE: I will be creating links in this document to screen shots to help clarify what I'm writing about).

 

  1.  Install the Web Part Templates for Visual Studio .NET
  2. Start Visual Studio and create a new C# Web Part Project named HelloWebPartLib as illustrated.
  3. Visual Studio will automatically create several files for you. Delete the files: WebPart1.cs and WebPart1.dwp.
  4. Add a Web Part class to the project named HelloWorld.cs as illustrated.
  5. Add a new Web Part DWP item to the project named HelloWorld.dwp as illustrated.
  6. In the Properties window for the DWP file, set the Build Action property to "Content". This will be needed later when you build a CAB file for deployment.


     
  7. Create a new key pair using sn.exe so you can give the HelloWebPartLib a strong name as illustrated.

    For example:  sn -k c:\keypair.snk
     
  8. Edit the AssemblyInfo.cs file to change the AssemblyKeyFile attribute to point to the key pair file you just created.

    [assembly: AssemblyKeyFile("c:\\keypair.snk")]
     
  9. In the HelloWord.cs file, edit the RenderWebPart method to write out "Hello World!"

    protected override void RenderWebPart(HtmlTextWriter output)

    {

          //output.Write(SPEncode.HtmlEncode(Text));

        output.Write("Hello World!");

    }


  10. Build the HelloWebPartLib project.
  11. Now that you have built the project, we need to extract the PublicKeyToken value of the assembly to specify in the HelloWorld.dwp file. I use Lutz Roeder's Reflector utility to pull this information, however you can use sn -T from the Visual Studio command prompt as well.


     
  12. Edit the HelloWorld.dwp file so that the XML entity named <Assembly> contains the PublicKeyToken value that you extracted using Reflector or sn.exe. Also, add the TypeName value as shown below.

    NOTE: Make sure you replace the highlighted public key token value in the example below with your own!
     

    <?xml version="1.0" encoding="utf-8"?>

    <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >

          <Title>Hello World</Title>

          <Description>Hello World WebPart</Description>

          <Assembly>HelloWebPartLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4a5727bcff5dd0bb</Assembly>

          <TypeName>HelloWebPartLib.HelloWorld</TypeName>

          <!-- Specify initial values for any additional base class or custom properties here. -->

    </WebPart>
     

  13. Edit the Manifest.xml file to specify the HelloWorld.dwp file in your project. Note that you can simply alter the existing text that reads "<DwpFile FileName="WebPart1.dwp"/>" and change it to the following:
     

    <DwpFile FileName="HelloWorld.dwp"/>
     

  14. Now we are ready to deploy our Web Part. The easiest way to do so is to create a CAB project.
  15. Right-click on your solution and then click Add New Project.
  16. As illustrated, create a Setup and Deployment CAB project named HelloWPCab.
  17. Right-click on the CAB project, point to Add, and then click Project Output.
  18. When the Add Project Output Group dialog box appears, specify "primary output" and "content files" and then click OK.


     
  19. Rebuild the entire solution. At this point a CAB file will have been created in the project directory you specified when creating the CAB project.
  20. To install our CAB file on the SharePoint server, copy the generated CAB file to the server. For this example, I am copying to the C:\ drive of the server.
  21. Once our CAB file has been copied over to the server, we can run the SharePoint command-line administration tool named stsadm.exe. This will deploy our CAB file containing the HelloWorld WebPart.

    NOTE: The stsadm.exe file on the server is in C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN\ by defult.
     
  22. As illustrated, run the stsadm.exe utility using the following parameters to install the HelloWorld WebPart. Note that you may need to change the highlighted path below depending on where you copied the CAB file on the server.

    stsadm -o addwppack -globalinstall -force -filename C:\HelloWPCab.cab
     
  23. As illustrated, now you can add your WebPart to a SharePoint portal page! In this case, I am adding it to the default page in "My Site". Do this by clicking on the Modify My Page link, point to Add Web Parts, and then click Browse.
  24. When you click on the Virtual Server Gallery link on the right window pane, you will see the Hello World Web part available for use.


     
  25. Drag and Drop your Hello World WebPart into one of the available zones as illustarted. That's it!



Digg!

posted on Sunday, December 05, 2004 1:43 PM

Feedback

# New Article - Creating an Deploying a 12/5/2004 3:50 PM TheChaseMan's Frenetic SoapBox

# re: HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint 12/6/2004 9:00 PM Patrick Tisseghem

Did you try the SmartPart?

# re: HOWTO:msn search WebPart for SharePoint portal server site 3/23/2005 12:54 PM SPNewbee

Hi !
Can any one tell me how I could add msn search to sharepoint portal server site?
I am new to this, and links or step by step examples will be greatly appreciates


thank in advance

# re: HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint 8/15/2005 12:38 PM Kaj Bonfils

Is there any way to include web.config entries into the cab file?

# re: HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint 2/4/2006 12:31 PM BimpnBerry

SPNewbee - follow this tutorial, but instead of outputing "Hello World" output the html for a msn searchbox. you prbly can get the html code from msn's website.

# re: HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint 2/17/2006 3:07 AM JackW

Hi,
I have one question: How I can make DLL for my WebPart.
It means that I have:
AssemblyInfo.cs
Lead-it UserAlerts.dwp
Leadit.SharePoint.Administration.csproj
Leadit.SharePoint.Administration.csproj.user
Leadit.SharePoint.Administration.sln
Leadit.SharePoint.Administration.suo
Manifest.xml
UserAlerts.cs
wppackager.xml

And inside the Mainfest.xml file I have direction to DLL file:

<?xml version="1.0"?>
<!-- You need only one manifest per CAB project for Web Part Deployment.-->
<!-- This manifest file can have multiple assembly nodes.-->
<WebPartManifest xmlns="http://schemas.microsoft.com/WebPart/v2/Manifest">
<Assemblies>
<Assembly FileName="Leadit.SharePoint.Administration.dll">
<SafeControls>
<SafeControl
Assembly="Leadit.SharePoint.Administration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bb3cb8ddab1d86c9"
Namespace="Leadit.SharePoint.Administration"
TypeName="*"
/>
</SafeControls>
</Assembly>
</Assemblies>
<DwpFiles>
<DwpFile FileName="Lead-it UserAlerts.dwp"/>
</DwpFiles>
</WebPartManifest>

Due to I would like to ask You again: How I can make this DLL, or what I should do to intall thi on SPP
Thank in advance

Jacek Wiecki

# re: HOWTO: Change the form size of asp page so that when displayed in the page-viewer web part, scroll-bars don't come. 3/22/2006 2:25 AM ajay sharma

please respond soon. Urgent

# re: HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint 8/9/2006 10:25 AM sara

Hi,

I have developed webpart on my local machine that has visual studio.net on windows XP. My sharepoint is installed on another web server. I did all the above steps.
But I am facing problem, when i am trying to deploy CAB file on server.
No web part found in configuration database.
would you please tell me where to run this command stsadm.exe on the local machine that has visual studio.net or on the server machine.
please reply back

# re: HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint 10/16/2006 3:23 AM Amitabh Srivastava

I need to deploy a webpart in the machine which contains some custom user control. Once I deployed this web part, got an error file can not be found. Please gice me its solution ASAP.
Please reply back.

# re: HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint 11/7/2006 6:56 AM Cenk

hi,
can somebody explain me the points 20 and 21 with details, i got some problems there!!!

# grr.... 4/21/2007 2:38 PM ekkis

broken feedback mechanims ate my comments!!!

# re: HOWTO: Create and Deploy a "Hello World" WebPart for SharePoint 4/21/2007 2:40 PM ekkis

I've installed the templates but when I try to use them I get the error "The specified module could not be found".

I'm running VS 2003 7.1.3088; SPS 2003 is running on a different box for which I've created a share to expose the Microsoft.Shreapoint.dll (required when installing the template).

help!

# Register Web Part | keyongtech 1/22/2009 2:47 AM Pingback/TrackBack

Register Web Part | keyongtech

# Import de Webpart | hilpers 1/22/2009 9:46 AM Pingback/TrackBack

Import de Webpart | hilpers