Showing posts with label List. Show all posts
Showing posts with label List. Show all posts

Friday, March 8, 2013

Sharepoint 2013 creating a new discussion on discussions list

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



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();   
          }





Cheers :D
Luckshan Roy 

Tuesday, February 26, 2013

Getting the values from discussions list

Hi guys,

Im sharing this because I had some issues when I tried to access the discussions list values in community site (SharePoint 2013 Template). When I tried accessing the list values by specifying the list name it gave me only the values of the replies of the discussion. So what we have to do is when we initializing the list mention the view. After that you can get the discussion list's values.

If you have tried the community site you'll get an idea how discussions and the replies are stored in the same list. When you check the list in 'Management' view you'll get a better understanding. Main discussion and the related values will be stored in the top level and when you click one discussion it'll show the replies and the related value of it. So check the below code for more details.


using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
  using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Discussions List"];
SPListItemCollection listItemCln = list.GetItems(list.Views["Management"]);

foreach (SPListItem post in listItemCln)
{
//Access the discussions list values
String title = post["Title"];
}
}
}

Thank You.
Luckshan Roy.