Using the ecmascript object model, we can copy or move files in the documentlist. Here, we need to achieve the effect that when a document in the document list is selected, a button appears in its associated ribbon area. Click this button, you can copy the selected document or move it to another document list. As follows:
Here we areCodeSpecifies the name of the target document list. In actual operations, you can click the button to bring up the modal window, allowing users to select or enter the name of the target document list, to enhance flexibility.
I. Implementation of the replication function
Go directly to the step
1. Create a Sharepoint empty project named ecmascriptcopyfile
2. Add a new feature in this project and name it ecmascriptcopyfilefeature.
Name this feature:
3. Add two SharePoint directories: Images and layouts.
Copy an image to the images \ ecmascriptcopyfile \ directory, and name it copyfile.png for backup.
4. Add a new empty element named ecmascriptcopyfileelement and define the code for this element.
<? XML version = "1.0" encoding = "UTF-8"?>
<Elements xmlns = "http://schemas.microsoft.com/sharepoint/">
<Customaction
Id = "copyingfilesbutton"
Location = "commandui. Ribbon"
Registrationid = "101" registrationtype = "list"
Title = "copy documents">
<Commanduiextension>
<Commanduidefinitions>
<Commanduidefinition
Location = "ribbon. Documents. Manage. Controls. _ Children">
<Button id = "ribbon. Documents. Manage. copydocuments"
Command = "ribbon. Documents. Manage. copydocuments. copyfilecommand"
Labeltext = "copy documents"
Image32by32 = "/_ layouts/images/ecmascriptcopyfile/copyfile.png"
Templatealias = "O1"/>
</Commanduidefinition>
</Commanduidefinitions>
<Commanduihandlers>
<Commanduihandler
Command = "ribbon. Documents. Manage. copydocuments. copyfilecommand"
Commandaction = "javascript: copydocumentlistfile ()"/>
</Commanduihandlers>
</Commanduiextension>
</Customaction>
<Customaction
Id = "ribbon. Documents. Manage. copydocuments. script"
Location = "scriptlink"
Scriptsrc = "/_ layouts/ecmascriptcopyfile/ecmacopyfile. js"/>
</Elements>
In this element:
- We have set the button to be placed in ribbon. Documents. Manage. controls.
- Ecmacopyfile. js
- The action with the button set is to call the copydocumentlistfile () function in the Javascript file.
- Note that the command attribute of the button in <commanduidefinition> must be the same as that in <commanduihandlers>, indicating the corresponding command settings and responding to the corresponding button event.
5. Create a Javascript file named ecmacopyfile. js in the ecmascriptcopyfile directory of layouts. The file content is as follows:
VaR Ecmacopyfile;
VaR Ecmacopycontext
VaR Ecmacopyweb;
VaR Ecmacopy_destinationlib;
VaR Policyid;
Function Copydocumentlistfile (){
Ecmacopycontext = sp. clientcontext. get_current ();
Ecmacopyweb = ecmacopycontext. get_web ();
Ecmacopycontext. Load (ecmacopyweb );
Ecmacopy_destinationlib = ecmacopyweb. get_lists (). getbytitle ('My shared documents '); // Get destination document list
// Ecmacopy_destinationlib = ecmacopyweb. get_lists (). getbytitle ('mybackupdocuments'); // get destination document list
Ecmacopycontext. Load (ecmacopy_destinationlib );
VaR Currentlibid = sp. listoperation. selection. getselectedlist ();
VaR Currentlib = ecmacopyweb. get_lists (). getbyid (currentlibid );
VaR Selecteditems = sp. listoperation. selection. getselecteditems (ecmacopycontext );
VaR Count = countdictionary (selecteditems );
For ( VaR I In Selecteditems) // Loop the source document files
{
Alert ('now copying document: '+ I );
VaR Currentitem = currentlib. getitembyid (selecteditems [I]. ID );
Ecmacopycontext. Load (currentitem );
// Debugger;
Ecmacopyfile = currentitem. get_file (); // Get the source file
Ecmacopycontext. Load (ecmacopyfile );
Ecmacopycontext.exe cutequeryasync (function. createdelegate ( This , This . Onquerysucceededcopydocumentlistfile ),
Function. createdelegate ( This , This . Onqueryfailedcopydocumentlistfile ));
}
}
Function Onquerysucceededcopydocumentlistfile (sender, argS ){
If (Ecmacopyfile! = Null ){
Debugger ;
VaR Ecmacopy_destinationliburl = ecmacopyweb. get_serverrelativeurl ()
+ Ecmacopy_destinationlib.get_title () + '/'
+ Ecmacopyfile. get_name ();
Policyid = sp. UI. Policy. addnotification ('copying file... '+ Ecmacopyfile. get_serverrelativeurl ()
+ 'To' + ecmacopy_destinationliburl,True);
Ecmacopyfile. copyto (ecmacopy_destinationliburl, True ); // Copy the selected file to destination document list
// Ecmacopyfile. moveTo (ecmacopy_destinationliburl, 1); // move the selected file to destination document list (overwrite the file with the same name if it exists)
// Ecmacopyfile. moveTo (ecmacopy_destinationliburl, sp. moveoperations. allowbrokenthickets );
// Ecmacopyfile. moveTo (ecmacopy_destinationliburl, sp. moveoperations. Overwrite );
Ecmacopycontext.exe cutequeryasync (function. createdelegate ( This , This . Onquerysucceededcopyone ),
Function. createdelegate ( This , This . Onqueryfailedcopyone ));
}
}
FunctionOnqueryfailedcopydocumentlistfile (sender, argS ){
Alert ('error occured' + args. get_message ());
}
function onquerysucceededcopyone (sender, argS) {
/// debugger;
sp. UI. policy. removenotification (policyid);
sp. UI. policy. addnotification ('file copied successfully ', false );
}
FunctionOnqueryfailedcopyone (sender, argS ){
//Debugger;
Sp. UI. Y. addnotification ('error copying file ',False);
Sp. UI. Policy. removenotification (policyid );
Showerror (ARGs. get_message ());
}
After completion, the entire project is as follows:
6. build and deploy the project, and then go to the test website to view the effect as follows:
Ii. Move Selected Documents
Most of the steps are the same as those above, but they must be described in the document mobile code. We need to use file. the moveTo () code replaces the previous file. copyto () code. Some people use the moveTo function to encounter a failed result because they set the wrong parameter.
In the parameters of the file. moveTo () function, the first parameter is the same as copyto, and is the address of the target document. However, the second option is not 'boolean' type, but the sp. moveoperations Enumeration type. The possible options for this type are as follows:
None:No specific instructions
Overwrite:If a document with the same name already exists in the target list, overwrite it.
Allowbrokenthickets:This option is mainly used to support thicket files. It is generally used to save complicated HTML documents. When saving such documents, you must not only save the document itself, you must also save several other documents related to this document, this document group is usually composed of a main file and a hidden thicked folder (this folder contains the thicked description list and other supported documents listed in the List ). If this option is used, you can copy one or more documents in this document group.
Bypassapprovepermission:It is generally applicable to documents with published version.
Here, the overwrite type is set as follows:
Function Onquerysucceededcopydocumentlistfile (sender, argS ){
If (Ecmacopyfile! = Null ){
Debugger ;
VaR Ecmacopy_destinationliburl = ecmacopyweb. get_serverrelativeurl ()
+ Ecmacopy_destinationlib.get_title () + '/'
+ Ecmacopyfile. get_name ();
Policyid = sp. UI. Policy. addnotification ('copying file... '+ Ecmacopyfile. get_serverrelativeurl ()
+ 'To' + ecmacopy_destinationliburl,True);
Ecmacopyfile. moveTo (ecmacopy_destinationliburl, sp. moveoperations. Overwrite );
Ecmacopycontext.exe cutequeryasync (function. createdelegate (This,This. Onquerysucceededcopyone ),
Function. createdelegate (This,This. Onqueryfailedcopyone ));
}
}