Create a Discussion Thread on a Discussion Board with Author Override

So, there I was, trying to figure out how to create a new Discussion Thread on a SharePoint Discussion Board using the ‘feedback’ comments from a just-completed SharePoint task. 

It’s a great way to put all the comments from each completed workflow task in a SharePoint Discussion Board, providing an easy, ‘one-stop shopping’ view of all the feedback from all the workflow participants.  They can even engage in threaded conversations, if desired.

A quick Google search and I was off to the races using SPUtility.CreateNewDiscussion.  Should have been what I call a ‘quick victory’ in terms of time and code required.  It should have been even quicker because I wrote a small, one-step ‘proof of concept’ workflow using Visual Studio 2008 to verify how things should work.  VS 2008 streamlines the time needed to go from code to running a workflow by a factor of 4:1 compared to VS 2005.

Experience tells me there is a lot that ‘Mother [Microsoft] Never Told You About <<fill in subject>>…’, and this situation, of course, was no different. 

It seems that the ‘Author’ of the conversation thread cannot be specified via the CreateNewDicussion API.  It took some digging/experimentation (mostly try this, fail, try something else, fail) – the usual pattern in other words.  Even when I thought I had the solution, I got tripped up by the ‘freaky string format syntax’ required to change the ‘Author’ (and ‘Editor’ too as it turns out) to something other than the default of ‘System Account’ (or whatever account runs the SharePoint ‘engine’). 

So what exactly was it that ‘Mother [Microsoft] Never Told You About Specifying the Author of a SharePoint Discussion Thread’?  In code, it looks like the following, paying particular attention to the string.Format syntax. 

My question to anyone who might have some insight is this: was the person that dreamed up the freaky “;#” syntax needed to set the ‘Author’ and ‘Editor’ fields on drugs at the time he/she architected that part of SharePoint?  What happened to the KISS principle on this one?  Remember: Eschew Obfuscation!

/// <summary>
/// Creates a new Discussion thread on the specified Discussion Board, while associating the creator of the thread
/// to the specified person, not the default of 'System Account'
/// (or whatever account is running the SharePoint 'engine').
/// </summary>
/// <param name="ThreadCreator">SPUser object instance of the person who should become the thread Author.</param>
/// <param name="DiscussionBoard">SPList object instance of the Discussion Board a new Discussion Thread will be added too.</param>
/// <param name="Title">String value of the title of the new Discussion Thread.</param>
/// <param name="Body">String value of the body of the new Discussion Thread.</param>
/// <returns>SPListItem instance pointing to the newly created Discussion Thread</returns>
private SPListItem CreateDiscussionThread(SPUser ThreadCreator, SPList DiscussionBoard, string Title, string Body)
{
    SPListItem returnValue = SPUtility.CreateNewDiscussion(DiscussionBoard.Items, Title);
    returnValue[SPBuiltInFieldId.Body] = Body;
    string userInformation = string.Format("{0};#{1}", ThreadCreator.ID, ThreadCreator.Name);
    returnValue[SPBuiltInFieldId.Author] = userInformation;
    returnValue[SPBuiltInFieldId.Editor] = userInformation;
    returnValue.Update();
    return returnValue;
}

Advertisements

5 thoughts on “Create a Discussion Thread on a Discussion Board with Author Override

  1. Hello Fred,

    I have not the same, but similar problem. I want to replace in ‘Posted by: ‘ section of discussion post username of author with full name (Title1 + Name + LastName + Title2).
    It can be done by making a webpart that will completly override functionality of Threaded.aspx, but it is lot of work. Displayed name is only thing that I nedd to change.
    Users are stored in Sql Membership database and are 1:1 mapped to Sharepoint users that are stored in ‘MembersOfSite’ list. This list contain SP users with lot of fields, including Title1, Name, LastName, Title2 (that I need to display as discussion post author).
    Have you some idea how to do it?
    I was at it more than 4 days without success…

    Thank you for any help,
    Peter

  2. Fred i read your comment in this post: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2529769&SiteID=1
    In that post you are saying not to use the template, you say that we must cast. We should this always or only if we are using a replicator or a while?
    I never saw this casting before, and i read lots of articles, but what you say has sense…
    Can you send me the code of the minsample you mentioned on that post with the replicator, the wsstaskactivity and the cag to handle the due dates?
    Thanks a lot!!

  3. hi,
    I dont know if you faced this problem but people cant participate in a dicussion using Mozilla. On clicking reply, the body field gets distorted.I want to override this functionality of MOSS. So that if user is working on mozilla, another pop up should come where user can enter the reply.
    Any idea how this can be achieved?

  4. Chetali: Unfortunately, I don’t have to deal with browsers other than IE where I work, so I don’t know how to override the MOSS functionality that you wrote about.

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