コンテンツの展開と移行APIがインポートされていないエラーがスローされない
-
09-12-2019 - |
質問
次の
.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Deployment;
namespace WssOMtest
{
class Program
{
static void Main(string[] args)
{
// export
SPSite site = new SPSite("http://localhost:4000");
SPWeb web = site.OpenWeb("/");
SPList list = web.Lists["MyList"];
SPListItem listItem = list.Items[0];
SPExportObject exportObject = new SPExportObject();
exportObject.Id = listItem.UniqueId;
exportObject.Type = SPDeploymentObjectType.ListItem;
SPExportSettings exportSettings = new SPExportSettings();
exportSettings.ExportObjects.Add(exportObject);
exportSettings.FileLocation = @"c:\export\CopyListItem";
exportSettings.FileCompression = false;
exportSettings.SiteUrl = "http://localhost:4000";
SPExport export = new SPExport(exportSettings);
export.Run();
web.Dispose();
// import
SPImportSettings importSettings = new SPImportSettings();
importSettings.SiteUrl = "http://localhost:4000";
importSettings.FileLocation = @"c:\export\CopyListItem";
importSettings.FileCompression = false;
importSettings.RetainObjectIdentity = false;
SPImport import = new SPImport(importSettings);
EventHandler<SPDeploymentEventArgs> startedEventHandler = new EventHandler<SPDeploymentEventArgs>(OnStarted);
import.Started += startedEventHandler;
import.Run();
// cleanup
site.Dispose();
}
// event handler to assign a new parent to the orphaned image in the package
static void OnStarted(object sender, SPDeploymentEventArgs args)
{
SPSite site = new SPSite("http://localhost:4000");
SPWeb web = site.OpenWeb("/PressReleases");
SPList list = web.Lists["TargetList"];
SPImportObjectCollection rootObjects = args.RootObjects;
foreach (SPImportObject io in rootObjects)
{
io.TargetParentUrl = list.RootFolder.ServerRelativeUrl;
}
web.Dispose();
site.Dispose();
}
}
}
大丈夫です。私のリスト名と私のサイトのURLを使用しているように、私はスーケティングを変更しました。輸出はうまく機能しています。インポートが機能していません。私はドキュメントlib上のコードを実行しています。エラーはスローされませんが、コンテンツアイテムはリストを変更していません。
何が間違っているのでしょうか。エクスポート後にOrginal項目を削除しても、親リストを変更せずにインポートを行っていないことはありません。
解決
I have tested your code and found the problem was with the files being created during exported were not cleaned up properly on the local disk. I implemented the files deletion logic to ensure Export and Import works fine. Note, I ran the Export and Import with and without FileCompression
and both are working fine. I ran my test on two Picture libraries.
Sorry, I didn't have much time to improve your code more for e.g. properly disposing the SPWeb and SPSite objects. Hope you will do it yourself.
The updated code is pasted below:
class Program
{
static void Main(string[] args)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// implementation details omitted
SPSite site = new SPSite("http://mylocaldev/");
SPWeb web = site.OpenWeb("/");
SPList list = web.Lists["ParentPic"];
SPListItem listItem = list.Items[0];
try
{
//delete the old files
DirectoryInfo dir = new DirectoryInfo(@"c:\export\CopyListItem");
foreach (System.IO.FileInfo file in dir.GetFiles()) file.Delete();
// export
SPExportObject exportObject = new SPExportObject();
exportObject.Id = listItem.UniqueId;
exportObject.Type = SPDeploymentObjectType.ListItem;
SPExportSettings exportSettings = new SPExportSettings();
exportSettings.ExportObjects.Add(exportObject);
exportSettings.FileLocation = @"c:\export\CopyListItem";
exportSettings.FileCompression = false;
//exportSettings.BaseFileName = "exportfile.cmp";
exportSettings.SiteUrl = "http://mylocaldev/";
SPExport export = new SPExport(exportSettings);
export.Run();
}
catch (Exception ex)
{
}
finally { web.Dispose();}
// import
SPImportSettings importSettings = new SPImportSettings();
importSettings.SiteUrl = "http://mylocaldev/";
importSettings.FileLocation = @"c:\export\CopyListItem";
importSettings.FileCompression = false;
//importSettings.BaseFileName = "exportfile.cmp";
importSettings.RetainObjectIdentity = false;
SPImport import = new SPImport(importSettings);
EventHandler<SPDeploymentEventArgs> startedEventHandler = new EventHandler<SPDeploymentEventArgs>(OnStarted);
import.Started += startedEventHandler;
import.Run();
// cleanup
site.Dispose();
});
}
// event handler to assign a new parent to the orphaned image in the package
static void OnStarted(object sender, SPDeploymentEventArgs args)
{
SPSite site = new SPSite("http://mylocaldev/");
SPWeb web = site.OpenWeb("/");
SPList list = web.Lists["ChildPic"];
SPImportObjectCollection rootObjects = args.RootObjects;
foreach (SPImportObject io in rootObjects)
{
io.TargetParentUrl = list.RootFolder.ServerRelativeUrl;
}
web.Dispose();
site.Dispose();
}
}
他のヒント
Fortunately, just before some days i was working on similar kind of tasks.
I have tried with your given code. and almost Its working.
But I am agree with @Falak that you should write code with proper try/catch and with SharePoint standard coding pattern.
Here, i found that once your code should run perfectly, second time you should get error, because you are not cleaning your File Location folder which is: "c:\export\CopyListItem".
This may be help to you.