sharepoint - 将列表项从源复制到目标时复制文件夹

标签 sharepoint sharepoint-2007 sharepoint-api

这是我将列表中的文件从源复制到目标的代码。使用下面的代码我只能复制文件,但不能复制文件夹。关于如何复制文件夹和这些文件夹中的文件有什么想法吗?

using (SPSite objSite = new SPSite(URL))
            {
                using (SPWeb 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();
                        }
                    }
                }
            }

谢谢

最佳答案

这对我有用。我必须将文件夹从 spweb 移动到另一个。

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

这会递归地复制所有文件和文件夹。

希望它对某人有帮助。

关于sharepoint - 将列表项从源复制到目标时复制文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2511694/

相关文章:

python - 如何在 IronPython 中针对 SharePoint 2007 进行远程编程?

c# - 在 Sharepoint SiteData Web 服务中使用 GetChanges

sharepoint - 如何在我的 SharePoint 网站中获取多个同名列表?

SharePoint BCS 创建超链接列

SharePoint wsp 解决方案 : How to Deploy Globally

c# - 如何将 WebPart 添加到 SharePoint 网站中的所有页面?

c# - 来自 Sharepoint 2010 的用户个人资料图片 - C#

web-services - 验证 Office 365 SharePoint Online OOTB 服务

c# - 在列表之外保存 Sharepoint 数据

c# - 苔藓 : Running code when creating a site from a template?