Search This Blog

Friday, March 17, 2017

Discussion Board SharePoint 2010

PowerShell:



Discussion board is tricky to deal with. Discussion are folders and Messages are items inside folders (very simple explanation). Best way to migrate one Discussion from source to destination list is to recreate new Discussion on destination list and then to recreate all Messages inside it.

Add-PSSnapin Microsoft.SharePoint.PowerShell

$web = Get-SPWeb "http://TeamSite"
$sourceList = $web.GetList("http://TeamSite/Lists/source/")
$destinationList = $web.GetList("http://TeamSite/Lists/destination/")

#Get desired discussion by Name (or use some other identifier eg. ID)
$sourceDiscussion = $sourceList.Folders | Where-Object {$_.Name -eq "My cool subject"}

#Add new discussion to destination list
$destinationDiscussion = [Microsoft.SharePoint.Utilities.SPUtility]::CreateNewDiscussion($destinationList.Items, $sourceDiscussion.Title)
#Copy basic field values (you can copy some custom fields if needed)
$destinationDiscussion["Body"] = $sourceDiscussion["Body"]
$destinationDiscussion["Author"] = $sourceDiscussion["Author"]
$destinationDiscussion["Editor"] = $sourceDiscussion["Editor"]
$destinationDiscussion["Modified"] = $sourceDiscussion["Modified"]
$destinationDiscussion["Created"] = $sourceDiscussion["Created"]
#Add discussion
$destinationDiscussion.SystemUpdate($false)

#Get all discussion messages (maybe there is better way to get it but this works)
$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 basic field values (you can copy some custom fields if needed)
    $destinationMessage["Body"] = $sourceMessage["Body"]
    $destinationMessage["TrimmedBody"] = $sourceMessage["TrimmedBody"]
    $destinationMessage["Author"] = $sourceMessage["Author"]
    $destinationMessage["Editor"] = $sourceMessage["Editor"]
    $destinationMessage["Modified"] = $sourceMessage["Modified"]
    $destinationMessage["Created"] = $sourceMessage["Created"]
    #Add message
    $destinationMessage.SystemUpdate($false)
}


No comments:

Post a Comment