This past week I have been working with a DataView that needed to filter a list by the title of the PublishingPage containing the DataView web part. In order to provide this functionality, I created a straight-forward custom parameter:

public class PageTitleParameter : System.Web.UI.WebControls.Parameter
{
  protected override object Evaluate(System.Web.HttpContext context, System.Web.UI.Control control)   
  {
    SPListItem listItem = SPContext.Current.ListItem;

    if (listItem != null)
    {
      try
      {
        return listItem.Title;
      }
      catch
      {
        return string.Empty;
      }
    }

    return string.Empty;
  }
}

Creating custom Parameters has been covered elsewhere, so I won’t bother going into that, it works pretty much the same in SharePoint as in any ASP.NET application. Where things get different, is after you export your shiny-new DataView web part to SharePoint. If your experience is anything like mine, when you add your web part to a page instead of seeing your rendered DataView, you see “Parsing Error: Object reference not set to an instance of an object”.

So what went wrong? If you look carefully at the parser error, you will likely see that the TagPrefix that you setup when you registered your custom parameter in SharePoint Designer has been replaced with something like “cc3”, “cc4”, etc. In order to fix this, instead of exporting the web part from SPD straight into SharePoint, save it to a file first. Open the .webpart file that you saved and add your @Register directive for “cc3” or “cc4” or whatever your custom parameter’s TagPrefix happened to become. You can now go into the Web Part Gallery and upload your web part.

Good luck!