SPContent types lack internal names

As you could read in my last post I had a little issue with a feature that create a new list item and writes this to list. However, turning custom errors off didn't help that much: I had everything in a try/catch and I still got a null reference exception.

As I was testing, it turned out that the problem had something to do with the Dutch language pack. So I installed the Dutch language pack on my dev machine (yeah, I know... should have done that from the moment I started developing for this client) and bingo.

The problem was that I assumed the value of SPContentType.Name is an internal name and behaves the same as SPField.

So setting the content type on the list like this:
SPContentTypeId contactTypeId = _currentList.ContentTypes["Contacts"].Id;
Will work for English lists, but will cause a null reference exception for Dutch ones, since the name for this content type is "ContactPersonen" in Dutch...

Instead we need to do it like this:
// 0x0106 : Parent Id for Contacts
SPContentTypeId baseTypeId = new SPContentTypeId("0x01060018141B8367FAC64C9CFF864F77742CCB");
SPContentTypeId contactTypeId = _currentList.ContentTypes.BestMatch(baseTypeId);

The trick is to create a bogus SPContentTypeId with the same parentId as the actual one and then use the BestMatch method of the SPList.ContentTypes.
This way we always get the correct content type Id.

Happy hacking!!

SharePoint & Custom Errors

Since SharePoint is a User centric product, it has custom error pages by default. Recently I had a bug in one of my features and only on the productional environment. The error message was "An unexpected error has occurred" with nothing written to log files, trace or the event log.... Great!

Now, you could debug the problem by adding comment & funneling the problem, but that didn't help in this case, so I had to loose the custom error page.

This is what needs to change in the web.config, first modify the custom error entry:
<customErrors mode=“Off“/>

and, if you want to see the callstack, change it to
<SafeMode MaxControls=“200“ CallStack=“true“…

This will change the custom "An unexpected error has occurred" to the standard ASP.Net error page!

Don't forget to change it back when you're done debugging!!!

syntaxhighlight test

This is a quick test to see if the syntax highlighter is working...
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BvDWeB.Classes.Core;
using BvDWeB.Classes.Models;

namespace BvDWeB.UI
{
 public partial class _Default : Page
 {
  private BvDSystem _bvdSystem;
  protected void Page_Load(object sender, EventArgs e)
  {
   if(!IsPostBack)
   {
    if(Session["BvDSystem"] == null)
    {
     Response.Redirect("~/Index.aspx");
    }
    _bvdSystem = (BvDSystem) Session["BvDSystem"];
    
    
    if (Request["systemChoosen"] != null)
    {
     bvdTree.Value = _bvdSystem.ActiveSystem.TreeToUse;
    }
    else
    {
     bvdLoadBvDUserSystem.Value = _bvdSystem.LoadBvDUserSystem.ToString();

     if (!_bvdSystem.LoadBvDUserSystem)
     {
      foreach (BvDSystem system in _bvdSystem.Systems)
      {
       ListItem item = new ListItem(system.Title, system.Config, false);
       item.Attributes.Add("systemIndex", system.SystemIndex.ToString());
       ddlSystems.Items.Add(item);
      }
     }
     else
     {
      bvdTree.Value = _bvdSystem.ActiveSystem.TreeToUse;
     }
    }
   }
  }
 }
}