Copy or move a Sharepoint website (continued)-calling in various ways

Source: Internet
Author: User
ArticleDirectory
    • Ispstsadmcommand
    • Powershell cmdlets
    • Implement a Sharepoint cmdlet
    • Spcmdlet
    • Implement this cmdlet
    • Application page
    • Interest

This article will go on to analyze the functions of copying and moving SharePoint websites. We will adopt different development methods provided by Sharepoint, includingProgramPage implementation in management tools such as stsadm or powershell. The main purpose of this operation is to familiarize everyone with different SharePoint development perspectives.

Note:: Except ispstsadmcommand, all examples are created on SharePoint 2010 using Visual Studio 2010.

Ispstsadmcommand

All users who have used pointpreviously have heard that stsadm.exe is a command line tool that allows administrators to perform certain operations on SharePoint servers. Just as sharepointis easy to configure and expand, stsadm.exe can also add custom commands as needed. The method is to implement the ispstsadmcommand interface, which is located in the Microsoft. Sharepoint. stsadmin namespace.

This interface is described in reference documents. Here, we only demonstrate the basic implementation of this interface.

Public class helloworld: ispstsadmcommand {# region ispstsadmcommand members Public String gethelpmessage (string command) {return "Help world";} public int run (string command, system. collections. specialized. stringdictionary keyvalues, out string output) {output = "Hello, world"; return 0 ;}# endregion}

After the compiled assembly is deployed in GAC, you also need to add an XML file under the .. \ 12 \ config folder.

  <?  XML version = "1.0" encoding = "UTF-8"  ?>  
< Commands >
< Command Name = "Hello"
Class = "Helloworldcommand. helloworld,
Helloworldcommand,
Version = 1.1.1.1,
Culture = neutral,
Publickeytoken = a9baec847ddb5ad1" />
</ Commands >

This article will not go into detail. For more information, see references. DetailedCodeAre all in the downloaded package.

Powershell cmdlets

Unfortunately, the support for extending stsadm.exe has been deprecated in SharePoint 2010. Therefore, the above Code cannot be run. Replace windows powershell and cmdlet in SharePoint 2010. Like other SharePoint items (such as feature and website definition), the new feature can be deployed using the SharePoint package WSP. It can also inherit other advantages of Windows powershell, which is beyond the scope of this article.

Implement a Sharepoint cmdlet

To create a Sharepoint cmdlet, you must first know which class to use. There are five controllable options for the base class: spnewcmdletbase, spsetcmdletbase, spgetcmdletbase, spremovecmdletbase, and spcmdlet. All these classes are located in the Microsoft. Sharepoint. powershell namespace. The differences are as follows:

    • Spnewcmdletbase <tcmdletobject>: an abstract base class used to create a new instance of an object and save it to data storage.
    • Spgetcmdletbase <tcmdletobject>: an abstract base class that allows inherited classes to find and return a series of tcmdletobject objects.
    • Spsetcmdletbase <tcmdletobject>: an abstract base class that allows inherited classes to update attributes of existing objects in data storage.
    • Spremovecmdletbase <tcmdletobjec>: an abstract base class that allows you to delete an existing data object of a specific type from data storage.
    • Spcmdlet: represents an abstract base class for all custom cmdlets written for a Sharepoint deployment, providing unified behavior across all SharePoint cmdlets.

As mentioned above, the first four base classes are basic addition, deletion, modification, and query operations. Create = spnewcmdletbase, READ = spgetcmdletbase, update = spsetcmdletbase, and delete = spremovecmdletbase. Finally, it is a base class used to customize the cmdlet. Because website copy does not include addition, deletion, modification, and query operations, we select the spcmdlet as our base class.

Spcmdlet

To create spcopycmd, start with a blank SharePoint project.

You need to add a referenceMicrosoft. Sharepoint. powershell. dll.Here is a tip. This dll is located in GAC and can be found under 14 \ ISPAI unlike other SharePoint-related DLL. You can use the following command to find the DLL:

  DirMicrosoft.SharePoint.Powershell.DLL/S

Copy it to another location so that we can reference it in the project. It is recommended to copy it to 14 \ ISAPI, so it will be very convenient to find it in the future. Another component to be referenced is system. Management. Automation. dll. Although the. NET label in the Add reference dialog box contains system. Management, it is not what we want. Must be viewed and locatedC: \ Program Files (x86) \ reference assemblies \ Microsoft \ windowspowershell \ V1.0Find it.

After all references are added, we can write the code:

[Spcmdlet (requirelocalfarmexist = true, requireuserfarmadmin = false)] public class spcopybase: spcmdlet {protected override void internalvalidate () {If (string. isnullorempty (source) throw new argumentexception ("The Source parameter must be specified"); If (string. isnullorempty (destination) throw new argumentexception ("the destination parameter destination must be specified");} protected override void internalprocessrecord () {// specific implementation} # region properties [parameter (position = 0, mandatory = true)] Public String source {Get; set;} [parameter (position = 1, mandatory = true)] Public String destination {Get; set;} protected bool iscopy {Get; Set ;}# endregion}

 

As shown above, this class inherits from the spcmdlet and implements two overwrites, internalvalidate and internalprocessrecord. If the cmdlet is intended to implement a series of read and write operations, we will overwrite retrievedataobject or updatedataobject accordingly. However, since our cmdlet does not add, delete, modify, and query classes, we only need to implement these two methods. At the same time, we can see that this class also uses spcmdletattribute. Three parameters can be used: requirelocalfarmexists, requireuserfarmadmin, and requireusermachineadmin. It is not difficult to guess their meanings by name.

To tell powershell that this cmdlet exists, you must create an XML file and put it in14 \ config \ powershell \ RegistrationDirectory. It is very easy to add to this folder in vs201. Add the-> SharePoint ing folder at the project point, navigate to the desired folder, and click OK.

After creating the folder, add an XML file. Although you can get a name at will, you 'd better start a name related to the project. This XML file is used when SharePoint powershell is started to load all available commands.

  <  PS: config  Xmlns: PS = "Urn: Microsoft. Sharepoint. powershell"  
Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "Urn: Microsoft. Sharepoint. powershell spcmdletschema. XSD" >
< PS: Assembly Name = "$ Sharepoint. Project. assemblyfullname $" >
< PS: cmdlet >
< PS: verbname > Copy-spweb </ PS: verbname >
< PS: classname > Mansoftdev. Sharepoint. powershell. spcopycmd </ PS: classname >
< PS: helpfile > Mansoftdev. Sharepoint. powershell. dll-help.xml </ PS: helpfile >
</ PS: cmdlet >
< PS: cmdlet >
< PS: verbname > Move-spweb </ PS: verbname >
< PS: classname > Mansoftdev. Sharepoint. powershell. spmovecmd </ PS: classname >
< PS: helpfile > Mansoftdev. Sharepoint. powershell. dll-help.xml </ PS: helpfile >
</ PS: cmdlet >
</ PS: Assembly >
</ PS: config >

First, you must set the name attribute of the Assembly element. The value is the complete name of the component compiled in our current project. $ Sharepoint. Project. assemblyfullname $ is automatically replaced with this name during compilation. Next, we need to add a cmdlet element for each cmdlet in the component. The verbname element tells powershell what verb-name is associated with the cmdlet. The classname element contains the fully qualified name of the class that implements this operation. The helpfile element is required, but the specified file does not need to exist. By convention, you can use the component name, which makes it easy to provide a Help file. This article does not involve the Help file.

Implement this cmdlet

Since the components we write contain both copying and moving, powershell needs to know when to use classes. This is exactly the role of cmdletattribute. Two parameters are required to distinguish between the verb and the noun. Verbscommon enumeration contains common verbs, such as set, get, and find. If you cannot find the one that suits you, you can also provide a string by yourself.

[Cmdlet (verbscommon. copy, "spweb")] public class spcopycmd: spcopybase {public spcopycmd () {base. iscopy = true;} [cmdlet (verbscommon. move, "spweb")] public class spmovecmd: spcopybase {public spmovecmd () {base. iscopy = false ;}}

All the replication or movement work is implemented in the spweb class in the previous article. Therefore, it is very easy to use the cmdlet. Direct call:

 
Protected override void internalprocessrecord () {mansoftdev. sharepoint. powershell. spweb copy = new mansoftdev. sharepoint. powershell. spweb (); copy. sourceurl = source; copy. destinationurl = destination; If (iscopy) Copy. copy (); else copy. move ();}

After compilation, right-click the project in Solution Explorer and click deploy. In this process, the XML is automatically copied to the registration directory, and the component is automatically placed in the GAC. Open the SharePoint 2010 console, and our cmdlet will be loaded through the XML file.

Of course, this article does not fully discuss the development of the cmdlet. The cmdlet also contains many highlights that can be used by us.

Application page

Although it is relatively simple to create an ispstsadmcommand or spcmdlet to replicate and move websites, It is not user-friendly. To use them, we must log on to the Sharepoint Server. To implement our functions in a more user-friendly manner, we can use custom website operations or website settings + application page design.

For developers who have used SharePoint 2007, using tools in Visual Studio 2010 to create and deploy a solution package is really gratifying. Here we will use part of the Visual Studio 2010 SharePoint tool. The detailed tool introduction is not within the scope of this article.

To create an application page and associate it with the corresponding menu, we should first start from creating a blank SharePoint project. Right-click the project in Solution Explorer and choose add-> SharePoint "layouts" mapped folder. Right-click the folder and choose add-> new item.

Select an appropriate name for the Application page. In this example, spcopy. aspx is selected and click Add. The current project structure is as follows:

The rest is to add appropriate controls and bind corresponding events on the ASPX page.

 

Protected void onmove (Object sender, eventargs e) {If (destination. Text. length! = 0) {web. sourceurl = spcontext. current. web. URL; web. destinationurl = destination. text; web. move (); response. redirect (destination. text) ;}} protected void oncopy (Object sender, eventargs e) {If (destination. text. length! = 0) {web. sourceurl = spcontext. current. web. URL; web. destinationurl = destination. text; web. move (); response. redirect (destination. text );}}

To access this page, I will add a link under the website management group on the website settings page. You can add an element. Visual Studio 2010 is also very simple. In Solution Explorer, right-click our project and choose add-> new item. In the pop-up dialog box, select empty element. The XML content is as follows. Although we should logically add it to the website operation group, we have added it to the website management group because the corresponding groupid is not found.

 

 

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  
< Elements Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Customaction ID = "Spcopy"
Requiresiteadministrator = "True"
Location = "Microsoft. Sharepoint. sitesettings"
Groupid = "Siteadministration"
Title = "Copy/mobile Website"
Description = "Copy or move this Website"
Sequence = "1001" >
< Urlaction URL = "_ Layouts/mansoftdev/spcopy. aspx" />
</ Customaction >
</ Elements >

This article will not detail SharePoint cumtomactions, elements, and features. For more information, see references.

 

Interest

Visual Studio 2010's improvements in SharePoint development are amazing. Development Based on this platform has become easier and more interesting.

 

Code download

Stsadm.zip

Cmdlet.zip

Applicationpage.zip

 

References

Deploy and use spcopy: Part2

Related to istsadmcommand: how to extend the stsadm Utility

Spcmdlet related: Microsoft. Sharepoint. powershell namespace

Default customaction location and ID

Customaction Element

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.