MOSS 2007-SP 2010 Migration · PowerShell · SharePoint 2010

My Links Migration from MOSS 2007 to SP 2010- Part 2


Hi There,

I discussed about My Links migration in post 1 here https://pathikhrawal.wordpress.com/2011/07/14/my-site-migration-from-moss-2007-to-sp-2010/  . In this post I am going to provide code snippet for migration of My links.

The actual migration process involves 2 major activities

1) EXPORT- Export My Links on MOSS 2007’s My Site (C# Code)

2) IMPORT- Import all My Links on SharePoint 2010’s My Site (PowerShell)

Export Process:

Details Steps

1) You can create a console application/Windows Applciation or WPF Application in Visual Studio 2008 with .Net Framework 2.0

2) Create an XML with links data. (By running our application on MOSS 2007 which will create an XML file)

XML format:

<UserProfiles>
    <UserProfile userNames=“user1” accountName=“VM-MOSS01\user1”>
<UserLinks>
     <Link>
         <title>hello</title>
        <group>General</group>
        <groupType>General</groupType>
        <privacy>Public</privacy>
       <url>
         <![CDATA[ http://teamsites.moss.com/  ]]>
       </url>
  </Link>
</UserLinks>
  </UserProfile>
  </UserProfiles>
 
 
The actual code is provided below ( this is function you can copy to you console application). The
only one Parameter is URL- You need to provide the Central Admin URL
 
Code to export My links
public static void ExportUserProfiles(string url)
       {
           //accessing the profile manager to get profile properties
           UserProfileManager profileManager = default(UserProfileManager);
 
           using (SPSite site = new SPSite(url))
           {
               ServerContext serverContext = ServerContext.GetContext(site);
               profileManager = new UserProfileManager(serverContext);
           }
 
           //declaration of xml file
           XmlDocument xmlDoc = null;
           XmlNode userProfilesNode = null;
 
                  //creating new XMl document
                   xmlDoc = new XmlDocument();
                   XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(“1.0”, “UTF-8”, “yes”);
                   xmlDoc.AppendChild(xmlDeclaration);
 
                   //creation first node of userprofile
                   userProfilesNode = xmlDoc.CreateElement(“UserProfiles”);
                   xmlDoc.AppendChild(userProfilesNode);
 
 
           foreach (UserProfile userProfile in profileManager)
           {
               try
               {
                  
 
                   //creating user profile node
                   XmlNode userProfileNode = xmlDoc.CreateElement(“UserProfile”);
                   userProfilesNode.AppendChild(userProfileNode);
                   XmlAttribute userProfileAttribute;
 
                   // creating username property for profile node
                   userProfileAttribute = xmlDoc.CreateAttribute(“userNames”);
                   userProfileAttribute.Value = userProfile[PropertyConstants.UserName].Value.ToString();
                   userProfileNode.Attributes.Append(userProfileAttribute);
 
                   // creating accountName property for profile node
                   userProfileAttribute = xmlDoc.CreateAttribute(“accountName”);
                   userProfileAttribute.Value = userProfile[PropertyConstants.AccountName].Value.ToString();
                   userProfileNode.Attributes.Append(userProfileAttribute);
 
 
                   ////creating myLink node in export xml file
                   XmlNode myLinksNode = xmlDoc.CreateElement(“UserLinks”);
                   userProfileNode.AppendChild(myLinksNode);
 
                   QuickLinkManager linkManager = userProfile.QuickLinks;
 
                   foreach (QuickLink link in linkManager.GetItems())
                   {
                       try
                       {
                           XmlNode linkNode = xmlDoc.CreateElement(“Link”);
                           myLinksNode.AppendChild(linkNode);
 
                           XmlNode linkTitleNode = xmlDoc.CreateElement(“title”);
                           linkNode.AppendChild(linkTitleNode);
                           linkTitleNode.InnerXml = link.Title;
 
                           XmlNode linkGroupNode = xmlDoc.CreateElement(group);
                           linkNode.AppendChild(linkGroupNode);
                           linkGroupNode.InnerXml = link.Group;
 
                           XmlNode linkGroupTypeNode = xmlDoc.CreateElement(“groupType”);
                           linkNode.AppendChild(linkGroupTypeNode);
                           linkGroupTypeNode.InnerXml = link.GroupType.ToString();
 
                           XmlNode linkPrivacyeNode = xmlDoc.CreateElement(“privacy”);
                           linkNode.AppendChild(linkPrivacyeNode);
                           linkPrivacyeNode.InnerXml = link.PrivacyLevel.ToString();
 
                           XmlNode linkUrlNode = xmlDoc.CreateElement(“url”);
                           linkNode.AppendChild(linkUrlNode);
                           XmlCDataSection linkValCData;
 
                           linkValCData = xmlDoc.CreateCDataSection(link.Url);
                           linkUrlNode.AppendChild(linkValCData);
 
                       }
                       catch (Exception ex)
                       {
                       }
 
                   }
 
                   xmlDoc.Save(“MyLinksExported.XML”);
 
               }
 
               catch (Exception ex)
               {
               }
 
 
           }
       }

 

Import

Now for Import I am going to provide you PowerShell script to import all links for all users.

Details Steps:

1) Copy the XML file to a folder

2) Create a New ImportLinks.PS1 in the same folder and copy below code to PS1 file

 
$commonconfig = “links.xml”
 
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
#region scripts body
cls
#region snapin inclusion
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
# if snapin is not installed then use this method
[Void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
#endregion
 
[xml]$configurations = Get-Content “$executingScriptDirectory/$commonconfig” 
 
 
$site = newobject Microsoft.SharePoint.SPSite(“http://w7-pathikr:15767”)
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
$ProfileManager = newobject Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
 
#provision managed accounts
 
    foreach($UserProfile in $configurations.UserProfiles.UserProfile)
    {
       
        Write-Host $UserProfile.accountName;
        $User = $ProfileManager.GetUserProfile($UserProfile.accountName);
        $quickLinkManager = $User.QuickLinks;
             
       foreach($Link in $UserProfile.UserLinks.Link)
        {
           $title=$Link.title;
           $group=$Link.group;
           $groupType=$Link.groupType;
           $privacy=$Link.privacy;
           $url=$Link.url.InnerText;
           $quickLinkManager.Create($title, $url, $group, $null, $privacy)       
        
        }
       
    }
 
 
 
 

 

Now Run the PowerShell script on SharePoint 2010 Management Shell (Run as Administrator)

Thats all ! All Links from MOSS 2007’s My Site to SP 2010’s My Site for all users.

..

Advertisements

10 thoughts on “My Links Migration from MOSS 2007 to SP 2010- Part 2

  1. Thanks, this is great. I had 2 issues with it which I resolved and may be beneficial to others:

    For the export code the following code needs to be moved above the “foreach (UserProfile userProfile in profileManager)” line or the XML node will keep getting overwritten creating an XML document with only 1 user in it:

    //creating new XMl document
    xmlDoc = new XmlDocument();
    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration(“1.0”, “UTF-8”, “yes”);
    xmlDoc.AppendChild(xmlDeclaration);
    //creation first node of userprofile
    userProfilesNode = xmlDoc.CreateElement(“UserProfiles”);
    xmlDoc.AppendChild(userProfilesNode);

    Then in the import script I found that I needed to change line ” foreach($Link in $configurations.UserProfiles.UserProfile.UserLinks.Link)” to “foreach($Link in $UserProfile.UserLinks.Link)” instead, or the arguments would all be empty.

    Thanks again for this as it assisted greatly with our migration of links from 2007 to 2010.

  2. In the c# code, the statement
    XmlNode linkGroupTypeNode = xmlDoc.CreateElement(“groupType”);
    linkNode.AppendChild(linkGroupTypeNode);
    linkGroupTypeNode.InnerXml = link.GroupType.ToString();

    should be modified as

    XmlNode linkGroupTypeNode = xmlDoc.CreateElement(“groupType”);
    linkNode.AppendChild(linkGroupTypeNode);
    QuickLinkGroupType groupType=link.GroupType;
    linkGroupTypeNode.InnerXml =groupType.ToString();

    in order to create the userspecified group Type as well

    Also,the last statement in the the powershell script should be
    $quickLinkManager.Create($title, $url,$groupType, $group, $privacy)

  3. I have two questions:

    1. For the PowerShell script, shouldn’t $commonconfig = “MyLinksExported.XML.xml” and not $commonconfig = “links.xml” to be consistent with the exported XML file.

    2. what is the URl http://w7-pathikr:15767 the site for?

    Thanks!

  4. Hi,

    Running into an issue with the My Links. The USERLINKS table only has 18 records and I know there are many more links than that because I have links setup under profile and they are not listed in this table.

    I also ran this query and it came up with 18:

    Use SharedServices1_DB

    Select

    UP.NTName,

    UP.PreferredName,

    UP.Email,

    UL.Title,

    UL.URL

    From

    UserLinks UL inner join UserProfile_Full UP on UL.recordId = UP.recordID

    Do you have any clue where else those links could be stored? I’ve been looking everywhere in the DB and can’t find them. Running MOSS 2007 SP3.

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s