2

Im working on Sharepoint 2013 Community site template. When I manually create a discussion in discussions list liking and replying features are working fine, but when I programmatically add a dicussion in discussions list its giving the following error "Value does not fall within the expected range".

This is the C# code im using to add an item in 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();
    }
}

Check the following images for more information...

enter image description here enter image description here

Is there anyone came across this issue? Please help me out to solve this problem. Thank You

2 Answers 2

2

i think your issue might be here:

SPListItemCollection listItemCln = list.GetItems(list.Views["Management"]);

make sure that Management exists, i think you need todo the following:

using (SPSite site = new SPSite("http://site-url"))
{
    using (SPWeb web = site.OpenWeb())
    {

        // Get data from a list.
        SPList list = web.Lists["Discussions List"];
        SPView view = list.Views["Management"];
        SPListItemCollection items = list.GetItems(view);

        SPListItem item = list.Items.Add();
        item["Title"] = "New Post";
        item["Body"] = "Body of the post";
        item.Update();
    }
}

EDIT

after looking online it seems to be a common issue:

using (SPSite site = new SPSite("http://site-url"))
{
    using (SPWeb web = site.OpenWeb())
    {

        SPList discussionList = web.Lists["Discussions List"];

        SPListItem lstItm = SPUtility.CreateNewDiscussionReply(discussionList);

        lstItm[SPBuiltInFieldId.Body] = "Body of the post";
        lstItm["ContentTypeId"] = list.ContentTypes.BestMatch(SPBuiltInContentTypes.Message);
        lstItm.Update();
    }
}

last part is taken from here to get it working:

https://stackoverflow.com/questions/14123357/sputility-createnewdiscussionreply-creates-a-discussion-item

if that doesnt work that you have this that should do exactly what you want

http://www.etechplanet.com/codesnippets/post-a-reply-on-sharepoint-discussion-thread-programmatically-using-c-code.aspx

11
  • I tried your code but its giving the same error. I checked whether the "Management" view is existing also everything is there but I couldn't find the reason for this issue. In your given code you have only changed web.Lists["Discussions List"].Views["Management"] instead of list.Views["Management"]. What is the difference between both the code??
    – Roy Justin
    Mar 7, 2013 at 10:28
  • ok thats no problem, im slightly confused tho asto why your getting the view? are you using it somewhere else?
    – Ali Jafer
    Mar 7, 2013 at 10:34
  • try the updated code, sorry im just going off the top of my head as i cba to test the code ;) , but that should work as its inline whith what msdn says
    – Ali Jafer
    Mar 7, 2013 at 10:37
  • Im using the view to get the list value because when I try to get the discussions values by specifying only the list will give the values of the replies, by specifying the view only we can get the values of the discussions. Btw I tried the updated the code also same issue coming.
    – Roy Justin
    Mar 7, 2013 at 11:52
  • Now im trying to check the log files and find out the error. I'll post the futher details...
    – Roy Justin
    Mar 7, 2013 at 11:53
0

Hi guys I found the reason why replying to discussions aren't working. Even though discussions list is a type of list there is a different way of adding items programmatically. I'll share my code below

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.