Thursday, August 15, 2013

Move (Personal) Documents into SkyDrive Pro after migrating from SharePoint 2010 to SharePoint 2013

Introduction

If you have working on migrating SharePoint 2010 personal sites to SharePoint 2013 personal sites;  documents migration may become a headache. The expectation of the action is
"The Personal and Shared document libraries in SharePoint Server 2010 are combined into the SkyDrive Pro document library on the user's My Site in SharePoint Server 2013. Items from the Shared folder are stored in the Shared with Everyone folder in SharePoint Server 2013. Items from the Personal folder are stored at the root of the SkyDrive Pro document library and only shared with the owner of the My Site." - MicroSoft

Problem Background

But in our case, the documents didn't shift under SkyDrive but they were seen under site contents as another document library.

So I wrote a console application to move all the documents to SkyDrive. Here I'm presenting a simple code snippet which you have to modify to use other document library and sub folders under document library. 

Solution 

private static void MovePersonalDocuments(SPWeb web)
{
 SPFolder folder = web.GetFolder("Personal Documents");
 SPFileCollection collFile = folder.Files;
 ///*Copy the files to a generic List of type SPFile*/
 List listFiles = new List(collFile.Count);
 foreach (SPFile oFile in collFile)
 {
  listFiles.Add(oFile);
 }
 SPList list = (SPDocumentLibrary)web.Lists["Documents"];
 for (int i = 0; i < listFiles.Count; i++)
 {
    SPFile movefile = collFile[0];
    Console.WriteLine("Moving File: " + movefile.Name);
           web.AllowUnsafeUpdates = true;
    byte[] fileBytes = movefile.OpenBinary();
    string destUrl = list.RootFolder.Url + "/" + movefile.Name;
    movefile.MoveTo(destUrl, true);
           web.AllowUnsafeUpdates = false;
   Console.WriteLine("Success");
 }
}

Conclusion 

  • Please note that I have only considered moving Personal Documents.
  • The code will help to move Shared Documents and Sub folders as well. 

2 comments:

  1. would suggest to use the following instead of custom code:
    Microsoft.SharePoint.Portal.UserProfilesMySiteDocumentMoveUtility.FirstRunDocumentMove(spweb)

    it covers both, personal and shared doclibs (including localization of library names)

    ReplyDelete