Create WebPart in sharepoint

Introduction

In this article I am going to explain you that what is web part in share point and how to create it.

Definition:

A Web Part, also called a Web Widget, is an ASP.NET 2.0 server control which is added to a Web Part Zone on Web Part Pages by users at run time. The controls enable end users to modify the content, appearance, and behavior of Web pages directly from a browser. It can be put into certain places in a web page by end users, after developing by programmer.

SharePoint 2007’s portal infrastructure uses the ASP.NET Portal Framework classes as base classes. The heart of SharePoint development is in the WebPart class. the System.Web.UI.WebControls.WebParts.WebPart class is what you must derive from to develop web part.

Detail:-

I will take a sample program to display some text on sharepoint page using webpart.

To do so i will be using Render method. However we can also use CreateChildControl method.

Basically when we are rendering some html stuff then we should use Render method. But when you require to load some control like DropDownBox or control with events then we should use CreateChildControl method.

Sample Program:-

 

using System;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Microsoft.SharePoint;

namespace Home.Project.Solutions.WebParts
{
     // Display some text on sharepoint page
     public class MyDocs : System.Web.UI.WebControls.WebParts.WebPart
     {
         public DMyDocs()
         {
             this.ChromeType = PartChromeType.None;
         }
         protected override void CreateChildControls()
         {
	     // If you wish you can add text using below line also
             // this.Controls.Add(new LiteralControl("This is sample text"));
             base.CreateChildControls();
         }
         protected override void Render(HtmlTextWriter writer)
         {
             writer.Write("<h1>This is sample text</h1>");
             base.Render(writer);
         }
    }
}


Deploy web part using feature:-

To deploy web part using feature in sharepoint we need to create below 4 files:-

1. webpart c# class which we have created in sample program already.
2. .webpart file which includes webpart information.
3. feature xml file.
4. Element xml file.

See below solution structure which will explain you how to keep files and in which structure:-

solution structure


Note
: This solution has been created by using WSP builder template. So you will have “WSP Build” on right click of solution file. By doing this WSP Builder will take care to add safe control entry in web.config file. You need not to worry about. Just deploy this WSP, will work for you.
See Below Image which shows option to build WSP:-

wsp builder


.webpart file

 

<?xml version="1.0" encoding="utf-8" ?>
<webParts>
  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
    <metaData>
      <type name="Home.Project.Solution.WebParts.Doc, Home.Project.Solution, Version=1.0.0.0,
       Culture=neutral, PublicKeyToken=13c2a0e60a5fedb4" />
      <importErrorMessage>Cannot import Doc Web Part.</importErrorMessage>
    </metaData>
    <data>
      <properties>
        <property name="Title" type="string">Doc Web Part</property>
        <property name="Description" type="string">Display some text</property>
      </properties>
    </data>
  </webPart>
</webParts>


element.xml

 

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE">
    <File Path="Doc.webpart" Url="Doc.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="Custom WebParts"></Property>
      <Property Name="QuickAddGroups" Value="Custom WebParts" />
    </File>
  </Module>
</Elements>


feature.xml

 

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="1CE6BDAD-78b9-41c2-B7E3-D743E58E8965"
          Title="RBU Web Part"
          Description="Display Some Text"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Site"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml" />
    <ElementFile Location="Doc.webpart" />
  </ElementManifests>
</Feature>

Once WSP is ready; you can deploy this using stsadm commands.

Stasdm commands should be in below sequence
1. Retract Solution
2. Delete Solution
3. Add Solution
4. Deploy Solution

Detail of these commands can be view at below url
TechNet StsAdm Commands

 
Note: please visit my other article for How to create custom property for sharepoint web part.

Thanks!
Avinash

calendarApril 1, 2012 · cardInfoyen · commentsOne Comment
tagTags: , , , , , , ,  · Posted in: MOSS, SharePoint, WebPart

One Response

  1. miryam - June 29, 2012

    Great info. Thanks.

Leave a Reply

Spam Protection: , required

myworldmaps infoyen