Friday, June 15, 2007

Creating a new discussion item on Discussion Board

Hi all,

Ever tried to use C# to add list items to Tasks list? Annoumcements? piece of cake.

To a Document Library? saving a file, updaging meta data - no problem.

But - did you ever try to create a new discussion on a Discussion Board? Well, there is more to it than you think.

My customer had me create a new discussion automatically for every task created on the site. This way - when ever you assign a task to a group or a person they can communicate with each other and work a solution together.

Doing a little research i found that creating a new discussion is a bit different than working with a normal list that we are used to.

First you must create a new discussion using the SPUtility.CreateNewDiscussion method.
This method reiceves the name of the discussion and the SPListItemCollection of the SPList item representin the discussion.

well, for example if you have a discussion boared called "What say you" your code should look like this:

SPList list = web.Lists["What say you"];
SPListItemCollection myListItems = list.Items;
SPListItem d = SPUtility.CreateNewDiscussion(myListItems, "Talk about Task X");
d["Body"] = "What say you about task X?";
d.Update();

And a reply will look like this:

SPListItem r = SPUtility.CreateNewDiscussionReply(d);
r["Body"] = "Looks interesting... Doesnt it?";
r.Update();


Well, hope this helps...

14 comments:

Anonymous said...

Is there any way to affect the "Created By" value when using CreateNewDiscussion?

I need to collect the "feedback" for each completed Task in a workflow.

CreateNewDiscussion is great, but instead of Created By "System Account", I want the Created By value to be the person who the task was assigned to.

I can get an SPUser object, but don't see any way to change the Created By value then Update() the discussion thread.

Anonymous said...

Thanks a lot...

I was using the Items.Add() function but there was an issue with the bread crump of Sharepoint. It looked like that :

Site > Team Discussion > 19_0.000

It was very awfull... With your code, it display the good bread crump ;)

shekar said...

Can we create multiple discussions for "Talk about Task X"

Shai Petel said...

Hi Fred,
After creating the discussion, you can get it as a SPListItem, and update the created by using item["Author"] = ..., like any other item. See this discussion for example:
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/21f65f64-54d8-450f-b261-3d2f4ae42bd9/

Shekar,
A discussion is implemented as a folder in a list. Since you cannot create 2 folders in the same name, I suspect you should not be able to create a new disucssion in the same name. I suspect you may have the same title for 2 discussions, but the folder name cannot be the same for sure. Not sure if SharePoint will automatically create a unique folder name for you or fail, you have to check that.

Anonymous said...

hi,
1)I am facing one problem.
2)I have created a discussion Board list, created some custom column, one of them is LookUp column to a other list.
3)I uses SpUtility.CreateNewDiscussion for creating New Post.
4) I have created a Feature for installing this list with the help of Solution Generator.
5) Now after creating a list using this feature, CreateNewDiscussion Methode starts giving error "value does not fall in the range"

Shai Petel said...

Does the lookup column you created work in the new site, in your custom discussion board list? Try the web UI, can you create a new discussion through it?

I would also look at the SharePoint logs.

I think it has something to do with the lookup column looking for a list that does not exist perhaps?

Good luck.

Page said...

Hi, I had the same problem as anonymous...

But all I had to do was delete the list and create a new discussion list. I think it has something to do with the content type. I think if u delete the default discussion content type it breaks. You can add additional content types but dont remove the default one... by the way this is just an opinion.

Unknown said...

Hi im working on SharePoint 2013 community site, I have added many discussion items to the discussions list using the method you have mentioned above but in the category webpart the counts of discussions are not accurate. Is there a reason behind it??

Shai Petel said...

I have not encountered this issue, can you perhaps share you code?

Unknown said...

Hello - I have a similar issue.

Above code works perfectly for a OOTB discussion board. In my case we a custom discussion board with custom content type which is inherited from OOTB "Discussion"/"Message" content types. And I am trying to populate discussions programmatically.

SPListItem discussion = SPUtility.CreateNewDiscussion(spWeb.Lists["DISCUSSION"], “NOT WORKING”);

The above statement is throwing "Value does not fall within the expected range" exception.

Thanks in advance !

Unknown said...

Hi - I have a similar issue. The above code works for OOTB SharePoint 2010 Discussion Board. In my case, I have a custom discussion board with Custom Site Columns added. And I am trying to populate discussions programmatically. Similar to what you have mentioned:

SPListItem discussion = SPUtility.CreateNewDiscussion(spWeb.Lists["DISCUSSION"], “NOT WORKING”);

The above statement is throwing "Value does not fall within the expected range" exception.

Please advise.

Thanks !

Shai Petel said...

Hi,
Interesting - I would have to know how you created this discussion board.
Did you create a custom list and add the content type?

Also, what is the business requirement that made you customize the discussion board?

Unknown said...

Thanks for your response !

Content Types: We created two custom content types i.e. xyzDiscussion(Parent Content Type - Discussion) & xyzMessage(Parent Content Type - Message). Added few Site Columns to xyzDiscussion.

List: And for custom discussionboard list definition, we added both xyzDiscussion & xyzMessage content types. Everything works perfectly alright except for populating discussions.

Please let me know if you need any other info.

Thanks in advace !

Shai Petel said...

I suspect your custom list definition may be the issue.

I suggest you compare it with the original OOB list definition, but still - you might have to use the original list definition and not create a new one.

You should be able to create a new list template based on the OOB discussion board definition and add your custom content types to it.

Good luck!