现在的位置: 首页 > 综合 > 正文

Sharepoint学习笔记—ECMAScript对象模型系列– 10、 复制/移动Document List中的文档

2012年08月07日 ⁄ 综合 ⁄ 共 6652字 ⁄ 字号 评论关闭

    使用ECMAScript对象模型,我们可以实现对DocumentList中Files的拷贝或移动。在这里,我们需要实现的的效果是当选中某个Document List中的文档时,在其关联Ribbon区出现一个按钮,点击此按钮,可以实现把这个选中的文档复制或移动到另一个Document List中。  效果图如下:

   

   这里我们在代码中指定了目标Document List的名字,而在实际操作中,你可以在点击按钮后弹出模态窗口,让用户自行选择或输入目标Document List的名字,以增强灵活性。

 一、复制功能的实现
 直接进入步骤
 1、新建一个Sharepoint空项目,命名为ECMAscriptCopyFile

 

 2、在此项目中添加新的Feature,并命名为ECMAscriptCopyFileFeature

 

命名此Feature:

 

3、在此项目中添加Images与Layouts两个Sharepoint目录。

 拷贝一个文件拷贝的图片放到Images\ECMAscriptCopyFile\目录下,并命名为copyFile.png以备用。

 

 4、添加一个新的空Element,命名为ECMAscriptCopyFileElement.并定义此元素的代码

 

<?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>

  在此元素中:

  •    我们设置了按钮的放置位置在Ribbon.Documents.Manage.Controls
  •    设置了引用外部Javascript文件ECMAcopyFile.js
  •    设置了按钮的Action是调用Javascript文件中的CopyDocumentListFile()函数
  •    要注意<CommandUIDefinition>中的Button的Command属性必须与<CommandUIHandlers>中的Command属性命名一致,表示对应的命令设置,响应对应的Button事件。

 

5、在Layouts的ECMAscriptCopyFile目录下新建一个名为ECMAcopyFile.js的Javascript文件。文件内容如下

var ECMACopyFile;
var ECMACopycontext
var ECMACopyweb;
var ECMACopy_destinationlib;
var notifyId;

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.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceededCopyDocumentListFile),
                             Function.createDelegate(thisthis.onQueryFailedCopyDocumentListFile));
    }
}

function onQuerySucceededCopyDocumentListFile(sender, args) {
    if (ECMACopyFile != null) {
         debugger;
        var ECMACopy_destinationlibUrl = ECMACopyweb.get_serverRelativeUrl()
                               + ECMACopy_destinationlib.get_title() + '/'
                               + ECMACopyFile.get_name();

        
         notifyId = SP.UI.Notify.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.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceededCopyOne),
                             Function.createDelegate(thisthis.onQueryFailedCopyOne));
    }
}

function onQueryFailedCopyDocumentListFile(sender, args) {
    alert('Error occured' + args.get_message());
}

function onQuerySucceededCopyOne(sender, args) {
   // debugger;
    SP.UI.Notify.removeNotification(notifyId);
    SP.UI.Notify.addNotification('File copied successfully', false);
}

function onQueryFailedCopyOne(sender, args) {
   // debugger;
    SP.UI.Notify.addNotification('Error copying file', false);
    SP.UI.Notify.removeNotification(notifyId);
    showError(args.get_message());
}

 

完成后,整个项目如下图:

6、Build并部署这个项目,然后到测试网站上查看效果如下:

 

 

 

 

二、移动选中文档的功能实现

   绝大部分和上述步骤相同,只是在文档移动代码上需要说明,我们需要用File.moveTo()代码来代替前面的File.copyTo()代码,有些人用moveTo函数会遇到失败的结果,那是因为他们设置了错误的参数。
File.moveTo()函数的参数中,第一项参数与copyTo相同,都是目标文档的地址。但第二项不再是Boolean类型,而是SP.MoveOperations Enumeration类型,此类型可能的选项如下:

None : 没有任何特定说明
Overwrite:如果目标List中已经存在同名的文档 ,则覆盖它。
AllowBrokenThickets :此选项主要用于支持文档群类型(thicket files)的文档,一般是用于保存比较复杂的HTML文档,在保存此类文档时,不但要保存文档本身,还要保存若干与此文档相关的其它文档,此文档群通常由一个main文件和一个隐藏的thicked folder(此folder下存放有thicked描述清单以及清单中列举的其它支持文档)。如果使用此项,则表明可以把属于这个文档群中的某个或某几个文档复制出去。
BypassApprovePermission :一般针对有published version的文档。
此处,我们设置了Overwrite类型,如下

function onQuerySucceededCopyDocumentListFile(sender, args) {
    if (ECMACopyFile != null) {
         debugger;
        var ECMACopy_destinationlibUrl = ECMACopyweb.get_serverRelativeUrl()
                               + ECMACopy_destinationlib.get_title() + '/'
                               + ECMACopyFile.get_name();

        
         notifyId = SP.UI.Notify.addNotification('Copying file…' + ECMACopyFile.get_serverRelativeUrl()
                                               + 'to' + ECMACopy_destinationlibUrl, true);

         ECMACopyFile.moveTo(ECMACopy_destinationlibUrl, SP.MoveOperations.Overwrite);
           
        ECMACopycontext.executeQueryAsync(Function.createDelegate(thisthis.onQuerySucceededCopyOne),
                             Function.createDelegate(thisthis.onQueryFailedCopyOne));
    }
}

 

抱歉!评论已关闭.