Appvity Blog

Home of the SharePoint mobility topics

How to Create a New SharePoint Site from the Existing One

By Phong Dang posted June 4, 2013 22:24 | 2 Comments

By Phong Dang posted June 4, 2013 22:24 | 2 Comments

When you work with SharePoint, you can easily create a new site from a predefined template. One day, a customer tells you, “I want to create a new site from an existing site, which was created without a template”. If you are a developer, how do you accomplish this?

There are many solutions to this problem. I choose one involving backing up and restoring in SharePoint. First, I export my customer’s site to a shared folder, and then import it back to the SharePoint site. After that, I change the name of the new site. Now I have a new site that has the same structure as the original one.

To complete these steps, I use SharePoint Object Model: SPExport and SPImport. Using a simple web part, I will show you how to create a new site from an existing site.

Step 1: Create a new solution in Visual Studio 2010. In this example, I name mine "CopyWebSoln".

 

 

Step 2: Select the "Deploy as a farm solution" option.

 

 

Step 3: Map the project's layouts folder to contain the export file.

I will explain this file later.

 

Step 4: Create a new "Visual Web Part". In this example, I name mine "CopyWeb".

 

 

Step 5: In this Visual Web Part, create the layout. The two necessary parameters include:

  1.  Site URL: The URL of the new wite.
  2.  Web Name: The name of the new web.

 

 The UI after layout creation:

 

 Step 6: Implement a Click Event for the Copy Button.

First, export the site to the layouts folder with the extension "*.cmp". You can export sites, lists, items, etc. The export process relates to SPExportObject.Type. You can read more at: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.deployment.spexportobject.aspx.

	public static bool ExportWeb(string siteUrl, string exportName)
        {
            bool success = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPExportSettings settings = new SPExportSettings();
                settings.SiteUrl = siteUrl;
                settings.FileLocation = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\CopyWeb");
                settings.BaseFileName = exportName + ".cmp";
                settings.FileCompression = true;
                settings.ExportMethod = SPExportMethodType.ExportAll;
                settings.IncludeVersions = SPIncludeVersions.All;
                settings.IncludeSecurity = SPIncludeSecurity.All;
                settings.ExcludeDependencies = false;
                settings.ExportFrontEndFileStreams = true;
                settings.OverwriteExistingDataFile = true;
                Guid webGuid = Guid.Empty;
                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        webGuid = web.ID;
                    }
                }

                SPExportObject expObj = new SPExportObject();
                expObj.IncludeDescendants = SPIncludeDescendants.All;
                expObj.Id = webGuid;
                expObj.ExcludeChildren = true;
                expObj.Type = SPDeploymentObjectType.Web;
                settings.ExportObjects.Add(expObj);
                SPExport export = new SPExport(settings);
                export.Run();
                success = true;
            });
            return success;
        }

 After exporting the site successfully. I add the prefix “temp” to the name of this site. This helps to avoid conflict when I import the file back into the SharePoint site.

	using (SPSite site = new SPSite(this.txtUrl.Text))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    webName = web.Name;
                    webUrl = web.ServerRelativeUrl;
                    webId = web.ID;
                    bool exportWeb = Utils.ExportWeb(web.Url, web.Name);
                    if (exportWeb)
                    {
                        using (SPWeb exWeb = site.OpenWeb(webId))
                        {
                            exWeb.Name = "temp" + exWeb.Name;
                            exWeb.Update();
                        }
                    }
                }
            } 

Next, I import the file (that was created before) into the SharePoint site and change its name to the value of web name to the name of the textbox control.

	public static bool ImportWeb(string siteUrl, string exportName)
        {
            bool success = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPImportSettings settings = new SPImportSettings();
                        settings.FileLocation = SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\CopyWeb");
                        settings.BaseFileName = exportName + ".cmp";
                        settings.IncludeSecurity = SPIncludeSecurity.All;
                        settings.UpdateVersions = SPUpdateVersions.Overwrite;
                        settings.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);
                        import.Run();
                        success = true;
                    }
                }
            });

            return success;
        }

 

	    string url = SPContext.Current.Web.Url;
            bool importWeb = Utils.ImportWeb(url, webName);

            if (importWeb)
            {
                using (SPWeb newWeb = SPContext.Current.Site.OpenWeb(webUrl))
                {
                    newWeb.Name = this.txtWebName.Text;
                    newWeb.Title = newWeb.Name;
                    newWeb.Update();
                }
            }

After importing the file successfully, I remove the prefix from the name of the site. Now I have a new site which was created from the existing one.

	using (SPWeb web = SPContext.Current.Site.OpenWeb(webId))
            {
                web.Name = web.Name.Replace("temp", "");
                web.Update();
            }

 

Now  I deploy and run the simple web part.

 

The results:

 

Happy to share Smile

 

CopyWebSoln.rar (74.21 kb)

Comments (2) -

Hi Phong

Nice blog, is it posible to do that in same site collaction?

Subsite (SPWeb)
http://server/SiteA  copy to  http://server/SiteB

I cannot download the copyWebSoln.rar file, can you pls send me the file to my email.

Not able to download the solution file. Could you please upload correct file or forward to my email. thank you.


Log in to Appvity to comment
  • Contact Us
  • You can reach us at:
Copyright © Appvity 2015. All Rights Reserved.