Use spexport and spimport to copy or move a Sharepoint website

Source: Internet
Author: User
Tags visual studio 2010
ArticleDirectory
    • Introduction
    • Background
    • Tool Selection for copying or moving a site
    • Spexport/spimport
    • Copy/mobile website
    • Verify source website
    • Verify target website
    • View spwebtemplate
    • Export website
    • Import website
    • Subsequent enhancements
Introduction

Sharepoint is an excellent tool that helps you provide information within your organization to the people you need. This information must also be adjusted to adapt to the new organizational structure when the company's internal organization changes or restructured. For example, a branch of a company has used a conference site to organize the content of a project and facilitates the collaboration of stakeholders. However, the project may be taken over by another branch and need to be transferred to its own site. How can we save and move existing information? Although Sharepoint is a great platform, there is a shortage of tools in this area. Here, we will provide an option or a possible solution to allow websites to copy or move from one location to another.

Background

Let's first define a scenario for the solution in this article. Assume that your company has created a Sharepoint website. Its structure is similar to the following table:

Root site
Beijing Tianjin Shanghai Shenzhen

Branch 1

Branch 3

Branch 5

Branch 7

Branch 2

Branch 4

Branch 6

Branch 8

This is of course a very simple real-world application, because each site may have many lists and sub-sites. However, this will give us a starting point for discussion. Now, assume that branch 8 has moved from Shenzhen to Beijing. Of course, to maintain the structure of the entire website, the website can still be stored in the current location, but logically, people will search for the website of this branch from the Beijing website, this is because of the current situation. Therefore, we may create a new sub-website under Beijing and then manually move all the content. However, this is a tedious and labor-intensive process and does not retain audit information or version history.

Note:This project uses SharePoint 2010 and Visual Studio 2010. Although it should be available in earlier versions, it has not been tested.

Tool Selection for copying or moving a site

When you need to copy or move a Sharepoint site set or website, you have limited options.

Backup and restoration in the Management Center

The management center provides backup and recovery functions for Sharepoint websites. In SharePoint 2007, the backup is not very detailed. You can runProgram.

For a separate website set or website or list, Sharepoint 2010 provides granular backup.

Page for exporting websites and lists:

There is also a very useful function to extract information from the unconnected content database:

Stsadm

The old command line tool stsadm.exe is also the most commonly used tool for backup and restoration. The backup feature is still available in SharePoint 2010. The parameters are very detailed.

  Stsadm-O backup-URL http://Myserver/Shenzhen/Division8
-Directory c:\Spbackup-filename division8.Bak-backupmethod full

For detailed settings of stsadm parameters, refer to this article on technet.

Stsadm has a size limit: The content cannot exceed 15 GB. For versions earlier than SharePoint 2007 SP2, you must also use the setsitelock item to prevent adding or modifying website content during the backup process. In addition, the subscription and workflow will not be retained. To restore a website, run the restore command. These two steps are time-consuming and can only be performed by persons with administrator permissions. It is not prepared for common users.

SharePoint designer

When you use SharePoint designer 2007 to open a Sharepoint 2007 site, you can use the "website"> "management"> "backup Website" menu to back up the website. This item cannot be found in SharePoint designer 2010.
To restore the backup to another location, you must first create the website and then restore it. Three cumbersome steps. At the same time, the restriction is that you must be able to access SharePoint designer, and not everyone installs SharePoint designer.

Spexport/spimport

In Microsoft. sharepoint. in the deployment namespace, we find the spexport and spimport classes, which include the most basic methods to back up and restore other objects in a website set, website, list, or Sharepoint, these methods are also used by Sharepoint designer. To be able to use spexport and spimport, we first need to configure the detailed information involved in the entire process through the corresponding settings class.

 
Spexportsettings settings = new spexportsettings (); settings. filelocation = "C: \ spbackup"; settings. siteurl = http: // server/mysitesettings. filecompression = true; settings. overwriteexistingdatafile = true; settings. basefilename = "Export"; spexport export = new spexport (settings); export. run ();

ThisCodeAn archive file named export. CMP will be created, which contains all the information in the website set http: // server/mysite and stored in the C: \ spbackup folder. When filecompression is set to true, a cab compressed package is generated. Overwriteexistingdatafile = true tells spexport to overwrite any existing file. If it is set to false and the export. CMP file already exists, an exception is thrown. The operations in the opposite direction are as follows:

 
Spimportsettings settings = new spimportsettings (); settings. filelocation = "C: \ spbackup"; settings. basefilename = "Export"; spimport Import = new spimport (settings); import. run ();

It is interesting to note that when commandlineverbose = true is set in the settings object, the output information is written to a console window, we can easily see the detailed process during the import and export process. Although the spexport and spimport classes both have a progressupdated event, it does not send such information. It only sends the total number of related objects and the number of processed objects.

This is just some basic settings. Now let's take a closer look at this process and see how to use it to copy or move a website.

Copy/mobile website

We can create a class to provide a simple interface for copying or mobile websites.

 
Spweb web = new spweb ("http: // server/Shenzhen/division8", "http: // server/Beijing"); web. Copy ();

The first parameter is the URL of the copied website. The second parameter is the target URL. In this example, copy http: // server/Shenzhen/division8 to http: // server/Beijin/division8.

Verify source website

The first thing that must be done is to verify the existence of the source website.

 
Private void validatesource () {try {URI uri = new uri (sourceurl); sourcesite = new Microsoft. Sharepoint. spsite (sourceurl); If (sourcesite! = NULL) {sourceweb = sourcesite. openweb (URI. localpath); If (! Sourceweb. exists) {sourceweb. dispose (); sourceweb = NULL; throw new argumentexception ("the source website is invalid") ;}} catch (system. io. filenotfoundexception) {// The specified website throw new argumentexception ("sourceurl is invalid") cannot be found ");}}

The interesting thing here is that if we do not pass the relative path of the website we want to open when calling openweb, as shown below, the root website will be returned, of course, spweb. exists returns true.

 
Sourceweb = sourcesite. openweb ();

A tedious but feasible method is to search for spwebcollection to match the name. It is necessary to use stringcomparer. currentcultureignorecase because the name and URL are not necessarily case-insensitive.Note: This method is invalid for websites with Chinese titles, because the title and URL names in Chinese are usually different, and the same content must be decode before comparison. This is not a simple case.

 
String web = sourceurl. replace (sourcesite. URL ,""). remove (0, 1); If (sourcesite. allwebs. names. contains (URI. localpath, stringcomparer. currentcultureignorecase) {sourceweb = sourcesite. openweb ();}
Verify target website

Verification is also required for the target website. If a website with the same name already exists, we must check the server type. If it is different from the source website, an exception is thrown during the import process. Therefore, you must first delete it. You do not have to create a new website in advance because it is automatically created during the import process. If the website type is the same, the target website will be overwritten during the import process.

 

Private void validatedestination () {try {URI uri = new uri (destinationurl); destinationsite = new Microsoft. Sharepoint. spsite (destinationurl); If (destinationsite! = NULL) {destinationweb = destinationsite. openweb (URI. localpath); If (destinationweb. exists) {comparetemplates ();} else {Throw new argumentexception ("the target website is invalid") ;}} catch (system. io. filenotfoundexception) {// The specified website throw new argumentexception ("the target website is invalid");} private void comparetemplates () {uint localid = convert. touint16 (sourceweb. locale. lcid); string templatename = gettemplatename (sourcesite. c Ontentdatabase. databaseconnectionstring, sourceweb. ID, sourceweb. webtemplate); spwebtemplate sourcetemplate = sourcesite. getwebtemplates (localid) [templatename]; templatename = gettemplatename (destinationsite. contentdatabase. databaseconnectionstring, destinationweb. ID, destinationweb. webtemplate); spwebtemplate desttemplate = destinationsite. getwebtemplates (localid) [templatename]; // If the template is different, delete the target Website if (sourcetemplate. Name! = Desttemplate. Name) {recursivelydeleteweb (destinationweb); destinationweb. Dispose (); destinationweb = NULL ;}}
View spwebtemplate

When comparing the spwebtemplate of a website, the focus is first placed on the webtemplate attribute. However, this attribute does not provide sufficient information for accurate comparison. For example, for a website created using the basic Meeting Workspace template, spweb. webtemplate returns MPs. For a website created using a blank website template, STS is returned. The correct template names should be MPs #1 and STS #1. Where are these specific configuration information? Unfortunately, it seems that the spweb object cannot be found. The only thing that can be found is the wss_content content database.

 
Private string gettemplatename (string connstring, guid ID, string webtemplate) {string plain text = string. format ("select provisionconfig from" + "DBO. webs where id = '{0}' ", id. tostring (); int provisionconfig = 0; using (sqlconnection conn = new sqlconnection (connstring) {using (sqlcommand cmd = new sqlcommand (plain text, Conn) {Conn. open (); provisionconfig = convert. toint32 (CMD. executescalar ();} return string. format ("{0 }#{ 1}", webtemplate, provisionconfig );}

Here, we get the connection string through the spsite object, and then find the corresponding value in the webs table. Of course, we still need to remind you that direct access to the database is not recommended. However, in our case, the API cannot be found, so we have no choice.

Export website

As mentioned above, the configuration of the export process requires a settings class, which is spexportsettings.

 
Spexportsettings settings = new spexportsettings (); settings. filelocation = exportpath; settings. basefilename = export_filename; settings. siteurl = sourcesite. URL; settings. exportmethod = spexportmethodtype. exportall; settings. filecompression = true; settings. includeversions = spincludeversions. all; settings. includesecurity = spincludesecurity. all; settings. excludedependencies = false; settings. exportfrontendfilestreams = true; settings. overwriteexistingdatafile = true; spexport export = new spexport (settings); export. run ();

What will happen when running the above Code? Is the entire website set exported? The whole SharePoint website set refers to the root website of the site specified by siteurl and all its subsites. However, we only need one of them. To export a website, add it to the exportedobjects collection. Spexport uses this set to identify which content needs to be exported. If this set is empty, all content will be exported.

Spexportobject expobj = new spexportobject (); expobj. includescendants = spincludescendants. all; expobj. id = sourceweb. ID; expobj. type = spdeploymentobjecttype. web; settings. exportobjects. add (expobj );

We specify the sourceweb ID and set type to web. The spdeploymentobjecttype enumeration contains a series of members for identification: site, web, list, listitem, file, and folder.

Import website
 
Private void import () {If (destinationsite = NULL) throw new applicationexception ("the target website is empty"); spimportsettings settings = new spimportsettings (); settings. filelocation = exportpath; settings. basefilename = export_filename; settings. includesecurity = spincludesecurity. all; settings. updateversions = spupdateversions. overwrite; settings. retainobjectidentity = false; settings. siteurl = destinationsite. URL; settings. weburl = destinationurl; spimport Import = new spimport (settings); import. run ();}
Subsequent enhancements

Our code is of no practical use now. Unless it is called in other code. This is just a background for the subsequent work. In the following blog, you will see how to call this type of method in different SharePoint development methods to implement the copy or mobile website functions.

 

References

Using spexport and spimport: Part1

Code download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.