How to import an SPWeb using c# to have the same behavior than PowerShell?

Viewed 920

I have a problem with comportment of sharepoint.deployment.spimport.

I want to copy a web within the same site collection like that :

myserver/mysitecoll/website1

myserver/mysitecoll/website2

When I use the PowerShell command to execute this, it does it perfectly, the website2 is the same as the website1

 Export-SPWeb -Identity http://myserver/mysitecoll/website1 -Path D:\mybackups\mytestsave\mybackup.bak

and

Import-SPWeb -Identity http://myserver/mysitecoll/website2 -Path D:\mybackups\mytestsave\mybackup.bak

But I need to do the same with c# I use

    private void ExportSpWeb()
    {

        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb = SPContext.Current.Web;  
        SPExportObject exportObject = new SPExportObject();
        exportObject.Id = myWeb.ID;
        exportObject.ParentId = mySite.ID;
        exportObject.Type = SPDeploymentObjectType.Web;
        SPExportSettings settings = new SPExportSettings();
        settings.SiteUrl = mySite.Url;
        settings.ExportMethod = SPExportMethodType.ExportAll;
        settings.FileLocation = "D:\\mybackups\\mytestsave";
        settings.BaseFileName = "test.cab";
        settings.FileCompression = true;
        settings.ExcludeDependencies = true;
        settings.CommandLineVerbose = true;
        settings.ExportObjects.Add(exportObject);
        SPExport export = new SPExport(settings);
        export.Run();
    }
    private void importSpWeb()
    {
        SPSite mySite = SPContext.Current.Site;
        SPWeb myDestWeb = mySite.AllWebs["website2"];
        SPImportSettings impsettings = new SPImportSettings();
        impsettings.SiteUrl = mySite.Url;
        impsettings.LogFilePath = "D:\\mybackups\\mytestsave\\test.log";
        impsettings.WebUrl = myDestWeb.ServerRelativeUrl;
        impsettings.FileLocation = "D:\\mybackups\\mytestsave";
        impsettings.FileCompression = true;
        impsettings.BaseFileName = "test.cab";
        impsettings.RetainObjectIdentity = false;
        SPImport import = new SPImport(impsettings);
        import.Run();
    }

But the comportment is not the same as PowerShell : Instead of being created using the specified WebUrl setting (http://myserver/mysitecoll/website2), the imported website is created as a new subsite with the path http://myserver/mysitecoll/website2/website1

How should I edit my code to obtain the same results as PowerShell?

1 Answers
Related