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
- SQL Server with the Northwind database available
Download the zip file for the code:
- Web project (Web part)
- ToolPartHelper (
- 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.
- Putting the DLL and the Webpart into the correct place:
Putting the Jscript file "ToolPartHelper.js" into the correct place on the SharePoint Server:
- Put ToolpartHelper.dll into your development GAC.
- Put ToolpartHelper.dll into your development server GAC.
- Add this line to the web.config "
<assemblies>" section of your SharePoint services server:
<add assembly="ToolpartHelper, Version=184.108.40.206,
Culture=neutral, PublicKeyToken=cd83bf6e90388ad4" />
If you want to see the already finished NorthWinds Webpart (from the downloaded code example):
- 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\".
- 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.
- Change the connection string in the ASP.NET code in "NorthWindData.ascx.vb" to find your local NorthWind database.
- Put the "NorthWindData.ascx" and "NorthWindData.ascx.vb" in to the SmartPart "UserControls" directory on your SharePoint Server.
- 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:
- 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.
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.
- 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:
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.
Note: I choose comma delimited because it is easy to put into the "
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.
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.
- 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.
- 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.
- 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.
- 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
SingleSelectProperty class – Similar to the multiple version, but much simpler. Returns one value and you pass in a comma delimited string.
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.
SpacerClassForRebind class – As explained above.
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.