2011-10-22 100 views
1

我想使用SPExport(它工作正常)和SPImport复制一个网站到另一个位置。我在Sharepoint Foundation 2010中使用应用程序页面。此代码在Button单击事件上执行。Sharepoint 2010 SPImport.Run安全异常

using (SPWeb web = site.OpenWeb(sourceWebUrl)) 
        { 
         SPExportSettings exportSettings = new SPExportSettings(); 
         exportSettings.FileLocation = exportPath; 
         exportSettings.BaseFileName = exportFileName; 
         exportSettings.SiteUrl = site.Url; 

         exportSettings.ExportMethod = SPExportMethodType.ExportAll; 
         exportSettings.FileCompression = true; 
         exportSettings.IncludeVersions = SPIncludeVersions.All; 
         exportSettings.IncludeSecurity = SPIncludeSecurity.All; 
         exportSettings.ExcludeDependencies = false; 
         exportSettings.ExportFrontEndFileStreams = true; 
         exportSettings.OverwriteExistingDataFile = true; 

         SPExportObject expObj = new SPExportObject(); 
         expObj.IncludeDescendants = SPIncludeDescendants.All; 
         expObj.Id = web.ID; 
         expObj.Type = SPDeploymentObjectType.Web; 
         exportSettings.ExportObjects.Add(expObj); 

         SPExport export = new SPExport(exportSettings); 
         export.Run(); 
        } 
using (SPWeb web = site.OpenWeb(destinationWebUrl)) 
        { 
         web.AllowUnsafeUpdates = true; 

         SPImportSettings importSettings = new SPImportSettings(); 

         web.FileLocation = exportPath; 
         web.BaseFileName = exportFileName; 
         web.IncludeSecurity = SPIncludeSecurity.All; 
         web.UpdateVersions = SPUpdateVersions.Overwrite; 
         web.RetainObjectIdentity = false; 
         web.SiteUrl = site.Url; 
         web.WebUrl = web.Url; 
         web.Validate(); 

         SPImport import = new SPImport(importSettings); 
         import.Run(); 
         web.AllowUnsafeUpdates = false; 
        } 

例外“此页面的安全验证无效点击返回在Web浏览器,刷新页面,并再次尝试您的操作。”当SPImport.Run()被调用时抛出。

我一直没有找到解决方案,这个问题既没有在应用程序页面上添加FormDigest控件,也没有在目标网页上允许不安全的更新。

此外,从控制台应用程序运行此代码工作正常,但如果代码从应用程序页面运行它不起作用(即使提高安全性)。

任何帮助,将不胜感激。谢谢。

回答

2

管理做到这一点通过在溶液中加入

SPUtility.ValidateFormDigest(); 

在第1行

+0

祝贺。如果可以,请确保将您的答案标记为“已接受”,以便其他人可以从您的成功中学习。干杯〜 –

+1

对于正在提升特权的人员,您需要先执行此操作。 http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.validateformdigest(v=office.14).aspx – trgraglia