Wednesday, January 24, 2007

Beginner's Sharepoint Custom webpart using Web Control Library

  1. VS 2005 -> File -> New Project ->Visual C# Projects -> Web Control Library.
  2. Name it as 'Simple Webpart' and Specify the project location.
  3. Add reference to Microsoft.Sharepoint.dll and System.XML

    (Solution explorer -> References -> right click -> Add Reference -> .NET -> search and select the name spaces above.)
  4. Add the following to the using section:

    using System.Security;
    using Microsoft.SharePoint.WebPartPages;
    using System.Xml.Serialization;
  5. Edit the AsseblyInfo.cs to preserve a fixed version each time you build.
    Replace the
    [assembly: AssemblyVersion("1.0.*")]so that it reads[assembly: AssemblyVersion("")]

  6. Add the following statement in the AsseblyInfo.cs at the end.

    [assembly: System.Security.AllowPartiallyTrustedCallers]
  7. Chnage the name space so that we can have any number of webparts under a single namespace.

    namespace SimpleWebPart
    namespace MyWebParts
  8. Change the class declaration to inherit from the webpart class.
    public class SimpleWebPart : WebControl
    public class SimpleWebPart : WebPart
  9. Replace
    protected override void Render(HtmlTextWriter output)
    protected override void RenderWebPart(HtmlTextWriter output)
  10. Add the code needed in the webpart, add necessary child controls you needed.
    Replace the code in the Render webpart method with
  11. Strong name the assembly
  12. Copy the dll to the 'bin' directory of the application.
    C:\Intepub\wwwroot\wss\virtual directories\port number
  13. GAC the assembly:
    Drag and drop the dll to 'C\Windows\Assembly
  14. Increase the Default Trust Level and Add a SafeControl Entry for your Web Part

    Open the web.config file in the Web application root(C:\inetpub\wwwroot\wss\virtual deirectories\port\web.config) -> In the level attribute of the trust section, change WSS_Minimal to WSS_Medium.
  15. Add the assembly to the safe controls.
    Within the web.config file opened above, locate the
    <safe controls >
    section and add the following:

    <SafeControl Assembly="SimpleWebPart, Version=, Culture=neutral, PublicKeyToken=PublicKeyToken" Namespace="MyWebParts" TypeName="SimpleWebPart" Safe="True"/>

  16. In the above line, MyWebparts is a namespace.
    SimpleWebPart is an assembly name.
    C:\Windows\Assembly\SimpleWebPart -> right click -> properties.
    you can find the key value from the following command.
    Open the visual studio command prompt and type:
    sn.exe -T C:\inetpub\wwwroot\wss\VirtualDirectories\PortNumber\bin\SimpleWebPart.dll
  17. Open a notepad and copy paste the following lines;
    <?xml version="1.0"?>
    <WebPart xmlns="; <Assembly>SimpleWebPart, Version=, Culture=Neutral, PublicKeyToken=PublicKeyToken</Assembly> <TypeName>MyWebParts.SimpleWebPart</TypeName>
    <Title>My Simple Web Part</Title>
    <Description>A simple Web Part</Description>

    Save the file as 'SimpleWebPart.dwp' and save at the bin folder, where the dll is copied
  18. Site Actions -> Site settings -> galeeries -> Webparts -> upload -> browse to the above .dwp file.
  19. Site Actions -> Edit Page -> Add Webpart -> drag and drop the webpart on the page.

No comments:

Post a Comment