2

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 ?

1 Answer 1

2

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." } 
2
  • ok your code worked well .. but do u have a reason why my original code did not work as expected ? Commented Dec 23, 2016 at 17:24
  • i think there are 2 reasons - 1) $sourceItem = $list.items.GetItemById("270") - here you used string. 2) $newTopic = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewDiscussionReply($list,$sourceItem ); - here you should have passed only the sp list item parameter Commented Dec 23, 2016 at 17:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.