Sharepoint学习笔记—ECMAScript对象模型系列-- 12、通过邮件发送带有Unique DocumentID的文档链接...

在Sharepoint Document List默认的Ribbon中有这么一个发送Email的按钮,通过它可以把选中的文档分享给其它用户,如下图:

 

     但在发送的邮件内,默认Email按钮采用的是发送文档的Url地址,而并没有用到Sharepoint提供的Unique Document ID,使用文档的URL分享文档最明显的坏处就是,一旦我们移动了这个文档,那么这个URL就失效了,曾经分享过这个文档的用户要想再通过这个URL链接来获取这个文档就不再会成功。所以在这里,我们就通过ECMAscript结合Ribbon的相关知识来实现通过Sharepoint2010提供的Unique Document ID把文档通过邮件分享给其它用户的目标。下面进入操作步骤。

1、新建一个空的Sharepoint项目

 
2、添加新的Feature,并命名为EmailLinkFeature,如下图

3、添加新的空Element,命名为EmailLinkElement

此Element的代码如下

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
     Id="Ribbon.EmailUniqueLink"
     Location="CommandUI.Ribbon"
     RegistrationId="101"
     RegistrationType="List">
        <CommandUIExtension>
            <CommandUIDefinitions>
                <CommandUIDefinition
                   Location="Ribbon.Documents.Share.Controls._children">
                    <Button
                       Id="Ribbon.Documents.Share.EmailUniqueLink"
                       Command="Ribbon.Documents.Share.EmailUniqueLink"
                       Sequence="15"
                       Image16by16="/_layouts/$Resources:core,Language;/images/formatmap16x16.png"
                       Image16by16Top="-16"
                       Image16by16Left="-88"
                       Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png"
                       Image32by32Top="-128"
                       Image32by32Left="-448"
                       Description="Sends the unique link to the document by e-mail"
                       LabelText="E-mail Unique Link"
                       ToolTipTitle="E-mail Unique Link"
                       ToolTipDescription="Sends the unique link to the document by e-mail"
                       TemplateAlias="o1"/>
                </CommandUIDefinition>
            </CommandUIDefinitions>
            <CommandUIHandlers>
                <CommandUIHandler
                   Command="Ribbon.Documents.Share.EmailUniqueLink"
                   CommandAction="javascript:EmailUniqueLink();"
                   EnabledScript="javascript:EnableEmailUniqueLink();"/>
            </CommandUIHandlers>
        </CommandUIExtension>
    </CustomAction>
    <CustomAction
       Id="Ribbon.Documents.Share.EmailUniqueLink.Script"
       Location="ScriptLink"
       ScriptSrc ="/_layouts/EmailLinkButton/EmailLinkButton.js"/>

</Elements>

 

4、添加Sharepoint的Layouts目录,并在此目录下新添加一个Javascript文件:EmailLinkButton.js文件

EmailLinkButton.js的内容如下:

//  This method will contain most of the code needed to request the unique url to the document
function EmailUniqueLink() {
     //  First get the context and web
     var ctx = SP.ClientContext.get_current();
     this.web = ctx.get_web();
     //  Get the current selected list, then load the list using the getById method of Web (SPWeb)
     var listId = SP.ListOperation.Selection.getSelectedList();
     var sdlist =  this.web.get_lists().getById(listId);
     //  Get the currently selected item of the list. This will return a dicustonary with an id field
     var items = SP.ListOperation.Selection.getSelectedItems(ctx);
     var mijnid = items[0];
     //  Request the list item from the server using the getItemById method. This will load all properties.   
     //  If needed, one could pre-request the fields to be loaded to preserve bandwidth.
     this.listItem = sdlist.getItemById(mijnid.id);
     //  load the item in the context for batch operation.
    ctx.load( this.listItem);
     // Execute the actual script on the server side. Specify delegates to handle the response. 
    ctx.executeQueryAsync(Function.createDelegate( thisthis.onQuerySucceeded), Function.createDelegate( thisthis.onQueryFailed));
}

//  Delegate that is called when server operation is complete upon success.
function onQuerySucceeded(sender, args) {
     //  Request url by using the get_item method. It will return the Url field type, which has a Url property. 
     var url =  this.listItem.get_item('_dlc_DocIdUrl').get_url();
     //  Request the name of the document. 
     var title =  this.listItem.get_item('FileLeafRef');
     //  Open a new e-mail in the default mail program.
    window.open('mailto:?subject=Emailing%3A%20' + title + '&body=' + url);
}

//  Delegate that is called when server operation is completed with errors.
function onQueryFailed(sender, args) {
    alert('failed ' + args.toString());
}

//  Method to enable/disable the e-mail unique button on the ribbon. 
function EnableEmailUniqueLink() {
     //  request number of selected items. 
     var items = SP.ListOperation.Selection.getSelectedItems();
     var count = CountDictionary(items);
     //  only return true is a single item is selected. 
     return (count == 1);
}
 

5、Build并部署我们的项目。
6、测试项目如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值