Best way to convert a SP 2013 (w/attachments) to Document Sets?

SLM 21 Reputation points
2021-07-13T21:12:56.02+00:00

What is the best way to convert a SP 2013 (w/attachments) to Document Sets?

I need to convert a SharePoint 2013 list (that has attachments) to a Document Library with Document Sets. I do not have access to SharePoint Designer and I have limited site permissions (one of the perks of working for the government - sigh).

I'm only an Intermediate level SP user so I'd need the feedback "dumbed-down" a little bit please. Any assistance would be greatly appreciated... I'm close to giving up!

Microsoft 365 and Office | SharePoint Server | For business
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Allen Xu_MSFT 13,866 Reputation points
    2021-07-14T06:30:09.457+00:00

    Hi @SLM ,

    According to your description, my understanding is that you want to all attachments in a list to be upload those attachments to a document set, right?

    Firstly, to download all attachments in a list to a local folder, you can use the below PowerShell commands.

    #Site URL and List Name variables  
    $WebURL = "http://xx/"     
    $ListName = "xxx"    
       
    #Local folder to which attachments to be downloaded  
    $DownloadPath = "C:\xx"      
       
    #Get the web  
    $Web = Get-SPWeb $WebURL  
    #Get the Library  
    $List = $Web.Lists[$ListName]      
       
    #Loop through each List item  
    foreach ($ListItem in $List.Items)  
    {     
        #Set path to save attachment  
        $DestinationFolder = $DownloadPath           
       
        #Check if folder exists already. If not, create the folder  
        if (!(Test-Path -path $DestinationFolder))          
        {              
            New-Item $DestinationFolder -type directory            
        }  
         
        #Get all attachments  
        $AttachmentsColl = $ListItem.Attachments  
       
        #Loop through each attachment  
        foreach ($Attachment in $AttachmentsColl)      
        {   
            #Get the attachment File         
            $file = $web.GetFile($listItem.Attachments.UrlPrefix + $Attachment)          
            $bytes = $file.OpenBinary()                  
       
            #Save the attachment as a file    
            $FilePath = $DestinationFolder + " \" + $Attachment  
            $fs = new-object System.IO.FileStream($FilePath, "OpenOrCreate")  
            $fs.Write($bytes, 0 , $bytes.Length)      
            $fs.Close()      
        }  
    }  
    

    You will find all attachments in the path you set in parameter $DownloadPath after exectuing the code. Then, use IE to access the document set. Click the "open with Explorer" link in the ribbon.
    114388-image.png

    Select all attachments in the local folder, and then you can upload all attahcments to the document set via a easy drag.
    114481-image.png

    Not sure which specific permission level you have to the list and the library but I suppose if you can upload/create documents/items in the list/library, you can easily achieve your requirement by above method. Anyway , have a try to this.

    ----------
    Updated----------
    @SLM ,

    There is no OOB way to convert list items to a document set with metadatas. You have to wirte your own code. Here is code which will move/migrate SharePoint List Attachments to Document Library with Created and Modified Date for your reference.

    SPSecurity.RunWithElevatedPrivileges(delegate()  
               {  
                   using (SPSite site = new SPSite("SiteURL"))  
                   {  
                       SPList docDestination = site.RootWeb.Lists["DocName"];  
                       SPFolder fldRoot = site.RootWeb.Folders[docDestination.Title];  
                       SPFileCollection flColl = null;  
                       SPList lstAttachment = site.RootWeb.Lists["ListName"];  
                       foreach (SPListItem lstItem in lstAttachment.Items)  
                       {  
                           if (lstItem.Attachments != null && lstItem.Attachments.Count > 0)  
                           {  
                               foreach (String strName in lstItem.Attachments)  
                               {  
                                   flColl = fldRoot.Files;  
                                   SPListItem listtem = docDestination.Items.Add();  
                                   SPFile FileCopy = lstItem.ParentList.ParentWeb.GetFile(lstItem.Attachments.UrlPrefix + strName);  
      
                                   string destFile = flColl.Folder.Url + "/" + FileCopy.Name;  
                                   byte[] fileData = FileCopy.OpenBinary();  
      
                                   SPFile flAdded = flColl.Add(destFile, fileData, site.RootWeb.CurrentUser, site.RootWeb.CurrentUser, Convert.ToDateTime(lstItem[SPBuiltInFieldId.Created]), Convert.ToDateTime(lstItem[SPBuiltInFieldId.Modified]));  
                                   SPListItem item = flAdded.Item;  
                                   item[SPBuiltInFieldId.Created] = Convert.ToDateTime(lstItem[SPBuiltInFieldId.Created]);  
                                   item[SPBuiltInFieldId.Modified] = Convert.ToDateTime(lstItem[SPBuiltInFieldId.Modified]);  
                                   flAdded.Item.Update();  
                               }  
      
                           }  
                       }  
                   }  
               });  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.