开发者

Copy folders when copying list items from source to destination

This is my code to copy files in a list from source to destination. Using the code below I am only able to copy files but not folders. Any ideas on how can I copy the folders and the files within those folders?

using (SPSite objSite = new SPSite(URL))
            {
                using (开发者_如何学JAVASPWeb objWeb = objSite.OpenWeb())
                {
                    SPList objSourceList = null;
                    SPList objDestinationList = null;

                    try
                    {
                        objSourceList = objWeb.Lists["Source"];
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("Error opening source list");
                        Console.WriteLine(ex.Message);
                    }

                    try
                    {
                        objDestinationList = objWeb.Lists["Destination"];
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error opening destination list");
                        Console.WriteLine(ex.Message);
                    }

                    string ItemURL = string.Empty;
                    if (objSourceList != null && objDestinationList != null)
                    {
                        foreach (SPListItem objSourceItem in objSourceList.Items)
                        {
                            ItemURL = string.Format(@"{0}/Destination/{1}", objDestinationList.ParentWeb.Url, objSourceItem.Name);
                            objSourceItem.CopyTo(ItemURL);
                            objSourceItem.UnlinkFromCopySource();
                        }
                    }
                }
            }

Thanks


This is what worked for me. I had to move folders from spweb to another.

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 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];

                            //Recursively copy all the files and folders
                            RecursiveCopy(objSourceList, objSourceList.RootFolder, objDestinationList.RootFolder);



                            DestinationWeb.Update();
                            DestinationWeb.AllowUnsafeUpdates = false;
                        }
                    }
                }
            }
        }

this copies all the files and folders recursively.

Hope it helps someone.


If you are copying to a destination that is located within the same SPWeb, you can try the following.

using (SPSite site = new SPSite("http://urltosite"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                //get the folder from the source library
                SPFolder sourceFolder = web.GetFolder("Documents/Folder 1");

                //get the folder to the destination
                SPFolder destinationFolder = web.GetFolder("New Library");

                sourceFolder.CopyTo(destinationFolder.ServerRelativeUrl + "/" + sourceFolder.Name);

            }
        }

Sadly I don't think this works when copying a folder to a different SPWeb or SPSite.


SPList.Items only returns non-folder items. You can use SPList.Folders to iterate all of the folders in a list. So if you did the same foreach loop, only using:

foreach (SPListItem objSourceFolderItem in objSourceList.Folders)

You would then get all of the folders. To properly move the folder and all of its contents, you would use objSourceFolderItem.Folder.CopyTo(ItemUrl).

I've tried this using a list with only one level of folders (pair it with a foreach loop to get all of the items in the root folder), and it worked for me in SP2007. I believe SPList.Folders gets all of the folders in the entire list, not just the ones in the root folder, so if you end up breaking the list with a multi-level folder system, then an alternative to try might be:

foreach (SPFolder objSourceFolderItem in objSourceList.RootFolder.SubFolders)

Since those are already SPFolder objects, you can just use objSourceFolderItem.CopyTo(ItemUrl).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜