none
import/export folder RRS feed

  • Question

  • Hi to all,

    how can i to export a single folder from document library to another folder of a subsite?

    I tried with "Export-SPWeb" and "Import-SPWeb" (for to do the import in folder of subsite) but the script export all files of old folder and not the specific folder that i have indicated.

    Thanks to all

    Thursday, February 23, 2017 3:34 PM

All replies

  • You can use Open with explorer if you don't worry about properties and versions else you will have to go for customized solution using PowerShell or CSOM

    Thanks

    Kislay

    Mark as answer if it resolves your query

     
    Thursday, February 23, 2017 5:21 PM
  • Hi,

    If you want to copy a single folder from document library to another folder of a sub site, the following code for your reference:

    private static void RecursiveCopy(SPList objSourceList, SPFolder objSourceFolder, SPFolder objDestinationFolder)
    {
    	SPListItemCollection objItems = ((SPDocumentLibrary)objSourceList).GetItemsInFolder(objSourceList.DefaultView, objSourceFolder);
    
    	foreach (SPListItem objItem in objItems)
    	{
    		//If it's a file copy it.
    		if (objItem.FileSystemObjectType == SPFileSystemObjectType.File)
    		{
    
    			byte[] fileBytes = objItem.File.OpenBinary();
    			string DestinationURL = string.Format(@"{0}/{1}", objDestinationFolder.Url, objItem.File.Name);
    
    			//Copy the file.
    			SPFile objDestinationFile = objDestinationFolder.Files.Add(DestinationURL, fileBytes, true);
    			objDestinationFile.Update();
    		}
    		else
    		{
    			string dirURL = string.Format(@"{0}/{1}", objDestinationFolder.Url, objItem.Folder.Name);
    			SPFolder objNewFolder = objDestinationFolder.SubFolders.Add(dirURL);
    			objNewFolder.Update();
    
    			//Copy all the files in the sub folder
    			RecursiveCopy(objSourceList, objItem.Folder, objNewFolder);
    		}
    	}
    }
    
    public static void CopyListItems(string SourceSiteURL, string DestinationSiteURL, string ListName,string folderName)
    {
    	string DestinationURL = string.Empty;
    
    	using (SPSite SourceSite = new SPSite(SourceSiteURL))
    	{
    		using (SPWeb SourceWeb = SourceSite.OpenWeb())
    		{
    			using (SPSite DestinationSite = new SPSite(DestinationSiteURL))
    			{
    				using (SPWeb DestinationWeb = DestinationSite.OpenWeb())
    				{
    					DestinationWeb.AllowUnsafeUpdates = true;
    
    					//Get the QA Forms Document libarary from the source web
    					SPList objSourceList = SourceWeb.Lists[ListName];
    
    					SPList objDestinationList = null;
    
    					try
    					{
    						objDestinationList = DestinationWeb.Lists[ListName];
    					}
    					catch
    					{
    						//Create a list in the destination web
    						DestinationWeb.Lists.Add(ListName, string.Empty, SPListTemplateType.DocumentLibrary);
    					}
    
    					objDestinationList = DestinationWeb.Lists[ListName];
    
    					SPFolder objSourceFolder = SourceWeb.GetFolder(objSourceList.RootFolder.Url + "/"+folderName);
    					SPFolder objDestinationFolder = DestinationWeb.GetFolder(objDestinationList.RootFolder.Url + "/"+folderName);
    					if (!objDestinationFolder.Exists)
    					{
    						objDestinationList.RootFolder.SubFolders.Add(objDestinationList.RootFolder.Url + "/" + folderName);
    					}
    					objDestinationFolder = DestinationWeb.GetFolder(objDestinationList.RootFolder.Url + "/" + folderName);
    					//Recursively copy all the files and folders
    					RecursiveCopy(objSourceList, objSourceFolder, objDestinationFolder);
    
    					DestinationWeb.Update();
    					DestinationWeb.AllowUnsafeUpdates = false;
    				}
    			}
    		}
    	}
    }
           
    static  void Main(string[] args)
    {
    	CopyListItems("http://sp13/sites/team", "http://sp13/sites/team/subsite", "DL","TestFolder");
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, February 24, 2017 4:18 AM
    Moderator