In one of my projects I was adding web parts to a SharePoint page and then configuring them depending on various conditions, one of which was an out of the box KPI Web Part (from the Enterprise Version of SharePoint 2010).

The API of this web part is not particularly well documented and a lot of it has the ominous “for internal use only” stamped all over it, but since we really needed this functionality (we did the same thing with the Excel web part), it was necessary to give it a try.

I ran into a nasty weird bug (or design flaw) that I had to use the excellent ILSpy to get to the bottom of.

Very simply, all I wanted to do was link a KPI web part to a list dynamically through code. This should be as easy as saying

SPLimitedWebPartManager manager;
Microsoft.SharePoint.Portal.WebControls.KPIListWebPart myWebPartReference;
SPList myStatusListReference;
myWebPartReference.TitleURL = myStatusListReference.RootFolder.ServerRelativeUrl;
myWebPartReference.ListURL = myStatusListReference.RootFolder.ServerRelativeUrl;
// You will get a NullReferenceException here

Doing this simple bit of code (some details left out obviously, so don’t copy and paste the above) will result in a NullReferenceException (Object reference is not set to an instance of an object).

The issue is actually in Microsoft’s code and it wasn’t until I ran ILSpy that I saw it. In the setter for ListURL, you can see what the problem is.

bool flag = string.IsNullOrEmpty(this.TitleUrl);
if (!flag && Uri.TryCreate(KPIHelper.GetAsAbsoluteUrl(this.TitleUrl), UriKind.Absolute, out uri) && Uri.TryCreate(KPIHelper.GetAsAbsoluteUrl(this.listUrl), UriKind.Absolute, out uri2))
// details omitted
if (flag)
this.TitleUrl = value;
this.listUrl = value

So it is basically checking to see if the TitleURL is set already, and if it is it will call this KPIHelper.GetAsAbsoluteUrl which demands an SPContext.Current reference. This does not exist during the provisioning and feature activation through creating a site collection through Central Administration, so was giving the null reference.

Very strange because if you do not set the TitleURL at all, then it will simply assume everything is fine and set the TitleURL to the same as the ListURL for you, which is the fix for this.

I guess I was being a bit too helpful and explicit and should of let SharePoint try to do it’s thing, though the problem is certainly not obvious.

One Reply to “SharePoint 2010 KPI Web Part ListURL Weirdness

Leave a Reply

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