设为首页 收藏本站
查看: 1181|回复: 0

[经验分享] Sharepoint学习笔记—ECMAScript对象模型系列-- 10、 复制/移动Document List中的文档

[复制链接]

尚未签到

发表于 2015-9-25 12:07:06 | 显示全部楼层 |阅读模式
  使用ECMAScript对象模型,我们可以实现对DocumentList中Files的拷贝或移动。在这里,我们需要实现的的效果是当选中某个Document List中的文档时,在其关联Ribbon区出现一个按钮,点击此按钮,可以实现把这个选中的文档复制或移动到另一个Document List中。  效果图如下:
DSC0000.jpg
  
  这里我们在代码中指定了目标Document List的名字,而在实际操作中,你可以在点击按钮后弹出模态窗口,让用户自行选择或输入目标Document List的名字,以增强灵活性。

   一、复制功能的实现
直接进入步骤
1、新建一个Sharepoint空项目,命名为ECMAscriptCopyFile
DSC0001.jpg
  2、在此项目中添加新的Feature,并命名为ECMAscriptCopyFileFeature
DSC0002.jpg
命名此Feature: DSC0003.jpg
  
  3、在此项目中添加Images与Layouts两个Sharepoint目录。
  拷贝一个文件拷贝的图片放到Images\ECMAscriptCopyFile\目录下,并命名为copyFile.png以备用。
DSC0004.jpg
  
  4、添加一个新的空Element,命名为ECMAscriptCopyFileElement.并定义此元素的代码
DSC0005.jpg


<?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.id);
        ECMACopycontext.load(currentItem);
        //debugger;
        ECMACopyFile = currentItem.get_file(); //Get the source file
        ECMACopycontext.load(ECMACopyFile);
        ECMACopycontext.executeQueryAsync(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();
        
         notifyId = SP.UI.Notify.addNotification('Copying file&#8230;' + 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(this, this.onQuerySucceededCopyOne),
                             Function.createDelegate(this, this.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());
}  
  完成后,整个项目如下图:
DSC0006.jpg
  6、Build并部署这个项目,然后到测试网站上查看效果如下:
DSC0007.jpg
DSC0008.jpg
  
     DSC0009.jpg
  
DSC00010.jpg
  
  二、移动选中文档的功能实现
  绝大部分和上述步骤相同,只是在文档移动代码上需要说明,我们需要用File.moveTo()代码来代替前面的File.copyTo()代码,有些人用moveTo函数会遇到失败的结果,那是因为他们设置了错误的参数。
File.moveTo()函数的参数中,第一项参数与copyTo相同,都是目标文档的地址。但第二项不再是Boolean类型,而是SP.MoveOperations Enumeration类型,此类型可能的选项如下:
DSC00011.jpg
  
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&#8230;' + ECMACopyFile.get_serverRelativeUrl()
                                               + 'to' + ECMACopy_destinationlibUrl, true);
         ECMACopyFile.moveTo(ECMACopy_destinationlibUrl, SP.MoveOperations.Overwrite);
           
        ECMACopycontext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededCopyOne),
                             Function.createDelegate(this, this.onQueryFailedCopyOne));
    }
}  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118673-1-1.html 上篇帖子: [原]SharePoint文档库上传文档 下篇帖子: What's new document web part for SharePoint [Free]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表