Click here to Skip to main content
15,887,027 members
Articles / Web Development / ASP.NET
Article

A ToolPartHelper to use with ReturnOfTheSmartPart

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
23 Oct 2007CPOL5 min read 29.3K   76   17   3
The code helps speed up the devopment of toolparts using SmartPart and SharePoint.

Introduction

I have used SmartPart for about a year, and love it. However, most of my first projects where very similar in nature, and had certain requirements for the Toolpart functionality. I think Microsoft should really have something like this pre-built (maybe, they or someone else does, and I just missed it). Since I did not see anything out there, I created this ToolpartHelper Library. And, that is all I really will try to talk about. How to use other things like Visual Studio, GridView, or ReturnOfTheSmartpart should be found elsewhere.

Things you will need:

  • SharePoint Services 3.0 installed on a server
  • SharePoint DLL
  • ReturnOfTheSmartPart
  • SQL Server with the Northwind database available

Download the zip file for the code:

  • Web project (Web part)
  • ToolPartHelper (Toolpart class)
  • JScript file
  • Note: I took out the SharePoint and ReturnOfTheSmartPart DLLs. They made the zip file just too big. Actually, it was the SharePoint DLL that was quite large, SmartPart was small, but you should already have them both.

Setup

  • Putting the DLL and the Webpart into the correct place:
    1. Put ToolpartHelper.dll into your development GAC.
    2. Put ToolpartHelper.dll into your development server GAC.
    3. Add this line to the web.config "<assemblies>" section of your SharePoint services server:
    4. XML
      <add assembly="ToolpartHelper, Version=1.0.0.0, 
           Culture=neutral, PublicKeyToken=cd83bf6e90388ad4" />
  • Putting the Jscript file "ToolPartHelper.js" into the correct place on the SharePoint Server:
    1. In the downloaded code, find the "ToolPartHelper.js" and put it in: "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\".
    2. Set the permissions on the Jscript file (Click the Advanced button) to "Inherit from parent". Simply reapply for the permissions by removing and then checking the Inherit box. Click Apply. Click OK.
  • If you want to see the already finished NorthWinds Webpart (from the downloaded code example):
    1. Change the connection string in the ASP.NET code in "NorthWindData.ascx.vb" to find your local NorthWind database.
    2. Put the "NorthWindData.ascx" and "NorthWindData.ascx.vb" in to the SmartPart "UserControls" directory on your SharePoint Server.
    3. Use ReturnOfTheSmartPart to display "Is My Way Cool SmartPart".

Seeing the WebPart with Toolpart in action

  • Using the BaseMultipleSelect property in the BindData method (you can see my method in the downloaded code) is complicated by the fact that:
    1. No data selected in the multiple select is no data on the webpart, except a message. So I make the user want to go to the Toolpart and make a selection.
    2. The BindData code routine is complicated because of the above, and the fact that the Select statement uses a "where" clause with the keyword(s) "IN" or "NOT IN". You can see this in the downloaded code, just remember that the SingleSelect class is much easer to use.
    3. Also in the code, the example event code that I use shows how to rebind to get the most up to date data – more on that later.

Using the code for Multiple Select

The instantiation includes these important points:

  1. PropertyKey – Both this parameter and the below one use a String identifier to store information into SharePoint using the "GetCustomProperty" in SmartPart. This is where a comma delimited string of choices is stored for retrieval.
  2. Note: I choose comma delimited because it is easy to put into the "where" clause.

  3. PropertyKeyAutoUpdate – If the user has the "Select All/Auto-Update" check box selected, it returns "true" or "false". This directly relates to the "Where" clause talked about above.
  4. DataTextFieldName and DataValueFieldName – Simply hook up the dataset to the checkbox list that is displayed. The display value and the actual value are stored. The "DataValueFieldName" is optional.
  5. Note: For Toolparts, an Apply, OK, or Cancel button will run the code for X number of Toolparts in the Toolpane. For multiple Toolparts, "Apply events will be raised". So, I created local events for each class. But there is a separate class that acts as a single point of consumption for events (that can run once for all Apply, OK, or Cancel buttons). The purpose ultimately is to avoid as many multiple rebinds.
  6. If you use only one of the Select (single and/or Multiple) options, then its Rebind event is just fine. If you do not at least do this, then when a user clicks a button, the freshest data will not display until the next postback.
  7. If you have many Select (single and/or multiple) options, then in order to avoid a Rebind for each event, you can use the setup I have in this example project. It has only one Rebind event that is raised for all Apply, OK, and Cancel button events that are raised.
  8. JScript functionality – If you open the Toolpart, you should see the multiple select Toolpart. You click on a check box, then the numbers in the above text increment or decrement without a postback. Also, the "Select All" checkbox will check or uncheck all. Note: If permissions where not set up correctly, a log on the dialog box will display.

Points of interest

  1. SingleSelectProperty class – Similar to the multiple version, but much simpler. Returns one value and you pass in a comma delimited string.
  2. SQLTextBoxString class - – If a passed in value matches certain characters, then an error is shown to the user. This is because it uses simple tactics against SQL injection. Not complete by any stretch of the imagination. My thought was I rather do something than nothing.
  3. SpacerClassForRebind class – As explained above.

History

In the future, I will put the code onto CodePlex with source. My hope is either someone has something better, or the code will be improved. Thanks, and I hope this is helpful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Programmer for only 7 years - started in vb6 and am now useing VB.net.

Comments and Discussions

 
GeneralI am getting an error Pin
Ajay756-May-08 6:17
Ajay756-May-08 6:17 
GeneralRe: I am getting an error Pin
.chris6-May-08 11:01
.chris6-May-08 11:01 
GeneralNice Article Pin
jeremykirkup18-Mar-08 1:36
jeremykirkup18-Mar-08 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.