Unable to add a Reply to a Discussion Board item. Exception calling “GetItemById” with 1 argument(s): “Value does not fall within the expected range”

sharepoint.stackexchange https://sharepoint.stackexchange.com/questions/203377

Question

I have a Discussion Board list inside my team site collection in SharePoint 2013.

Now I want to add some replies to existing Discussion Board items using PowerShell. First I have a Discussion with with Id = 270 as follows:-

enter image description here

so I wrote this to add a reply to it:-

$web = Get-SPWeb "http://t****01/" 
$list=$web.Lists.TryGetList("News & Announcements")
if($list -ne $null)
{ 
$sourceItem = $list.items.GetItemById("270")
$newTopic = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewDiscussionReply($list,$sourceItem ); 
$newTopic["Body"] = "Hi";
$newTopic["Modified"] = "12/20/2016 14:01"
$newTopic["Created"] = "12/20/2016 14:01"  
$user = $web.EnsureUser("\test.user")  
$newTopic["Editor"] = $user  
$newTopic["Author"] = $user  
$newTopic.UpdateOverwriteVersion() 

Write-Host $newTopic.Title " discussion topic is created successfully"
}
else
{ 
Write-Host "List does not exists." 
}

but I got this exception and the reply was not created:-

Exception calling "GetItemById" with "1" argument(s): "Value does not fall within the expected range." At line:3 char:1 + $sourceItem = $list.items.GetItemById("270") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentException

Cannot find an overload for "CreateNewDiscussionReply" and the argument count: "2". At line:4 char:1 + $newTopic = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewDiscussionReply ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : NotSpecified: (:) [], MethodE

so can anyone advice what is the problem inside my PowerShell script ?

Was it helpful?

Solution

Try it as below:

$web = Get-SPWeb "http://t****01/" 
$list=$web.Lists.TryGetList("News & Announcements")
if($list -ne $null)
{ 
    $sourceItem = $list.GetItemById(270)
    $newTopic = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewDiscussionReply($sourceItem); 
    $newTopic["Body"] = "Hi";
    $newTopic["Modified"] = "12/20/2016 14:01"
    $newTopic["Created"] = "12/20/2016 14:01"  
    $user = $web.EnsureUser("\test.user")  
    $newTopic["Editor"] = $user  
    $newTopic["Author"] = $user  
    $newTopic.UpdateOverwriteVersion() 

    Write-Host $newTopic.Title " discussion topic is created successfully"
}
else
{ 
    Write-Host "List does not exists." 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top