none
Sharepoint 2013 SPExport.Run exception into ADFS RRS feed

  • Question

  • Hello, 

    I want to use SPExport and SPImport to copy one web to another location. I am using web part in Sharepoint  2013. This code is executed on a Button click event.

    protected void btnCopy_click(object sender, EventArgs e)
            {
                string webName;
                string webUrl;
                // ReSharper disable once RESP520265
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                 
                    Guid webId;
                    using (SPSite site = new SPSite(this.txtUrl.Text))
                    {
                        using (SPWeb web = site.OpenWeb())
                        { 
                           
                            webName = web.Name;
                            webUrl = web.ServerRelativeUrl;
                            webId = web.ID;
                            bool exportWeb = ExportWeb(web.Url, web.Name);
                            if (exportWeb)
                            {
                                using (SPWeb exWeb = site.OpenWeb(webId))
                                {
    
                                    exWeb.Name = "temp" + exWeb.Name;
                                    exWeb.AllowUnsafeUpdates = true;
                                    exWeb.Update();
                                    exWeb.AllowUnsafeUpdates = false;
                                }
                            }
                        }
                    }
    
                #region importWeb
               
                string url = SPContext.Current.Web.Url;
                bool importWeb = ImportWeb(url, webName);
    
                if (importWeb)
                {
                    using (SPWeb newWeb = SPContext.Current.Site.OpenWeb(webUrl))
                    {
                        
                        newWeb.Name = this.TextName.Text;
                        newWeb.Title = newWeb.Name;
                        newWeb.Update();
                    }
                }
    
    
                #endregion
    
    
                using (SPWeb web = SPContext.Current.Site.OpenWeb(webId))
                {
                    
                    web.Name = web.Name.Replace("temp", "");
                    web.Update();
                }
    
                });
    
            }
    
            #region ExportWeb
    
            public static bool ExportWeb(string siteUrl, string exportName)
            {
                bool success = false;
    
                // ReSharper disable once RESP520265
            
                    SPExportSettings settings = new SPExportSettings
                    {
    
                        SiteUrl = siteUrl,
    #pragma warning disable 618
                        FileLocation = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\CopyWeb"),
    #pragma warning restore 618
                        BaseFileName = exportName + ".cmp",
                        FileCompression = true,
                        ExportMethod = SPExportMethodType.ExportAll,
                        IncludeVersions = SPIncludeVersions.LastMajor,
                        IncludeSecurity = SPIncludeSecurity.None,
                        ExcludeDependencies = false,
                        ExportFrontEndFileStreams = true,
                        OverwriteExistingDataFile = true
                    };
                    Guid webGuid;
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        
                        using (SPWeb web = site.OpenWeb())
                        {
                            //web.AllowUnsafeUpdates = true;
    
                            webGuid = web.ID;
                        }
                    }
    
                    SPExportObject expObj = new SPExportObject
                    {
                        IncludeDescendants = SPIncludeDescendants.All,
                        Id = webGuid,
                        ExcludeChildren = true,
                        Type = SPDeploymentObjectType.Web
                    };
                    settings.ExportObjects.Add(expObj);
                    SPExport export = new SPExport(settings);
                    export.Run();
                    success = true;
              
                return success;
            }
            #endregion
    
    
            #region ImportWeb
            public static bool ImportWeb(string siteUrl, string exportName)
            {
                bool success = false;
    
                // ReSharper disable once RESP520265
                SPUtility.ValidateFormDigest();
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                     
                        using (SPWeb web = site.OpenWeb())
                        {
                        
    
                            SPImportSettings settings = new SPImportSettings
                            {
    #pragma warning disable 618
                                FileLocation = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\CopyWeb"),
    #pragma warning restore 618
                                BaseFileName = exportName + ".cmp",
                                IncludeSecurity = SPIncludeSecurity.All,
                                UpdateVersions = SPUpdateVersions.Overwrite,
                                IncludeUserCustomAction = SPIncludeUserCustomAction.All
                            };
                            settings.UpdateVersions = SPUpdateVersions.Append;
                            settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
                            settings.RetainObjectIdentity = false;
                            settings.SiteUrl = site.Url;
                            settings.WebUrl = web.Url;
                            SPImport import = new SPImport(settings);
                            web.AllowUnsafeUpdates = true;
                            import.Run();
                            web.AllowUnsafeUpdates = false;
                            success = true;
                        }
                    }
                });
    
                return success;
            }
    
            #endregion
    
        }
    }

    It work into one farm without ADFS but into another farm with ADFS i have an execption abord on export.run()  when i click on the button and the page display " Acces required this site is not share with us".I m site collection administration of this site collection.

    Do you have any idea why this code display "access required "whereas i m site collection administrator? 

    Best regards, 


    • Edited by SPCode Tuesday, April 11, 2017 4:19 PM
    Tuesday, April 11, 2017 4:14 PM

Answers

  • Do you have access to the location specified in your FileLocation variable?
    FileLocation = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\CopyWeb"),
    • Marked as answer by SPCode Thursday, April 13, 2017 3:59 PM
    Tuesday, April 11, 2017 5:14 PM

All replies

  • Do you have access to the location specified in your FileLocation variable?
    FileLocation = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\CopyWeb"),
    • Marked as answer by SPCode Thursday, April 13, 2017 3:59 PM
    Tuesday, April 11, 2017 5:14 PM
  • thanks, it was a missing permission
    Thursday, April 13, 2017 4:00 PM