About
You arrived at the weblog of Geert Baeke. I am a technology consultant for a company called Xylos (Belgium). I mostly work with Microsoft technologies such as Windows, Active Directory, Exchange, Sharepoint, MSCS, and more. I am also actively busy with VMware's products, focussing on VMware ESX.

Sections
Search






XBOX 360

RSS Newsfeeds
baeke.info Main RSS Feed Main Page RSS
Sharepoint RSS Feed Sharepoint RSS
View Article  Creating connectable web parts with the Son Of SmartPart

If you have to develop web parts for SharePoint, you have probably heard of the SmartPart. If you want to create web parts with Visual Studio 2005 and ASP.NET 2.0, you can use the Son of SmartPart. The SmartPart and the Son of SmartPart can be downloaded from GotDotNet. This post walks through the steps to create connectable web parts created with Visual Studio 2005 and the Son of SmartPart.

Why use the SmartPart at all? The reason is quite simple. Without it, you have to create web parts completely in code without a designer. If you are not a developer (and even if you are), that is quite frustrating. The SmartPart allows you to easily pick an ASP.NET user control and host that in a web part. The advantage of an ASP.NET user control is that you can use the full power of Visual Studio and create your web part UI with dragging and dropping controls. Although it is possible to use user controls in web parts without the SmartPart, it is more complicated. The SmartPart takes care of some of the plumbing for you so that it even becomes easy to create connectable web parts.

This post is targeted at beginners with some development experience that have not created connectable web parts yet (with or without the SmartPart).

Let's get started. First of all, make sure your SharePoint sites are using ASP.NET 2.0. On a box that's already running Windows SharePoint Services 2.0 SP2, perform the following tasks:

  1. Install the .NET Framework 2.0.
  2. Using IIS Manager, open the properties of the web site that's extended with SharePoint. This is usually Default Web Site. Click the ASP.NET tab and switch to ASP.NET 2.0.
  3. Run the following command: stsadm -o upgrade -forceupgrade -url http://yoururl
  4. If step 3 fails, run the stsadm.exe program from the following path: C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN. Also, this is only possible with SP2 of Windows Sharepoint Services.

The next step is to install the Son Of SmartPart on the server. Use the instructions that come with the download.

Now, let's build the connected web parts. We will display the list of employees from the SQL Server Northwind database. When the user selects an employee, the orders created by that employee are shown. Fire up Visual Studio 2005 and let's get started:

  1. In Visual Studio 2005, create a new web site using File / New / Web Site...
  2. In the New Web Site dialog, select ASP.NET Web Site and select File System as location.
  3. Use an appropriate path. I used C:\Documents and Settings\<username>\My Documents\Visual Studio 2005\WebSites\SmartPart.
  4. For language, I chose C#.
  5. When you click OK, you will have a new project with a Default.aspx page. You can now start to add user controls.

Create the employee user control. This user control will be used in the Employees web part that will act as the provider. It provides data to the Orders web part.

  1. In the Solution Explorer, right click the project name and select Add New Item...
  2. Select Web User Control and call the file Employees.ascx.
  3. From the toolbox, drag and drop a SqlDataSource on the canvas.
  4. Click the little arrow in the top right corner of the SqlDataSource and select Configure Data Source...
  5. You will now get a wizard to connect to the database and configure your select statement. I will not go through all the answers here because it is quite straightforward. I only selected the following fields: EmployeeID, LastName and FirstName.
  6. Now drag and drop a GridView on the canvas and connect it to the SqlDataSource you configured in step 4 and 5. You can again use the little arrow in the top right corner to do that.
  7. Make sure you select Enable Selection in the GridView Tasks.
  8. Switch to code view by right clicking the canvas and selecting View Code.
  9. Add references to Microsoft.SharePoint.dll and SonOfSmartPart.dll. You do this by right clicking your project in the Solution Explorer and selecting Add Reference... Microsoft.SharePoint.dll you can select from the .NET tab. SonOfSmartPart.dll you will need to select by browsing to it on your file system.
  10. You need to implement the SonOfSmartPart.ICellProviderUserControl interface. Put the cursor behind System.Web.UI.UserControl and type the following: ,SonOfSmartPart.ICellProviderUserControl
  11. You will see a line under the letter S (of SonOfSmartPart). Hover the mouse over it and then click the icon that appears. Choose to implement the interface. Some code will be added automatically.
  12. The property ProviderMenuLabel can be used to return a string that the user will see while connecting the web parts in SharePoint.
  13. The method GetProviderData should return a string. That string will be passed to the connected web part (the consumer). Simple isn't it?

The full source code for the Employee user control should be something like:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using SonOfSmartPart;

public partial class Employees : System.Web.UI.UserControl, SonOfSmartPart.ICellProviderUserControl
{
    public string GetProviderData()
    {
        if (GridView1.SelectedIndex != -1)
        {
            // just return the selected datakey which is EmployeeID
            return GridView1.SelectedDataKey.Value.ToString();
        }
        else { return "-1"; }
    }

    public string ProviderMenuLabel
    {
        get { return "Provide employee to..."; }
    }

}

 

Now you are ready to create the Orders user control. This user control will be hosted in a Son Of SmartPart web part that will act as the consumer. It will consume the string of data (the EmployeeID) from the Employee web part.

  1. Add a new Web User Control and call it Orders.ascx.
  2. Add a SQLDataSource, a GridView and a TextBox. Set the Hidden property of the textbox to true.
  3. The SQLDataSource should again connect to Northwind and this time display the OrderID, CustomerID and OrderDate. You will also create a parameter that takes the parameter value from the (hidden) textbox. You create such a parameter by clicking the WHERE button in the Configure the SELECT Statement page of the Configure Data Source wizard.
  4. Switch to code view and implement the SonOfSmartPart.ICellConsumerUserControl interface. Some code will again be added when you choose to implement the interface.
  5. The ConsumerMenuLabel property should return a string. The user will see the string in the user interface of SharePoint while connecting the consumer web part to the provider.
  6. The SetConsumerData method should be used to receive the data from the provider and do something with it. In this example, the text of the textbox is set to the EmployeeID received from the Employee web part. After that, the GridView's DataBind method is called.

The full source code in Orders.ascx.cs should be:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using SonOfSmartPart;

public partial class Orders : System.Web.UI.UserControl, SonOfSmartPart.ICellConsumerUserControl
{
    public string ConsumerMenuLabel
    {
        get { return "Consumes employee from..."; }
    }

    public void SetConsumerData(string data)
    {
        // employeeID is in the data string
        txtEmployeeID.Text = data;
        GridView1.DataBind();
    }
}

 

You are now ready to deploy the web parts to SharePoint. Each user control consists of two files: an .ascx file and an .ascx.cs file. Copy those files to the usercontrols folder of the web site root that hosts your Sharepoint site. In my case that is c:\inetpub\wwwroot\usercontrols. If the usercontrols folder does not exist, create it. Make sure you copy the files. That way, they will inherit NTFS security rights properly.

Your usercontrols folder should contain: employees.ascx, employees.ascx.cs, orders.ascx, orders.ascx.cs.

Now open your SharePoint site and add a Son Of SmartPart UC web part to your page twice. The two web parts will need to be configured. Use the link in the web part to open the tool pane to select the user control to use from the list.

When you have added the Employees user control to the first web part and the Orders user control to the second, you can connect them. Put the SharePoint page in design mode and use the web part menu of the Employees web part to connect it to the Orders web part.

If everything went well, you should see the orders of the employee you selected.

View Article  MOSS 2007 Backup Strategies

AvePoint have a whitepaper called "MOSS 2007 Backup Strategies". It contains interesting information about some of the new backup features of MOSS 2007. Not surprisingly, it also discusses AvePoint's backup solutions. AvePoint have interesting backup solutions for Sharepoint (both 2003 and 2007) and are definitely worth investigating if your looking for things like file-level backup and restore, archiving and more.

A direct link to the whitepaper here (pdf).

 

Login
User name:
Password:
Remember me 
This Month
September 2006
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Networking
View Geert Baeke's profile on LinkedIn

Services

Powered by BlogHarbor
Powered by BlogHarbor
StatCounter