10.17.2011

Quick Hello World Web Part

This is a quick hello world web part. I am assuming your visual studio is already setup on your SharePoint server.
Please see this post if you need help with setting up your environment.

  1.  Launch Visual Studio 2008.
  2. Select File --> New --> Project.
  3. Select C# under Project Type.
  4. Select Web Part under Visual Studio Installed templates.
  5. Type HelloWorld in the Name textbox.
  6. Select a location in your hard drive.
  7. Click on the OK Button to create the HelloWorld web part.


  8. Right-click on the Reference foler in the Solution Explorer and the select Add Reference...


    Add the following references by going under the .NET table and then select the namespace and click OK:
    (This is NOT required for this HelloWorld tutorial. But I included this step so that next time if you need to add a reference, you know how to?)
    • System.Data.LINQ
    • Microsoft.SharePoint
      (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll)


  9. Open “AssemblyInfo.cs” from properties folder.

  10. At the very end of the file enter the following code:
    [assembly: AllowPartiallyTrustedCallers]   
    And make sure to add the following namespace
    using System.Security; 


Basic Setup
I always like to setup my class names in the web part first so that I can recognize it later within my SharePoint.

Open WebPart1.cs



Replace "WebPart1" from the line "public class WebPart1 :" with "MyHelloWorld" and replace "public WebPart1()" with " public MyHelloWorld()"
 


So far your WebPart1.cs should look like:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace HelloWorld
{
    [Guid("d862a16d-5319-4687-9cb9-5e6c02ed704a")]
    public class MyHelloWorld : System.Web.UI.WebControls.WebParts.WebPart
    {
        public MyHelloWorld()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();


            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
        }
    }
}
Writing the Code
In this web part we will make a simple "DIV" tag and inside that tag we will write hello world. But in order to use the html tags and controls, we need to add a namespace reference. So add the following line:



using System.Web.UI.HtmlControls;
Inside the "protected override void CreateChildControls()" function, write the following code:
            HtmlGenericControl MyDiv = new HtmlGenericControl("div");
            MyDiv.ID = "MyDivision";
            MyDiv.InnerHtml = "Hello World";
That's it! Your final code should look like:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.HtmlControls;

namespace HelloWorld
{
    [Guid("d862a16d-5319-4687-9cb9-5e6c02ed704a")]
    public class MyHelloWorld : System.Web.UI.WebControls.WebParts.WebPart
    {
        public MyHelloWorld()
        {
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            HtmlGenericControl MyDiv = new HtmlGenericControl("div");
            MyDiv.ID = "MyDivision";
            MyDiv.InnerHtml = "Hello World";
            this.Controls.Add(MyDiv);
            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
        }
    }
}


Deploying the web part
Now we need to deploy our web part.
  1. Build the solution

  2. If no error returned by the compiler, copy the DLL from your Birthday project bin > Debug directory to the bin directory of your WSS site (C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin).



  3. Go to (C:\inetpub\wwwroot\wss\VirtualDirectories\80\) and modify the Web.Config file for the WSS site to declare the custom web part as a safe control by adding the following code within the <SafeControls> tag.
 <SafeControl Assembly="HelloWorld" Namespace="HelloWorld" TypeName="*" Safe="True" />



Now we can walk through, How to insert this web part in our SharePoint Page?
  1. Go to your root SharePoint site and from there Site Actions -> Site Settings -> Modify All Site Setting.


  2. Under “Galleries" tab click "Web Parts" link.


  3. Add a new web part, Click New menu in web part gallery Page.


  4. Select the newly added web part in the "New Parts Page" and click on "populate gallery"


  5. If the import is successful, you will see the web part in Web part gallery list.

Now we see How to insert into a SharePoint Page?
  1. Go to site under which you previously created the Birthday List. Go to on Site Actions > Create Page
  2. Create a blank web part page and name it HelloWorldPage
  3. Once the page is created, click on "Add a Web Part"
  4. From the web part list, locate the one we just created and add.
Running the web part
If all went well in previous steps, you should see hello world.

happy programming!

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

 
POPULAR POSTS
TAG CLOUD