Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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:

http://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

share|improve this answer
    
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?? –  Justin Roy Mar 7 '13 at 10:28
    
ok thats no problem, im slightly confused tho asto why your getting the view? are you using it somewhere else? –  ali Sharepoint Mar 7 '13 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 Sharepoint Mar 7 '13 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. –  Justin Roy Mar 7 '13 at 11:52
    
Now im trying to check the log files and find out the error. I'll post the futher details... –  Justin Roy Mar 7 '13 at 11:53
show 6 more comments

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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