大家在操作文档库之间的文档的时候,需要用到打开文档库
SPFolder oFolder = web.GetFolder("Pages");
其实我们这个文档库本来是中文名字,叫做“页面”,可是在代码里面就要写成Pages,还有一个文档库叫做“过期文档”,在代码里面要写成DocLib,这两个名字可以从他们的链接里面看到,需要大家注意。
在下面代码中,我注释掉的就是不同列表库的列表项的拷贝,要注意的就是列表库的名字要用英文(红色部分对应的中文名字为过期新闻,是我们新建的时候写的,代码里面要用英文名字,可以从url中获取到) 和列表项的名字(绿色部分) 要加上
item.CopyTo(web.Url + "/DocLib /" + item.Name );
static void Main(string[] args)
{
SPSecurity.RunWithElevatedPrivileges(
delegate()
{
using (SPSite site = new SPSite(@"http://virus/sites/intranet"))
{
using (SPWeb web = site.AllWebs["team"])
{
web.AllowUnsafeUpdates = true;
//#region 从Pages(中文名字:页面)列表库拷贝/移动文件到DocLib(中文名字:过期新闻)列表库
//SPList list = web.Lists["页面"];
//foreach (SPListItem item in list.Items)
//{
// //列表项从一个列表库拷贝到另外一个列表库,列表项不支持移动(MoveTto)
// item.CopyTo(web.Url + "/DocLib/" + item.Name);
//}
//#endregion
//从Pages文档库拷贝/移动文件到DocLib文档库
SPFolder oFolder = web.GetFolder("Pages");
SPFileCollection collFiles = oFolder.Files;
for (int intIndex = collFiles.Count - 1; intIndex > -1; intIndex--)
{
//文档从一个文档库拷贝到另一个文档库
collFiles[intIndex].CopyTo(web.Url+"/DocLib/" + collFiles[intIndex].Name);
//文档从一个文档库移动到另一个文档库
collFiles[intIndex].MoveTo(web.Url+"/DocLib/" + collFiles[intIndex].Name);
}
web.AllowUnsafeUpdates = false;
}
}
});
Console.ReadLine();
}
文档库之间的拷贝是没有问题的,可是当使用自定义列表或者内容类型的时候,就会出现下列错误提示“"找不到源项目。请确认此项目存在,并且您具有读取此项目的权限。”
网上找了半天,原来是sharepoint本身的问题,有国外的家伙写了一个解决方案,链接如下
http://www.communardo.de/techblog/2008/01/08/sharepoint-listenelement-splistitem-in-eine-andere-liste-kopieren/
其实就是在目标列表中新建一个item,然后逐个栏的拷贝,附件单独拷贝,其他的直接赋值,但是如果两个列表的内容类型不相同的话,会出问题,所以我加上了try catch来处理一下,也就是遇到异常的时候什么都不做,继续下面的操作
添加如下方法
public static SPListItem CopyItem(SPListItem sourceItem, string destinationListName)
{
//copy sourceItem to destinationList
SPList destinationList = sourceItem.Web.Lists[destinationListName];
SPListItem targetItem = destinationList.Items.Add();
foreach (SPField f in sourceItem.Fields)
{
if (!f.ReadOnlyField && f.InternalName != "Attachments")
{
try{
targetItem[f.InternalName] = sourceItem[f.InternalName];
}catch{}
}
}
//copy attachments
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
targetItem.Attachments.Add(fileName, imageData);
}
targetItem.Update();
return targetItem;
}
使用方式如下
foreach (SPListItem item in list.Items)
{
CopyItem(item, "已发布公告");
//item.Delete();
}
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com