Programmatically Get the URL for a Discussion Board Message in SharePoint 2010

Programmatically accessing discussion lists is problematic. There is no API that I’m aware of that has been built for them; you have to understand the internals of how they work and code against the SPList, SPFolder, and SPListItem objects. And, because of how the discussion lists have been built, the normal methods often don’t work as desired. For example, if you want to programmatically generate a link to a discussion board post and you just use the URL property of the listitem like so:

String URL = listitem.Url;

You will get the Url to a page that looks like this:

The above is the default view for a regular list, but this is not at all optimal for a discussions. Not only does the above not show the user the other messages in the thread, but it does not provide the user any noticeable way to navigate to the rest of the messages in the thread. Of course, the discussion board doesn’t use the typical dispform.aspx by default, it uses flat.aspx or threaded.aspx. Also, even if you were to hard code the url to the Flat view, for example, the flat view does not use the typical ID to identify which item to show, as every other view does in SharePoint. Instead, it uses “RootFolder” and “FolderCTID”. Following is a URL that points to a discussion thread on my dev box:


RootFolder: Path to the parent discussion topic (every time you create a new discussion item in a discussion list, the discussion itself is a folder in the list)

FolderCTID: the content type of the parent discussion. As far as I can tell, this querystring parameter is optional. If you remove it, the URL still takes you to the correct discussion thread. So, to generate the above for a given SPListItem:

SPWeb web = …;
SPList discussionlist = …;
SPListItem listitem = …;
string rootfolder = web.GetFolder(listitem.Url).ParentFolder.ServerRelativeUrl;
string MessageURL = discussionlist.Views["Flat"].ServerRelativeUrl+ "?RootFolder=" + rootfolder;

Edit: For a discussion board subject, you can also just use the “URL Path” property. For replies, the “Path” property will return something along the lines of:

When the user navigates to the above, SharePoint will redirect it to the url as described above, using the default view.


4 thoughts on “Programmatically Get the URL for a Discussion Board Message in SharePoint 2010

  1. Elia

    Hi, nice article. But what about achieving the same with a workflow in SharePoint 2007? How can I get the mail notification receiver to the general discussion?

    1. Elia

      Forgot to say that it works fine if the message is in fact a new topic with the only opening post, but when replying the link in the notification email lead the receiver to a 404 or Bad request error page.

      1. Mike G Post author

        Do you mean a SharePoint Designer workflow? If so, for the replies, use the “Path” property.

  2. Elia

    First of all, thank you for the quick reply. 🙂 Yes I was talking about a SP designer 2007 workflow.
    You pointed me on the right direction, with the “Path” suggestion and the built link lead the user to the discussion page. But I think there’s a bug with blanks replacing: it correctly replace all blanks (” “) with the encoded “%20”. All, but the first!
    For example:
    …mydiscussionboard/NEW POST TODAY HELLO
    is encoded in
    Clicking it results in a “404 Not Found” browser error.
    Did you ever faced something similar?
    If so, have you managed to solve this really annoying issue?


Leave a Reply

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

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