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.

No comments:

Post a Comment