Using SPServices to Get the Display Names for a SharePoint List’s Content Types

My pal Tasha Scott (@TashasEv) tweeted a question today about how to get at the display name for a Content Type on a NewForm:

Hello Gentlemen,

As requested, I am posting my query from Twitter in regards to trying to pass the Content Type of an item into the PlaceHolderPageTitleinTitleArea, um, area of a New Form. I can do it easily in the Edit and Display forms using the “ContentType” property, but as stipulated i don’t think this property is set for the item until save. However, there is a Content Type ID# in the query string, so the new form knows what fields to pull… how can we convert this info into the Content Type name and display it?

Thanks for any assistance you can render! You guys are always the best.

It just so happens I got an email last week from a Codeplex user called jenglish who wanted to do something very similar, and s/he gave me the code s/he used. (I’ll just go with “he” for simplicity.) The approach is fairly simple.

First, he grabs the Content Type’s GUID from the URL. When you create a new item based on a Content Type (management of Content Types is enabled in the list’s settings), SharePoint passes the GUID for the Content Type on the URL in the Query String. It looks something like this:


Since the ContentTypeId is on the Query String, we can use it to look up the name of the Content Type so that we can display it however we’d like.

I’ve taken jenglish’s code, simplified it a little bit, and added some more comments, but it accomplishes the same thing he intended:

var queryStringVals = $().SPServices.SPGetQueryString(); // The SPGetQueryString function parses the Query String values out into an array
var contentTypeIdValue = queryStringVals["ContentTypeId"]; // This grabs the value of the ContentTypeId Query String parameter
var contentTypeName = ""; // Define a variable to hold the name of the Content Type

// Get the list's Content Types
  operation: "GetListContentTypes", // See the MSDN SDK at for details on this operation
  listName: $().SPServices.SPListNameFromUrl(), // The SPListNameFromUrl function gets the list name for the current context based on the URL
  async: false, // We'll do this asynchronously
  completefunc: function (xData, Status) {
    $(xData.responseXML).find("ContentType").each(function() { // All of the list's Content Types will be returned. We'll loop through to get the one we are interested in
      if($(this).attr("ID") == contentTypeIdValue) { // If the contentTypeId matches...
        contentTypeName = $(this).attr("Name"); // the name in our variable...
        return false; // ...and return false, which breaks us out of the loop. (We've found what we need, so no reason to continue looking.)
//... do something with the Content Type Name ...