Weird ContentType of Blog Posts

Okay, this is weird.

I’m trying to do something with various types of item, including Blog Posts, in SharePoint. Naturally, I’m getting the SPListItem for them, and then the SPListItem.ContentType, so I can check what kind of item is it. The weird thing is, if I try to get a Blog Post, the SPListItem.ContentType is NULL.

The code I’m running is:


static void Main(string[] args)
{
    string url = "http://vm-moss2007/sub/blog/Lists/Posts/";
    using (SPSite site = new SPSite(url))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList list = web.GetList(url);
            SPListItem item = list.GetItemById(1);

     Console.WriteLine(item.DisplayName);
     if (item.ContentType == null)
     {
         Console.WriteLine("ContentType is Null");
     }
     else
     {
         Console.WriteLine(item.ContentType);
     }
 }
    }
}

I was running this code in a console application on the server. The URL is to a Blog Post library in a Blog site. When run, the output was:

I then tried this, but using a URL to a Pages Library. This shows an output type of SPContentType – which is what I’d expect.

That’s weird. I mean, the item clearly DOES have a content type at some level – there is an SPBuiltInContentTypeId.BlogPost value. However, SharePoint Manager doesn’t show it, my code reports it as NULL, and I can’t find any references to this anywhere! And the Advanced Settings page for a Blog’s “Posts” list doesn’t have an option “Allow Management of Content Types”.

Views can show the Content Type column, which reports itself as ‘Post’ for each item.

Anyone else ever seen this?

Advertisements
Weird ContentType of Blog Posts

2 thoughts on “Weird ContentType of Blog Posts

  1. einaros says:

    The ContentType property seems to be null for posts, but if you look at the xml representation of a blog post (or in the database), it will have ows_ContentTypeId=’0x011000ACE94CF616B2804A8C75B75989D8D1D1′. 0x0110 is a basic BlogPost, according to the id lists, so this type is derived form that.

    Here’s a Powershell script I put together that demonstrates extracting the content type:

    [reflection.assembly]::LoadWithPartialName(“Microsoft.SharePoint”) > $null
    $site = New-Object Microsoft.SharePoint.SPSite(“http://vm-dev:1337/blog”) # Replace Url
    $web = $site.OpenWeb()
    $blog = $web.Lists[6] #Replace with actual list reference
    write-host ContentType is null: ($blog.Items[1].ContentType -eq $null)
    $contentTypeId = $blog.ContentTypes.BestMatch($blog.Items[1][“ows_ContentTypeId”])
    write-host But we can find the ct in the data: $contentTypeId
    write-host And its name is: ($blog.ContentTypes[$contentTypeId].Name)

    Like

  2. A bit more from Twitter:

    @einaros Thanks, I was worried I was going mad! Yeah, noticed the schema was okay, but am peeved that I have to treat blog posts separately

    @AndrewWBurns Reflector verdict: item.ContentTypeId returns SPContentTypeId.Empty if list.AllowContentTypes is false, which it is for blogs.

    @AndrewWBurns So .. it’s just yet another facepalm design decision. Rather than returning a CT Id, if any, they rely on something unrelated!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s