Scenario:
You want to copy all discussions from a discussion board to another
PowerShell script:
#Copy discussions to a new discussion board $web = Get-SPWeb "http://sharepoint.com" $sourceList = $web.GetList("http://sharepoint.com/Lists/OldDiscussions/AllItems.aspx") $destinationList = $web.GetList("http://sharepoint.com/Lists/NewDiscussions/AllItems.aspx") $sourceListItems = $sourceList.Folders foreach($item in $sourceListItems) { $title = $item['Title'] write-host "Copying $title" #Get desired discussion by ID $sourceDiscussion = $sourceList.Folders | Where-Object {$_.ID -eq $item['ID']} #Add new discussion to destination list $destinationDiscussion = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewDiscussion($destinationList.Items, $sourceDiscussion.Title) #Copy field values $destinationDiscussion["Body"] = $sourceDiscussion["Body"] $destinationDiscussion["Author"] = $sourceDiscussion["Author"] $destinationDiscussion["Editor"] = $sourceDiscussion["Editor"] $destinationDiscussion["Modified"] = $sourceDiscussion["Modified"] $destinationDiscussion["Created"] = $sourceDiscussion["Created"] #Copy attachments $attachmentCollection = $sourceDiscussion.Attachments $folder = $web.GetFolder($attachmentCollection.UrlPrefix); foreach ($file in $folder.Files) { $fileName = $file.Name $bytes = $file.OpenBinary() $filePath = "$webURL/$folder/$fileName" $destinationDiscussion.Attachments.Add([System.IO.Path]::GetFileName($filePath), $bytes); } #Add discussion $destinationDiscussion.SystemUpdate($false) #Get all discussion messages $caml='<Where><Eq><FieldRef Name="ParentFolderId" /><Value Type="Integer">{0}</Value></Eq></Where>' -f $sourceDiscussion.ID $query = new-object Microsoft.SharePoint.SPQuery $query.Query = $caml $query.ViewAttributes = "Scope='Recursive'"; $sourceMessages = $sourceList.GetItems($query) foreach ($sourceMessage in $sourceMessages) { #Add new message to discussion $destinationMessage = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewDiscussionReply($destinationDiscussion) #Copy field values $destinationMessage["Body"] = $sourceMessage["Body"] $destinationMessage["TrimmedBody"] = $sourceMessage["TrimmedBody"] $destinationMessage["Author"] = $sourceMessage["Author"] $destinationMessage["Editor"] = $sourceMessage["Editor"] $destinationMessage["Modified"] = $sourceMessage["Modified"] $destinationMessage["Created"] = $sourceMessage["Created"] #Copy attachments $attachmentCollection = $sourceMessage.Attachments $folder = $web.GetFolder($attachmentCollection.UrlPrefix); foreach ($file in $folder.Files) { $fileName = $file.Name $bytes = $file.OpenBinary() $filePath = "$webURL/$folder/$fileName" $destinationMessage.Attachments.Add([System.IO.Path]::GetFileName($filePath), $bytes); } #Add message $destinationMessage.SystemUpdate($false) } } write-host 'Done'
Note:
If you have created additional column you will have to add them to the script. Also make sure the columns in the script are present in both discussion boards.