2

これは、リスト内のファイルをソースから宛先にコピーするための私のコードです。以下のコードを使用すると、ファイルはコピーできますが、フォルダーはコピーできません。フォルダーとそれらのフォルダー内のファイルをコピーする方法についてのアイデアはありますか?

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

ありがとう

4

3 に答える 3

5

これが私のために働いたものです。フォルダーを 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;
                        }
                    }
                }
            }
        }

これにより、すべてのファイルとフォルダーが再帰的にコピーされます。

それが誰かを助けることを願っています。

于 2010-04-19T19:56:07.093 に答える
1

同じ SPWeb 内にある宛先にコピーする場合は、次のことを試すことができます。

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

            }
        }

残念ながら、フォルダーを別の SPWeb または SPSite にコピーする場合、これは機能しないと思います。

于 2010-03-25T22:08:42.380 に答える
0

SPList.Itemsは、フォルダー以外のアイテムのみを返します。SPList.Foldersを使用して、リスト内のすべてのフォルダーを反復処理できます。したがって、同じforeachループを実行した場合は、以下を使用するだけです。

foreach (SPListItem objSourceFolderItem in objSourceList.Folders)

次に、すべてのフォルダーを取得します。フォルダとそのすべてのコンテンツを適切に移動するには、を使用しますobjSourceFolderItem.Folder.CopyTo(ItemUrl)

1つのレベルのフォルダーのみを含むリストを使用してこれを試しました(ルートフォルダー内のすべてのアイテムを取得するには、foreachループとペアリングします)。SP2007では機能しました。SPList.Foldersは、ルートフォルダー内のフォルダーだけでなく、リスト全体のすべてのフォルダーを取得すると思います。したがって、マルチレベルフォルダーシステムでリストを壊してしまう場合は、次の方法を試してみてください。

foreach (SPFolder objSourceFolderItem in objSourceList.RootFolder.SubFolders)

これらはすでにSPFolderオブジェクトであるため、を使用できますobjSourceFolderItem.CopyTo(ItemUrl)

于 2010-03-31T21:11:41.460 に答える