Recently I was given the task of creating some custom Web Parts for a newly implemented Microsoft Sharepoint 2007 site. Not knowing anything much about Sharepoint, the first step was to figure out how to create a basic Web Part and then figuring out how to publish it for use.
Web Parts in SharePoint 2007 are based on the ASP.NET 2.0 Framework so you can use your choice of .NET languages to create your custom Web Parts. My .NET language of choice is C#, so i’ll be using that for this example.
The first thing you need to do is create a new Class Library project in Visual Studio 2005. Once created, add a reference for System.Web as it contains the Web Part controls. You then need to add an override for Render() and for testing use Write to output some text eg. writter.Write(“Hello Trent!”); - See the below code for an example:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
namespace SampleWebPart
{
public class SampleWebPart : WebPart
{
protected override void Render(
System.Web.UI.HtmlTextWriter writer)
{
writer.Write("Hello Trent!");
}
}
}
Before building the Web Part the assembly needs to be strongly named, you can do this in Visual Studio by opening the project properties and then going to the Signing tab. Tick on the Sign the assembly check box and then click on the drop down list and select New. Give the key a name and choose a strong password. Click Ok and you’re done. Additionaly you will need to open AssemblyInfo.cs and add [assembly: AllowPartiallyTrustedCallers()] on a new line. You can now build the Web Part assembly.
There are 2 ways that I found to deploy the Web Part assembly, Global Assembly Cache (GAC) or bin – I first tried using the GAC as it was the recommended way but was unsuccessful. Using the bin method worked without a hitch, so I will explain that method.
First copy your compiled assembly to the _app_bin directory (You’ll find it in your SharePoint installation directory – In my case it was at C:\Inetpub\intranet\_app_bin\).
For the next step you’ll need to get the Public Key Token for your assembly, see Strong Name Tool (Sn.exe).
Open the web.config file found in the SharePoint installation folder and add the following line to the SafeControls section (You’ll need to replace the PublicKeyToken with the Public Key Token of your assembly):
<SafeControl Assembly="SampleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" Namespace="SampleWebPart" TypeName="*" Safe="True" />
Now open your SharePoint site, go to Site Actions, Site Settings, and select Web Parts (It’s underneath Galleries). Click on New, put a tick next to your Web Part in the list and the click on Populate Gallery.
You can now add your custom Web Part to any of the pages in your SharePoint site.
Related Posts- Open Media Library for Vista Media Center I've decided to put the resources and knowledge I gained from working on MyTheatre to use by joining the Development team of Open Media Library. Since it seems we all share a common goal, we should be able to get...
- Compiling Open Media Library from source Now that Open Media Library is coming closer and closer to a functional application people are starting to ask "Where can I download this from!?", the answer - Because it's open source you can download the source and compile and...
- Meta Tag Use for SEO When it comes to search engine optimization for a website, one of the key strategies that you should be using is to make use of meta tags on every individual web page within your site, especially when it comes to...
- You Don't Need To Be A Professional To Create Powerful Video Products The explosion of Internet video proves one thing-you don't have to be a professional producer of video content to reach a huge audience on the Worldwide Web. If you have expert knowledge about something that can benefit customers, you can...







FUcking AMAZING
Hi There,
I am new in the field. Could you please provide a bit more detail how to get the Public Key Token?
Thanks a lot
Ray
Hi Ray,
You can get the Public Key Token using the Strong Name Tool.
First start up the Visual Studio 2005 Command prompt (It’ll be in your start menu under the visual studio program group).
Change the path to the path of your assembly and then type in sn -t MyAssembly.dll
do you know how to assign the “group” property to custom web parts built this way? I see that once I import the web part into the Sharepoint site, I can modify it and set the group name there, but I would like to have this group name set automatically, from within the DLL or however it would need to be done. Thanks for your help,
dave
Hi Trent,
I am a nebe at this. I saw your comments on Microsoft TECNET, blog ( I don;t even know if that is the right neame). You were helping someone with the EDGE server Issues. I am a medical Doctor and I have Office Communicator 2007. My IT person installed the edge server which, works well internally. But he does not knwo how to make it work internelly.
I need your help. Do you know any company who can fix this for me? You help is greatly appreciated.
Jody
Am also trying to create a custom web part but when i go to my file tab in visual studio 2005 to create a new project, i don’t see the Class Library Project section so as to create a class Library Project.
Can someone please help me with this problem cus i can’t get to build my custion web part without this.
Sounds like you don’t have Visual Studio installed correctly if you have no option to create a Class Library project.
That is very interesting. It provided me a number of ideas and I’ll be writing them on my web site soon. I’m bookmarking your site and I’ll be back. Thanks again!
Really great informative blog post here and I just wanted to comment & thank you for posting this. I’ve bookmarked youi blog and I’ll be back to read more in the future my friend! Also nice colors on the layout, it’s really easy on the eyes.
Very informative put up, love the best way you write and I believe that the data helps in a way. I don’t usually say this, however I think this is a nice job done. If you happen to prefer to change hyperlinks, I would be more than happy to provide a hyperlink again to your site. Hope to listen to from you soon. Cheers