Posted on:
Categories: PowerShell;SharePoint
Description:

​If you ever need to change protocol for the sites on your farm, you may want the ability to back up, and change the QuickLinks saved to users' MySites. These are the links used for the "Connect to Office" functionality.  These sites are kept in a hidden list on your mysite accesible either through "Managing your SharePoint sites" or via MysiteHostURL/_layouts/MyQuickLinks.aspx

 

If you implement a protocol change for any site with links in this list, these links will be broken. The Scripts below will allow you to backup the current state of all users' quick links as well as changing them from http to https and back.

With a simple modification to the script, you could also target a single user.

 

You will need to create the C:\_UProfileLinks directory before running this script.

The script below will export all links in their current state to the XML file "C:\_UProfileLinks\ddMMMyyyyHHmmss.xml" .

You will need to run the script with an account that has permission on the User Profile Service as well as possibly running the script from an elevated command prompt as administrator in order to write to the file system.

You also need to change line 15 in the script to include a valid ULR hosted by the farm to create a service context.

Below is the script for backing up the links:

 

 

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {  
   Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
 }  
$folder = "C:\_UProfileLinks"
$date = Get-Date -format "ddMMMyyyyHHmmss"

$Path = $folder + "\" + $date + ".xml"
$XmlWriter = New-Object System.XMl.XmlTextWriter($Path,$Null)

$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"

#------- YOU MUST CHANGE THE URL BELOW ----------------
$url = "http://AnyURLInTheFarm"  #Site only for getting context, this can be any url hosted in the farm
#------- YOU MUST CHANGE THE URL ABOVE ----------------

$site = Get-SPSite $url   
$context = Get-SPServiceContext($site)   
$UProfileMgr = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)   

$UProfileMgrEnum = $UProfileMgr.GetEnumerator()   

$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement('SiteCollection')
$XmlWriter.WriteAttributeString('SPSite', $site.Url)

foreach($UProfile in $UProfileMgrEnum ) 
{  
 Try  
 {  
  If ($UProfileMgr.UserExists($($UProfile["AccountName"])))
  {
  $qlm = $UProfile.QuickLinks
  $qlmLinks = $qlm.GetItems()
   If ($qlmLinks -ne $null)
   {
   $xmlWriter.WriteStartElement('Account')
   $xmlWriter.WriteAttributeString('Username', $UProfile["AccountName"])
    foreach($qlink in $qlmLinks)
    {
    $xmlWriter.WriteStartElement('Link')
    $XmlWriter.WriteAttributeString('Url', $qlink.Url)
    $xmlWriter.WriteEndElement()
    }
   $xmlWriter.WriteEndElement()
   }
  else 
   {
   #Do nothing
   }
  }
 }  
 Catch  [Exception]
 {  
  Return $_.Exception.Message
  $xmlWriter.Flush()
  $xmlWriter.Close()
 }
}
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()

 


To change the links from http/80 to https/443 you can use the script below.

(If some users already have the target protocl in place, this script will simply ignore them. It only changes http links to https (default).

You will need to create the C:\_UProfileLinks directory before running this script.

This script also outputs a log to the file C:\_UProfileLinks\ConversionLog_ddMMMyyyyHHmmss.xml showing the changes applied.

You also need to change line 15 in the script to include a valid ULR hosted by the farm to create a service context.

There are 3 lines to change to implement a change from https/443 to http/80.

Change all 3 lines to have the script flip links from https/443 to http/80:


 

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {  
   Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
 }  
$folder = "C:\_UProfileLinks"
$date = Get-Date -format "ddMMMyyyyHHmmss"

$Path = $folder + "\ConversionLog_" + $date + ".xml"
$XmlWriter = New-Object System.XMl.XmlTextWriter($Path,$Null)

$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"

#------- YOU MUST CHANGE THE URL BELOW ----------------
$url = "http://AnyURLInTheFarm"  #Site only for getting context, this can be any url hosted in the farm
#------- YOU MUST CHANGE THE URL ABOVE ----------------

$site = Get-SPSite $url   
$context = Get-SPServiceContext($site)   
$UProfileMgr = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)   

$UProfileMgrEnum = $UProfileMgr.GetEnumerator()   

$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement('SiteCollection')
$XmlWriter.WriteAttributeString('SPSite', $site.Url)

foreach($UProfile in $UProfileMgrEnum ) 
{  
 Try  
 {  
  If ($UProfileMgr.UserExists($($UProfile["AccountName"])))
  {
  $qlm = $UProfile.QuickLinks
  $qlmLinks = $qlm.GetItems()
   If ($qlmLinks -ne $null)
   {
   $xmlWriter.WriteStartElement('Account')
   $xmlWriter.WriteAttributeString('Username', $UProfile["AccountName"])
    foreach($qlink in $qlmLinks)
    {
     $xmlWriter.WriteStartElement('Link')
     $XmlWriter.WriteAttributeString('OG_Url', $qlink.Url)
     $xmlWriter.WriteEndElement()
     #------------------------------------
     $ogURL = $qlink.Url
     
     # 1/3 change condition to "https" to match changing FROM https TO http
     If ($ogURL.Contains("http:"))
     {
      # 2/3 change the parameters here to ("https:","http:") to change links FROM https TO http
      $newProtocolURL = $ogURL.Replace("http:","https:")
      # 3/3 change the parameters here to    (":443",":80") to change links FROM 443/https TO 80/http
      $finalURL = $newProtocolURL.replace(":80",":443")
      
      $qlink.Url = $finalURL
      $qlink.Commit()
      #------------------------------------
      $xmlWriter.WriteStartElement('Link')
      $XmlWriter.WriteAttributeString('New_Url', $qlink.Url)
      $xmlWriter.WriteEndElement()
     }
     Else
     {
      # do nothing
     }
    }
   $xmlWriter.WriteEndElement()
   }
  else
   {
    # do nothing
   }
  }
 }  
 Catch  [Exception]
 {  
  Return $_.Exception.Message
  $xmlWriter.Flush()
  $xmlWriter.Close()
 }
}
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()