ToolPart Class With System.Reflection

If you are still working with some of the SharePoint base classes, you probably are familiar with the ToolPart class. The ToolPart class allows you optionally expose a bunch of neat little custom pieces you decide on regarding your WebPart in a natively expandable menu within the WebPart ToolPane. This is nice when doing something like displaying the copyright or whatever of your WebPart. The basic code for a ToolPart class looks like this:

[csharp]

using Microsoft.SharePoint.WebPartPages;
using System;
using System.Web.UI;

namespace buenz.Webparts
{
public class BasicToolpart : ToolPart
{
public BasicToolpartMS()
{
base.Title = “My Name”;
}

public override void ApplyChanges()
{
}

public override void CancelChanges()
{
}

protected override void RenderToolPart(HtmlTextWriter output)
{
output.Write(“Adam Buenz”);
}

public override void SyncChanges()
{
}
}
}

[/csharp]
But, when you are displaying anything like version of WebParts etc. it is often a little bit eaiser to use .NET Reflection as opposed to having to go through a bunch of string values in the WebPart class file to display the version of the WebPart. Instead you can just pull it from the assignments made in the assembly info file using the System.Reflection namespace. To take advantage of this, there are just a couple things that you have to do. The first is to make a reference to System.Reflection and then initialize three strings to hold the data that is being pulled from the executing assembly, in this case the WebPart version, the Company name that built the webpart, and of course, the WebPart product name. The other is to get the information from the WebPart assembly file using GetExecutingAssembly() to get the location to the WebPart binary, and then get the relevant information.
[csharp]

using Microsoft.SharePoint.WebPartPages;
using System;
using System.Web.UI;
using System.Reflection;

namespace buenz.WebParts
{
public class ReflectingToolPart : ToolPart
{
private string labelVersion;
private string labelCompanyName;
private string labelProductName;

public ReflectingToolPart()
{
this.labelProductName = AssemblyProduct;
this.labelCompanyName = AssemblyCompany;
this.labelVersion = String.Format(“Version {0}”, AssemblyVersion);
base.Title = “My WebPart”;
}

public override void ApplyChanges()
{
}

public string AssemblyVersion
{
get
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}

public string AssemblyProduct
{
get
{
// Get all Product attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
// If there aren’t any Product attributes, return an empty string
if (attributes.Length == 0)
return “”;
// If there is a Product attribute, return its value
return ((AssemblyProductAttribute)attributes[0]).Product;
}
}

public string AssemblyCompany
{
get
{
// Get all Company attributes on this assembly
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
// If there aren’t any Company attributes, return an empty string
if (attributes.Length == 0)
return “”;
// If there is a Company attribute, return its value
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}

public override void CancelChanges()
{
}

protected override void RenderToolPart(HtmlTextWriter output)
{
output.Write(”

“);
output.Write(”
“);
output.Write(labelProductName);
output.Write(“”);
output.Write(labelVersion);
output.Write(“”);
output.Write(labelCompanyName);
}public override void SyncChanges()
{
}
}
}

[/csharp]

Just make sure that the attributes that you are pulling from the WebPart don’t contain null values.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *