Not too long ago, I had a project where we were upgrading a client environment from SharePoint 2016 to Subscription Edition and one of the tasks was to move to new URL as well. All good when we arrived at the part where we needed to show social comments on pages. SharePoint used to have really nice PowerShell cmdlet called “Move-SPSocialComment” but apparently in SPSE it does not work any more.

So without being to lazy I had to reverse engineer what this cmdlet does and wrote my own function based on the source code.

Exporting current links from database

First task was to get all URLs of pages where social comments were posted at. That was easily achieved with a select statement on a UPS Social database.

Next, a mapping needed to be done and a new column in Excel was added to replace old URL with new URL.

After that, a script was executed to replace links in database. This code was reverse engineered from SharePoint cmdlet and uses the same code as the original.


$dbServer="dbserver"
$socialDb="SP2022_SA_UPS_Social"
$path="c:\temp\socialLinks.csv"

$csv=import-csv $path


# Define connection string (modify as needed)
$connectionString = "Server=$dbServer;Database=$socialDb;Integrated Security=True"

$upsProxy=Get-SPServiceApplicationProxy | ?{$_.typename -like "*user*"}
# Define parameters
$partitionID = [Guid]"0C37852B-34D0-418e-91C6-2AC25AF4BE5B"  # Replace with actual partitionID
$correlationId = [Guid]::NewGuid() # Replace with the actual correlation ID logic


foreach($c in $csv){
		
	$oldUrl = $c.old # Replace with actual old URL
	$newUrl = $c.newUrl # Replace with actual new URL

	# Create SQL connection
	$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
	$connection.Open()

	# Create SQL command
	$command = $connection.CreateCommand()
	$command.CommandText = "dbo.proc_SocialData_MergeSocialNotes"
	$command.CommandType = [System.Data.CommandType]::StoredProcedure

	# Add parameters
	$command.Parameters.Add((New-Object System.Data.SqlClient.SqlParameter("@partitionID", [System.Data.SqlDbType]::UniqueIdentifier))).Value = $partitionID
	$command.Parameters.Add((New-Object System.Data.SqlClient.SqlParameter("@correlationId", [System.Data.SqlDbType]::UniqueIdentifier))).Value = $correlationId
	$command.Parameters.Add((New-Object System.Data.SqlClient.SqlParameter("@oldUrl", [System.Data.SqlDbType]::VarChar, 255))).Value = $oldUrl
	$command.Parameters.Add((New-Object System.Data.SqlClient.SqlParameter("@newUrl", [System.Data.SqlDbType]::VarChar, 255))).Value = $newUrl

	# Output parameter
	$successParam = New-Object System.Data.SqlClient.SqlParameter("@success", [System.Data.SqlDbType]::Bit)
	$successParam.Direction = [System.Data.ParameterDirection]::Output
	$command.Parameters.Add($successParam)

	# Execute command
	$ex=$command.ExecuteNonQuery()

	# Check output parameter value
	$success = [bool]$command.Parameters["@success"].Value

	# Close connection
	$close=$connection.Close()

	# Return success value
	if( $success){
		write-host "Migrate $oldUrl to $newUrl success"
	}
	else{
		write-host "Migrate $oldUrl to $newUrl error" -foregroundcolor red
	}
}

Hope it helps someone…

Posted in

Leave a comment