SharePOint 2010 编程导入导出站点

/// <summary>
        /// Exports plant site
        /// </summary>
        /// <param name="sourceSiteURL"></param>
        protected void ExportSite(string sourceSiteURL, ArrayList arrMoveSite)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(sourceSiteURL))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPExportObject exportObject = new SPExportObject();
                            exportObject.Type = SPDeploymentObjectType.Web;
                            exportObject.Id = web.ID;
                            exportObject.IncludeDescendants = SPIncludeDescendants.All;


                            SPExportSettings settings = new SPExportSettings();
                            settings.OverwriteExistingDataFile = true;
                            settings.SiteUrl = sourceSiteURL;
                            settings.BaseFileName = EXPORT_FILENAME;
                            settings.ExportMethod = SPExportMethodType.ExportAll;
                            settings.FileLocation = exportFolderName;
                            settings.FileCompression = true;
                            settings.IncludeSecurity = SPIncludeSecurity.All;
                            settings.IncludeVersions = SPIncludeVersions.All;
                            settings.LogFilePath = exportFolderName + web.ID + ".log";
                            settings.ExcludeDependencies = true;
                            settings.ExportObjects.Add(exportObject);

                            SPExport export = new SPExport(settings);
                            export.Run();
                            //Display Result
                            arrMoveSite.Add("Site "+web.Url.Substring(web.Url.LastIndexOf("/") + 1) + " has been exported successfully!");
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Log(ex, ex.Message);
            }
        }

        /// <summary>
        /// Imports plant site.
        /// </summary>
        /// <param name="destSiteURL"></param>
        protected void ImportSite(string destSiteURL)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite rootSiteColl = new SPSite(destSiteURL))
                    {
                        rootSiteColl.AllowUnsafeUpdates = true;
                        using (SPWeb rootWeb = rootSiteColl.OpenWeb())
                        {
                            rootWeb.AllowUnsafeUpdates = true;
                            SPUtility.ValidateFormDigest();
                            SPImportSettings importSettings = new SPImportSettings();
                            importSettings.SiteUrl = rootWeb.Url;
                            importSettings.WebUrl = rootWeb.Url;
                            importSettings.FileLocation = exportFolderName;
                            importSettings.FileCompression = true;
                            importSettings.BaseFileName = EXPORT_FILENAME;
                            importSettings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
                            importSettings.IncludeSecurity = SPIncludeSecurity.All;
                            importSettings.UpdateVersions = SPUpdateVersions.Append;
                            importSettings.LogFilePath = exportFolderName + rootWeb.ID + ".log";
                            //importSettings.RetainObjectIdentity = false;
                            importSettings.Validate();
                            SPImport import = new SPImport(importSettings);
                            import.Run();

                            rootWeb.AllowUnsafeUpdates = false;
                            rootWeb.Close();
                        }
                        rootSiteColl.AllowUnsafeUpdates = false;

                    }
                });
            }
            catch (Exception ex)
            {
                Log(ex, ex.Message);
            }

 

posted @ 2013-01-28 11:32  山之子  阅读(268)  评论(0编辑  收藏  举报