Populating a Drop Down Based on Content Type

Recently, I’ve had to populate a drop down with lists of a certain content type. So, SharePoint offers a great way to filter lists based on content types with their ContentTypes.BestMatch() and .IsChildOf()  functions.

In order to get the content type you’ll need, you’ll have to know the ID of the content type. Theses are pretty cyptic, but you can find a great MSDN Article containing a description of how content types are composed Here.

Here’s a table of the base content types courtesy of Microsoft:

Name ID
System 0x
Item 0x01
Document 0x0101
Event 0x0102
Issue 0x0103
Announcement 0x0104
Link 0x0105
Contact 0x0106
Message 0x0107
Task 0x0108
Workflow History 0x0109
Post 0x0110
Comment 0x0111
East Asia Contact 0x0116
Folder 0x0120

Use this function to get the lists that contain the content types you are looking for:

static bool ListContainsContentType(SPList List, SPContentTypeId CTID)
// Getting the Best Match for Supplied Content Type ID & Checking if the Content Type is a child of the supplied
SPContentTypeId _MatchID = List.ContentTypes.BestMatch(CTID);
return _MatchID.IsChildOf(CTID);

Now lets create a function to validate lists passed to it:

protected bool ValidateList(SPList CustomList, string ContentTypeCode)
// Checking if the list has the required fields/content types
SPContentTypeId _CTID = new SPContentTypeId(ContentTypeCode);
SPContentTypeCollection _List = QLList.ContentTypes;
if (ListContainsContentType(CustomList, _CTID))
return true;
return false;

Then, to use the code above to validate the site  lists and you add them to your drop down:

// Adding Lists to Drop Down
foreach (SPList _QuickLinksList in _QuickLinksLists)
if (ValidateListQL(_QuickLinksList, “0x0104”) == true)

Just a heads up, this obviously will only work if you’re searching for lists on the current site. Also, the example I use is checking for the Announcement Content Type.

As always, if you find any way to improve this or find any errors please let me know!

  1. No trackbacks yet.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: