SharePoint将图像从Web站点的文档库复制到另一个Web站点中的文档库

时间:2012-11-23 21:19:53

标签: sharepoint document

我将图像文件从网站的文档库移动到另一个网站的文档库。我得到以下错误@ line SPListItem oListItemDest = oFileDest.Item;

错误: [Microsoft.SharePoint.SPException] = {“指定的对象不属于列表。”}

代码:

   try
   {
      using (SPSite oSiteCollectionSrc = new SPSite("http://dev:32223/"))
      {
                SPWeb oWebsiteSrc = oSiteCollectionSrc.AllWebs["en/people"];
                SPList oListSrc = oWebsiteSrc.Lists["Images"];  

                SPListItemCollection collListItemsSrc = oListSrc.Items;

                foreach (SPListItem oListItemSrc in collListItemsSrc)
                {
                    SPFile oFileSrc = oListItemSrc.File;
                    Stream srcStream = oFileSrc.OpenBinaryStream();

                    using (SPSite oSiteCollectionDest = new SPSite("http://www.devmysites.com/"))
                    {
                        SPWeb oWebsiteDest = oSiteCollectionDest.OpenWeb("en/people");
                        SPList oListDest = oWebsiteDest.Lists["Images"];

                        SPFileCollection collFilesDest = oListDest.RootFolder.Files;

                        try
                        {

                            SPFile oFileDest = collFilesDest.Add(oListDest + @"/" + oFileSrc.Name, srcStream, true);
                            SPListItem oListItemDest = oFileDest.Item;
                            oListItemDest["Created"] = oFileDest.TimeCreated;
                            oListItemDest["Modified"] = oFileDest.TimeLastModified;
                            oListItemDest.Update();
                        }
                        catch(Exception es1)
                        {
                            Console.WriteLine("# Exception:#");
                            Console.WriteLine(es1.Message);
                        }

                        oWebsiteDest.Dispose();
                    }
                }
                oWebsiteSrc.Dispose();
            }
        }
        catch (Exception es)
        {
            Console.WriteLine("# Exception:#");
            Console.WriteLine(es.Message);
        }

1 个答案:

答案 0 :(得分:0)

我还必须在sharepoint列表之间复制文档,我以前使用的代码就是这个问题的答案:SharePoint 2010: Copy documents between lists