Wednesday, April 17, 2013

Creating a custom web part in SharePoint : SharePoint Tutorial #1

Web part is a server control that is added in SharePoint site to perform some operations on the run such as displaying a list, displaying a web page, adding content to the site, etc.  A web part is added to a web part zone present on the particular web page. 

While there are a number of predefined web parts in SharePoint that can be used out of box; we can create our own custom web parts to perform operations that are not possible with existing web parts.

  Following are the steps to create a custom web part:

1.    Create a class file to write web part code:


a)    Create a new Visual Studio project say ’MyTestWebpart’ and add a class file say ‘MyTestWebpart’ to it.  After this add ‘Windows SharePoint Service’ reference to this project (Right click on project, select add reference, select the required dll under .NET components).

b)    Inherit the web part class(Mircosoft.SharePoint.WebPartPages.WebPart)  and ‘System.Web’ in ‘MyTestWebpart’

c)    Over ride ‘CreateChildControl ‘ and ‘RenderControl’ method in the above class. ‘System.Web’ class has to be inherited as both these method are in ‘System.Web’ Class.

 ‘CreateChildControl’ method is to create and initialize objects. ‘RenderControl’ method is used to render the controls created in the ‘CreateChildControl’ method.
If you are not creating any control and just have to write something using web part, you can use the ‘Render’ method. In this ‘Writer.write’ can be used to display the required info.

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.Text;

namespace MyTestWebpart
{
    public class MyTestWebpart:Microsoft.SharePoint.WebPartPages.WebPart
    {
       

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write("Hello World");

        }

    }
}


2.    Sign the project with strong key:

Sign the project with a strong key name. Right click on the project, Select properties, Go to Signing tab. Under Signing tab-> Choose a strong key file->New key.  Enter the new key name say ‘MyTestWebpart’, uncheck the password option and click ok. Save and close.

3.    Deploying the web part dll:

Open Assembly( Type assembly in run and hit enter) and deploy the project dll (in this case ‘MyTestWebpart.dll ) in the Assembly( drag and drop the dll in assembly).

4.    Create a dwp file:  (DWP is the webpart extension to be uploaded in the webpart gallery)

Add an xml file to the project and rename the extension as dwp. Add the required info to below tags.

<?xml version="1.0" encoding="utf-8" ?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
  <Title>MyTestWebpart</Title>
  <Description>This is a Test Webpart</Description>
  <Assembly>MyTestWebpart, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=9f4da00116c38ec5</Assembly>
  <TypeName>MyTestWebpart.MyTestWebpart</TypeName>
  <!-- Specify initial values for any additional base class or custom properties here. -->
</WebPart>

<Assembly >name should be the name of the project dll. Rest of the assembly details such as version , culture and public key token can be obtain from the properties of the particular dll in the assembly.
<Type name > should be in the form of ‘AssemblyName.ClassName’.

5.    Add Safe Control Entry in application web.config file:

Open the web.config of the application(C:\Inetpub\wwwroot\wss\VirtualDirectories\)  and add a safe control entry for this webpart.

    <SafeControl Assembly="MyTestWebpart, Version=1.0.0.0, Culture=neutral,                   PublicKeyToken=9f4da00116c38ec5"Namespace="MyTestWebpart"
    TypeName="MyTestWebpart" Safe="True" />


6.    Upload the dwp file in the web part gallery:

Click on Site Settings, Select web part under ’Gallaries’.  Next, click on upload and upload the dwp file of this webpart.


7.    Reset IIS/ Application pool


8.    Add the custom web part to required web page

No comments:

Post a Comment