title: SharePoint 2007 Object Model: How can I make a new site collection, move the original main site to be a subsite of the new site collection?
body: <p>Here's my current setup:</p>
  
  <ul>
  <li>one site collection on a SharePoint 2007 (MOSS Enterprise) box (32 GB total in size)</li>
  <li>one main site with many subsites (mostly created from the team site template, if that matters) that is part of the one site collection on the box</li>
  </ul>
  
  <p>What I'm trying to do*:</p>
  
  <p>**If there is a better order, or method for the following, I'm open to changing it*</p>
  
  <ol>
  <li>Create a new site collection, with a main default site, on same SP instance (this is done, easy to do in SP Object Model)</li>
  <li>Move rootweb (a) to be a subsite in the new location, under the main site</li>
  </ol>
  
  <p>Current structure:</p>
  
  <pre><code>rootweb (a)
            \
            many sub sites (sub a)
  </code></pre>
  
  <p>What new structure should look like:</p>
  
  <pre><code>newrootweb(b)
         \
          oldrootweb (a)
              \
               old many sub sites (sub a)
  </code></pre>
  
  <p>Here's my code for step #2:</p>
  
  <p>Notes: 
      * SPImport in the object model under SharePoint.Administration, is what is being used here
      * This code currently errors out with "Object reference not an instance of an object", when it fires the error event handler</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Microsoft.SharePoint;
  using Microsoft.SharePoint.Deployment;
  
  public static bool FullImport(string baseFilename, bool CommandLineVerbose, bool bfileCompression, string fileLocation, bool HaltOnNonfatalError,
             bool HaltOnWarning, bool IgnoreWebParts, string LogFilePath, string destinationUrl)
          {
              #region my try at import
  
              string message = string.Empty;
              bool bSuccess = false;
              try
              {
                  SPImportSettings settings = new SPImportSettings();
                  settings.BaseFileName = baseFilename;
                  settings.CommandLineVerbose = CommandLineVerbose;
                  settings.FileCompression = bfileCompression;
                  settings.FileLocation = fileLocation;
                  settings.HaltOnNonfatalError = HaltOnNonfatalError;
                  settings.HaltOnWarning = HaltOnWarning;
                  settings.IgnoreWebParts = IgnoreWebParts;
                  settings.IncludeSecurity = SPIncludeSecurity.All;
                  settings.LogFilePath = fileLocation;
                  settings.WebUrl = destinationUrl;
                  settings.SuppressAfterEvents = true;
                  settings.UpdateVersions = SPUpdateVersions.Append;
                  settings.UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll;
  
                  SPImport import = new SPImport(settings);
  
                  import.Started += delegate(System.Object o, SPDeploymentEventArgs e)
                  {
                      //started
                      message = "Current Status: " + e.Status.ToString() + " " + e.ObjectsProcessed.ToString() + " of " + e.ObjectsTotal + " objects processed thus far.";
                      message = e.Status.ToString();
                  };
                  import.Completed += delegate(System.Object o, SPDeploymentEventArgs e)
                  {
                      //done
                      message = "Current Status: " + e.Status.ToString() + " " + e.ObjectsProcessed.ToString() + " of " + e.ObjectsTotal + " objects processed.";
                  };
                  import.Error += delegate(System.Object o, SPDeploymentErrorEventArgs e)
                  {
                      //broken
                      message = "Error Message: " + e.ErrorMessage.ToString() + " Error Type: " + e.ErrorType + " Error Recommendation: " + e.Recommendation
                          + " Deployment Object: " + e.DeploymentObject.ToString();
                      System.Console.WriteLine("Error");
                  };
                  import.ProgressUpdated += delegate(System.Object o, SPDeploymentEventArgs e)
                  {
                      //something happened
                      message = "Current Status: " + e.Status.ToString() + " " + e.ObjectsProcessed.ToString() + " of " + e.ObjectsTotal + " objects processed thus far.";
                  };
  
                  import.Run();
  
                  bSuccess = true;
              }
              catch (Exception ex)
              {
                  bSuccess = false;
                  message = string.Format("Error: The site collection '{0}' could not be imported. The message was '{1}'. And the stacktrace was '{2}'", destinationUrl, ex.Message, ex.StackTrace);
              }
  
              #endregion
  
              return bSuccess;
          }
  </code></pre>
  
  <p>Here is the code calling the above method:</p>
  
  <pre><code>[TestMethod]
  public void MOSS07_ObjectModel_ImportSiteCollection()
  {
      bool bSuccess = ObjectModelManager.MOSS07.Deployment.SiteCollection.FullImport("SiteCollBAckup.cmp", true, true, @"C:\SPBACKUP\SPExports", false, false, false, @"C:\SPBACKUP\SPExports", "http://spinstancename/TestImport");
      Assert.IsTrue(bSuccess);
  }
  </code></pre>