I've recently started working with SharePoint 2007 and needed to create my first WebPart. The purpose of the WebPart was simply to allow a user to connect to an external ASP.Net 2.0 application and allow a direct pass through for the current SharePoint user, simulating a single sign on experience. After a doing a bit of research and analyzing the needs it was decided to keep it as simple as possible. A simple LinkButton with an event to do a Response.Redirect to the required URL and adding the Username to QueryString. The directed application will take care of the login procedure. The WebPart itself required the following:
- Ability to change the redirect URL
- Ability to set the text of the LinkButton
While developing the WebPart I hit one or two little catches which I couldn't find detailed information on using Google, so decided to blog about the process for developing this particular WebPart. The principles will be the same for any other WebParts, and there is various tools out there that automates some of this. However it is always a good idea to have an understanding of what these tools will do.
Step 1: Create the DLL
We start out by creating a new Visual Studio 20045 Project, and selecting the Class Library Project Type. We also need to create a reference to some assemblies to make the integration process easier.
- System.Web - Allows access to the standard ASP.Net 2.0 WebParts and as well as the HttpCurrentContext.
- Microsoft.SharePoint - Allows for the setting of properties on the WebPart and direct integration into SharePoint.
The first is installed by default when you have the .Net Framework 2.0 installed. To get a reference to the Microsoft SharePoint assembly the easiest method is to copy the assembly from an already installed SharePoint Server and create a reference to the DLL locally. It is however not required to download all the dependencies, and when creating a deployment package explicit exclude the assembly as it will already be installed on the SharePoint Server.
Create a new class which will inherit either one of the following classes
- System.Web.UI.WebControls.WebParts - Provides generic WebPart support and remains compatible with ASP.Net 2.0 Application.
- Microsoft.SharePoint.WebPartPages.WebPart - Provides SharePoint WebPart support which is not backwards compatible. Allows the use of advanced SharePoint features.
Since the SSO WebPart requires settings to be set within the SharePoint environment it inherits the Microsoft.SharePoint assembly. This allows the public properties to be declared allowing users and administrators to set certain values when editing a SharePoint page that contains the WebPart. The code for the WebPart so far should look as follows:
1: using System;
2: using System.ComponentModel;
3: using System.Web;
4: using System.Web.UI.WebControls;
5: using Microsoft.SharePoint.WebPartPages;
6: using System.Text;
7:
8: namespace MyWebParts.MySingleSignon
9: {
10: public class SingleSignon : Microsoft.SharePoint.WebPartPages.WebPart
11: {
12:
13: }
14: }
Step 2: Declare the Public Properties
The next step in the process is to declare the Public Properties to store the values we want to be able to set through the SharePoint Page Editor after adding the WebPart to the page. In the SSO WebPart we need two properties, RedirectUri and LinkText. First we need to declare to private string variables to store the values in, and thereafter declare the two public properties. However we will also add some additional declarations to enable these properties to be displayed on editor. Please Note: These declaration are only available when inheriting from the SharePoint assembly, and not when using the Web assembly.
By creating these properties we allow the WebPart to be used again and again for various different sights that might require SSO type functionality. The idea is to keep the WebPart as simple as possible. The code for declaring the public properties will look as follows:
1: private string strURL = string.Empty;
2: private string strText = string.Empty;
3:
4: [Browsable(true),
5: Category("Link Settings"),
6: Description("The URL to Redirect to once active"),
7: DefaultValue("http://localhost"),
8: FriendlyName("Link Url"),
9: WebPartStorageAttribute(Storage.Shared)]
10: public string RedirectSite
11: {
12: get { return strURL; }
13: set { strURL = value; }
14:
15: }
16:
17: [Browsable(true),
18: Category("Link Settings"),
19: Description("The text to display for the link"),
20: DefaultValue("External Link"),
21: FriendlyName("Link Text"),
22: WebPartStorage(Storage.Shared)]
23: public string RedirectText
24: {
25: get { return strText; }
26: set { strText = value; }
27: }
Step 3: Add the LinkButton
The LinkButton can now be added to the WebPart and all the required properties set. To add controls to a WebPart the CreateChildControls() method for the WebPart needs to be overridden. There is currently now design interface for WebParts, so this requires all the code to be added manually. We will be using the LinkButton found in the System.Web.UI.WebControls namespace and the code will be added as follows:
1: protected override void CreateChildControls()
2: {
3: LinkButton lnkButton = new LinkButton();
4: lnkButton.Text = strText;
5: lnkButton.Click += new EventHandler(lnkButtonClick);
6: Controls.Add(lnkButton);
7: }
We also need to add an Event for the LinkButton click which will retrieve the Current SharePoint username and pass it as part of the QueryString. The event handler will also then redirect to the specified URL.
1: private void lnkButtonClick(object sender, EventArgs e)
2: {
3: if (!String.IsNullOrEmpty(strURL))
4: HttpContext.Current.Response.Redirect(strURL +
5: HttpContext.Current.User.Identity.Name);
6: }
At this point we have a work LinkButton and Two properties that can be set via Sharepoint directly.
Step 4: Render the HTML
The last item to be completed is informing Sharepoint to render the HTML and display the WebPart. This is done by overriding the Render() method and telling it to render all child controls.
1: protected override void Render(System.Web.UI.HtmlTextWriter writer)
2: {
3: RenderChildren(writer);
4: }
Compile the assembly and give it a strong name. The assembly can now be registered in the GAC on the Sharepoint Server and added to the SafeControls list for the site it is required. This is by no means the most secure SSO example out there, but a good start to learn how to quickly and easily create WebParts for Sharepoint 2007.