代码改变世界

sharepoint中的文档库之间的文件操作,列表库之间的列表项操作

2008-12-24 11:28  Virus-BeautyCode  阅读(2170)  评论(0编辑  收藏  举报

大家在操作文档库之间的文档的时候,需要用到打开文档库
 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();
                        }