Archive for April, 2008

Creating a Sharepoint webpart

Posted: April 15, 2008 in Webpart


Starting a Web Part Project Web Parts are based on ASP.NET Web Form Controls. You create Web Parts in Microsoft Visual C# or Microsoft Visual Basic by using the Web Part Library template in Microsoft Visual Studio 2005. The Web Part templates can be downloaded from MSDN. After you download and install these templates, you can proceed with creating a Web Part using C# as described in this topic.

To create a new Web Part Library project

1) Start Visual Studio.
2) On the File menu, point to New, and then click Project.
3) In the New Project dialog box, click Visual C# Projects, and then select the Web Part Library template.
4) Type SimpleWebPart as the name and specify the location for the project files, and then click OK.

To add a reference to Microsoft.SharePoint.dll to a project on a computer running Windows SharePoint Services

1) On the Project menu, click Add Reference.
2) On the .NET tab, double-click Windows SharePoint Services.
3) Click OK.

To add a reference to Microsoft.SharePoint.dll to a project on a computer not running Windows SharePoint Services

1) On the Project menu, click Add Reference.
2) On the .NET tab, click Browse, and then navigate to C:\inetpub\wwwroot\bin (or the folder to which you copied Microsoft.SharePoint.dll). Select Microsoft.SharePoint.dll, and then click Open.
3) Click OK.

To add a reference to System.Xml.dll to a project

1) On the Project menu, click Add Reference.
2) On the .NET tab, double-click System.Xml.dll.
3) Click OK.

Strong-Naming the Assembly

Web Parts are designed to be distributed over the Internet or an intranet. For security reasons when creating a custom Web Part, you should strong-name it to ensure that the part can be trusted by your users. A strong name consists of the assembly’s identity plus a public key and a digital signature. You can use the Strong Name tool (sn.exe) to manage keys, generate signatures, and verify signatures. This tool is installed with the .NET Framework SDK when you install Visual Studio 2005.
1) Create a file containing the key pair. From a command prompt on the drive where you installed Visual Studio 2005, type the following: cd \Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\
2) Type the following: sn.exe -k C:\keypair.snk
3) In Solution Explorer, double-click AssemblyInfo.cs in your project and edit the following line:
[assembly: AssemblyKeyFile(“”)]
so that it reads:
[assembly: AssemblyKeyFile(“C:\\keypair.snk”)]

After this you have to hard code the webpart as per your need.

Registering Your Web Part as a SafeControl As a security measure, Windows SharePoint Services requires you (or a server administrator) to register the Web Part’s assembly and namespace as a SafeControl in the web.config file of the server.

To register a Web Part assembly as a SafeControl

1) Open C:\inetpub\wwwroot\web.config in Visual Studio 2005 or Notepad.
2) Add the following lines in the block:
3) Replace the PublicKeyToken value (def148956c61a16b) with the actual value for your Web Part’s assembly. To determine the correct PublicKeyToken value for the Assembly attribute of the tag for your Web Part, use the sn.exe command-line utility:
sn.exe -T C:\inetpub\wwwroot\bin\SimpleWebPart.dll
You can also determine the correct PublicKeyToken value for your Web Part assembly, by performing the following workaround:

  • In Windows Explorer, open the C:\inetpub\wwwroot\bin folder.
  • In a separate window, open the C:\Windows\Assembly folder. (C:\Windows\Assembly exposes the contents of the global assembly cache.)
  • Drag your Web Part assembly (your sample from these procedures should be named SimpleWebPart.dll) from the C:\inetpub\wwwroot\bin folder into the C:\Windows\Assembly folder.
  • In the C:\Windows\Assembly folder, right-click your assembly, and then click Properties. Copy the PublicKeyToken value, and then paste it into the tag for your assembly.
    In the C:\Windows\Assembly folder, right-click your assembly, and then click Delete.

Creating a Web Part Definition File (.dwp)

A Web Part Definition file (.dwp) file is a simple XML file that contains property settings for a single Web Part. To import your Web Part into a Web Part Page, simply upload the .dwp file. After uploading the Web Part, you can display the Web Part by dragging it into one of the zones of the Web Part Page. Two properties are required in the .dwp file: Assembly and TypeName. However, to display a default name and description for the Web Part after it is imported, you should also include the Title and Description properties. If you want to set other Web Part properties during import, you can also define them in a .dwp file. A .dwp file takes the following form:



Now go to your website and click edit page and then add web parts

  • In the Add Web Parts dialog box, click Advanced Web Part gallery and options in the lower right corner.
  • In the Add Web Parts pane, click Import.
  • Browse to the location of the SimpleWebPart.dwp file, and then click the Upload button. After uploading, the page will refresh, and “My Simple Web Part” should be displayed under Imported Web Part.
  • Drag the icon next to “My Simple Web Part” to a zone on the Web Part Page.
    Type some text in the text box, and then click Set Web Part Title to test the part.