将列表项从源复制到目标时复制文件夹
这是我将列表中的文件从源复制到目标的代码。使用下面的代码我只能复制文件,但不能复制文件夹。关于如何复制文件夹和这些文件夹中的文件有什么想法吗?
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();
}
}
}
}
谢谢
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 (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();
}
}
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用。我必须将文件夹从 spweb 移动到另一个。
这将递归地复制所有文件和文件夹。
希望它能帮助某人。
This is what worked for me. I had to move folders from spweb to another.
this copies all the files and folders recursively.
Hope it helps someone.
如果您要复制到位于同一 SPWeb 内的目标,则可以尝试以下操作。
遗憾的是,我认为将文件夹复制到不同的 SPWeb 或 SPSite 时这不起作用。
If you are copying to a destination that is located within the same SPWeb, you can try the following.
Sadly I don't think this works when copying a folder to a different SPWeb or SPSite.
SPList.Items 仅返回非文件夹项目。您可以使用 SPList.Folders 迭代列表中的所有文件夹。因此,如果您执行相同的 foreach 循环,仅使用:
foreach (SPListItem objSourceFolderItem in objSourceList.Folders)
您将获得所有文件夹。要正确移动文件夹及其所有内容,您可以使用 objSourceFolderItem.Folder.CopyTo(ItemUrl)。
我已经尝试使用仅包含一级文件夹的列表(将其与 foreach 循环配对以获取根文件夹中的所有项目),并且它在 SP2007 中对我有用。 的文件夹,因此,如果您最终使用多级文件夹系统打破列表,那么可以尝试的替代方法是:
我相信 SPList.Folders 获取整个列表中的所有文件夹,而不仅仅是根文件夹中 >foreach (SPFolder objSourceFolderItem in objSourceList.RootFolder.SubFolders)
由于这些已经是 SPFolder 对象,因此您可以只使用 objSourceFolderItem.CopyTo(ItemUrl) 。
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)
.