Monday, May 29, 2006

HowTo - Custom WebPart development in Sharepoint 2007 Beta2

The purpose of this demo shows you how to build a simple “Hello World” web part using the very latest release of SharePoint 2007 Beta2. I found this to be quite simple except for the deployment part, so I will show you how easy it is to do this with a minimum of fuss.

If you do a fair amount of SharePoint Web Part development like I do, then you want stuff to just work first time without any hassles.

This demo is built using SharePoint 2007 beta2 and Visual Studio 2005.

The steps are as follows:
Step 1. Create the Web part class control
I created a new C# Class library project and called it ‘WebPartDemo’ and added a class called ‘MyWebPart.cs’

Step 2. Add the namespace references -
You will need to add a reference to System.Web.

Step 3. Add the following namespace using statements to the class:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

Step 4. Ensure your class inherits from the WebPart class and add the RenderContents () method
The finished code is as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


namespace WebPartDemo
{
public class MyWebPart : WebPart
{
protected override void RenderContents(HtmlTextWriter writer)
{
this.EnsureChildControls();

writer.Write("Hello World from the Web part");
}
}
}
This is pretty simple as it just displays a message from within the web part.

Step 5. Implement the ‘allow partially trusted callers’ attribute on the class.
This stops from a security exception from being raised when trying to add the Web Part to the Web part gallery.

To do this you will need to open up the ‘AssemblyInfo.cs’ class under the Properties folder in Visual Studio.

Add the using System.Security; reference

Decorate the class with the [assembly: AllowPartiallyTrustedCallers]attribute.

The ‘AssemblyInfo.cs’ code should look like the following:
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WebPartDemo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("SomeCompany")]
[assembly: AssemblyProduct("WebPartDemo")]
[assembly: AssemblyCopyright("Copyright © SomeCompany 2006")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("47f406bf-50f0-43da-b867-f1c35e3df7f9")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AllowPartiallyTrustedCallers]

Step 6. Strongly name the assembly and rebuild (This is pretty self explanatory)

Step 7. Copy the Web Part to SharePoint

This sounds pretty straightforward but it caught me out the first time I did it so I will pass on what I did to get it to work.

In SharePoint 2007 new sites are located under the following directory
(where GUID is the Guid or Port of your newly created site)

InstallDir\InetPub\wwwroot\wss\virtual directories\{GUID of Site} The \bin folder sits under the \{GUID of Site}\ folder.

This is where you will need to copy your compiled Web part. So the full path would be as follows:
InstallDir\InetPub\wwwroot\wss\virtual directories\{GUID of Site}\bin
Copy your compiled Web Part DLL to the Bin folder

Step 8. Add your Web Part to the Safe controls section of the web.config.

This is a trap for the unweary. The web.config you want to edit is the one located in:

InstallDir\InetPub\wwwroot\wss\virtual directories\{GUID of Site}
And nowhere else, unless you are deploying your web part to multiple sites.

The entry you want to add is as follows:



Save and close the web.config. This notifies SharePoint that it should “Trust” the web part control and be executable.

Step 9. Add your Web Part to the Web Part gallery

For your site do the following:

1) Go to site settings
2) Select Web Parts
3) Click the New button
4) A popup should appear
5) The Web Part ‘WebPartDemo’ should appear in the list with an unchecked check box next to it.
6) Select the check box and click on ‘ Populate gallery’
7) You can edit the control once it has been added but I usually don’t
8) The popup should disappear

Step 10. Add your Web Part to your site page
If all went well then you should be able to add your custom web part to any web part page on your site.
This is pretty self explanatory.

If you have any queries etc, please email me at: aaron.saikovski@readify.net

3 comments:

Anonymous said...

it is very useful. but still now i have one doubt
whether the GUID given in the assemlyifo.cs
and InstallDir\InetPub\wwwroot\wss\virtual directories\{GUID of Site} The \bin folder sits under the \{GUID of Site}\ folder is same id or not

prabhu said...

Hi Aaron,

I am prabhu, doing my MS in germany. Iam working with sharepoint webpart development in c#. I wanna add an item to the listview webcontrol which is created in parent class, from another class. I created an object reference to that base class and i just accessed that listview wencontrol thru that object and added the item. But, i ended up with error saying that null reference. Even i culdnt access any method from that parent class, declared as public aswell. Can u help me on this?.. Looking forward ur reply.

I got ur ID from http://ruskydotnet.blogspot.com/2006/05/howto-custom-webpart-development-in_29.html this link.

Thank u for ur time consideration sofar.

Regards,
prabhu.

Neets said...

Hi Aaron,
I have created a webpart that queries a document library's view. When we hard code the view's name in the webpart's code - the webpart displays its content to all.
The moment the view name is accepted via a property, the webpart correctly displays content to anyone having conributor rights or higher, but displays an 'Access Denied' error to visitors.
Have you ever encountered such a problem in the past?
We are trying all kinds of tricks here, but havent struck gold as yet. Appriciate anykind of help :)