Hi guys have you all ever tried creating a new discussion on discussions list programmatically?? I tried recently and got an issue. Like we all normally create and add an item to a list I tried adding a discussion item on discussions list. Well it got created as the way I want but when I tried to reply to that discussion a message box popped and gave this following error message.
"Value does not fall within the expected range"
This is the C# code I used to create and add a new disussion on discussions list.
using (SPSite site = new SPSite("http://site-url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Discussions List"];
SPListItemCollection listItemCln = list.GetItems(list.Views["Management"]);
SPListItem item = list.Items.Add();
item["Title"] = "New Post";
item["Body"] = "Body of the post";
item.Update();
}
}
This is the disussion got created
Cheers :D
Luckshan Roy
"Value does not fall within the expected range"
This is the C# code I used to create and add a new disussion on discussions list.
using (SPSite site = new SPSite("http://site-url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Discussions List"];
SPListItemCollection listItemCln = list.GetItems(list.Views["Management"]);
SPListItem item = list.Items.Add();
item["Title"] = "New Post";
item["Body"] = "Body of the post";
item.Update();
}
}
This is the disussion got created
This is the error message box popped out when I tried to reply that discussion
So when I checked the log file it gave me this error message.
Failed to cache field with id "{1805e563-22cf-44ed-96f5-58ebb8a6cb80}", overwrite=0
SocialRESTExceptionProcessingHandler.DoServerExceptionProcessing - SharePoint Server Exception [System.ArgumentException: Value does not fall within the expected range. at Microsoft.SharePoint.Utilities.SPUtility.CreateNewDiscussionReply(SPListItem parent) at Microsoft.SharePoint.ServerStub.Utilities.SPUtilityServerStub.InvokeStaticMethod??(String methodName, XmlNodeList xmlargs, ProxyContext proxyContext, Boolean& isVoid) at Microsoft.SharePoint.Client.ServerStub.InvokeStaticMethodWithMonitoredScope(Stri??ng methodName, XmlNodeList args, ProxyContext proxyContext, Boolean& isVoid)]
Answer
I did some research and got some online help from others and found the solution. Discussions list is a type of list but we can't add items to it like we add on other lists. So there is a different way of adding the discussions on discussions list. This is working code:
using (SPSite site = new SPSite("http://site-url"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Discussions List"];
SPListItem listItem = SPUtility.CreateNewDiscussion(list, "New Post");
listItem["Body"] = "Body of the post";
listItem.Update();
}
}
Luckshan Roy
good post
ReplyDelete