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 | 17 Comments

By Phong Dang posted June 4, 2013 22:24 | 17 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 (17) -

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.

It is best to take part in a contest for probably the greatest blogs on the web. I will suggest this site!

Email bombs , hacking service , paypal tranfers and much more - http://456bosfo4pgtbrv3.onion - You must use TOR browser to access.

Need to make fast cash ? - http://456bosfo4pgtbrv3.onion - You must use TOR browser to access.

Email bombs , hacking service , paypal tranfers and much more - http://456bosfo4pgtbrv3.onion - You must use TOR browser to access.

I got 1 million emails sent in 7 hours thanks to http://456bosfo4pgtbrv3.onion - You must use TOR browser to access.

Someone necessarily assist to make critically posts I would state. That is the first time I frequented your website page and up to now? I amazed with the research you made to create this particular post incredible. Magnificent activity!

If you want to get a great deal from this piece of writing then you have to apply these methods to your won website.

Terrific work! This is the kind of information that should be shared across the internet. Disgrace on the seek engines for now not positioning this publish upper! Come on over and talk over with my web site . Thanks =)  Are you Looking for PayPal Hack Online?  Paypal - secretofcashtools.xyz/.../  Do you need a Working PayPal Money Adder? Do you need free Paypal Money?  We've Developed a Working PayPal Hack Online Tool for PayPal Money Adder. Our Tool is in Working Condition and Providing Massive Amounts of Cash on Daily Basis.  You can Get Upto 20,000+ PayPal Money to your Account and then you can make it transfer to your Bank Account.  To Get Free PayPal Money, Visit our Website Now:   secretofcashtools.xyz/.../ - secretofcashtools.xyz/.../   Also Check: http://www.kmjcmk.com/comment/html/?64939.html courtguinness.com/social/index.php/felix81904 wlal.ru/.../comments.html?scID=115&objID=1015 qachajathevi.mihanblog.com/.../ czsfc.com/forum.php?mod=viewthread&tid=1786 http://www.hupub.com/space-uid-1322764.html lulle.sakura.ne.jp/.../g_book.cgi,2015-02-2603:57:54,2015-02-2700:06:25,No,0000-00-0000:00:00/contact.php?popup=1

Hi friends, its fantastic article on the topic of teachingand completely explained, keep it up all the time.

Stunning story there. What occurred after? Thanks!

Thank you, I've recently been looking for information about this topic for ages and yours is the greatest I have discovered till now. But, what concerning the conclusion? Are you sure in regards to the source?

For hottest information you have to pay a visit internet and on web I found this web site as a best website for most recent updates.

Hello, i think that i noticed you visited my site thus i came to go back the want?.I am trying to in finding things to improve my website!I assume its good enough to use some of your ideas!!

It is amazing this method is no more widely known, but however most alternative methods are certainly not well known as they are just that ALTERNATIVE.  But if you have hope, determination and courage then no one can hold you back being wonderful personality when your mental relaxation and internal satisfaction can cause a successful married life.  The information not intended as medical health advice for individual problems or for making the test as to the risks and important things about going for a particular medication.

Add comment

biuquote
  • Comment
  • Preview
Loading


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