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

[经验分享] Sharepoint学习笔记—ECMAScript对象模型系列-- 11、 Enable/Disable Ribbon上的Button

[复制链接]

尚未签到

发表于 2015-9-25 11:41:47 | 显示全部楼层 |阅读模式
  这里我们想要达到的目标如下:
1、在Ribbon的Ribbon.Library.ViewFormat位置创建一个Button控件。
2、 根据当前登录用户是否在特定的Groups内来决定他是否有权使用(Enable)此Button。
3、 此Button的功能就是跳出一个简单的信息提示框。   
  效果如下:
DSC0000.jpg
  按钮工作效果如下
DSC0001.jpg
  
  操作步骤如下:
1. 创建一个新的Project,为Farm Solution
2. 在此Project上添加一个新Feature.
3、在此Project上添加一个新的"Empty Element"
4、此Element定义代码如下:


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
       Id="ButtonForGroupUsersOnly"
       Location="CommandUI.Ribbon"
       RegistrationId="101"
       RegistrationType="List"
       Title="Owners Group Button">
        <CommandUIExtension>
            <CommandUIDefinitions>
                <CommandUIDefinition
                Location="Ribbon.Library.ViewFormat.Controls._children">
                    <Button Id="Ribbon.Library.ViewFormat.UsersBtn"
                    Command="usersBtnCommand"
                    LabelText="Group Users Button"
                    TemplateAlias="o1" />"
                </CommandUIDefinition>
            </CommandUIDefinitions>
            <CommandUIHandlers>
                <CommandUIHandler
                Command="usersBtnCommand"
                CommandAction="javascript:alert('Action from this button !');"
                EnabledScript="javascript:EnablerScript(4,5);"/>
            </CommandUIHandlers>
        </CommandUIExtension>
    </CustomAction>
    <CustomAction
         Id="Ribbon.Library.ViewFormat.Scripts"
         Location="ScriptLink"
         ScriptSrc ="/_layouts/SPRiboonTest/CheckUserInGroup.js"/>
</Elements>  5、添加一个Javascrip文件CheckUserInGroup.js,内容如下:


///<reference name="MicrosoftAjax.js" />
///<reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.core.debug.js" />
///<reference path="file://C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/TEMPLATE/LAYOUTS/SP.debug.js" />

var idGroupToVerify1 = 4;
var idGroupToVerify2 = 5;
var nameGroup1 = "DevpTest Owners";
var nameGroup2 = "DevpTest Visitors";
var arrIsInThisGroup = new Array(2);
arrIsInThisGroup["DevpTest Owners"] = false;
arrIsInThisGroup["DevpTest Visitors"] = false;
ExecuteOrDelayUntilScriptLoaded(InitGroups, "sp.js");

//initialize groups ids for the current user
function InitGroups() {
    //debugger;
    CheckUser(idGroupToVerify1, nameGroup1);  //Judge if user is in the given group
    //beacuse of some errors if execute together
    setTimeout("CheckUser(" + idGroupToVerify2 + ", '" + nameGroup2 + "')", 500);
}
//groupIDs is all the groups we need to check, but only 4 and 5 can enable the button
function EnablerScript(groupIDs) {
    if (groupIDs) {
        var isButtonEnabled = false;
        //groupid comes in string comma separated
        // '4' or '4,5'
        var arrGrId = groupID.split(',');
        var i = 0;
        for (i = 0; i < arrGrId.length; i++) {
            var currGroupID = arrGrId;
            if (currGroupID == idGroupToVerify1)
                isButtonEnabled = isButtonEnabled || arrIsInThisGroup[nameGroup1];
            if (currGroupID == idGroupToVerify2)
                isButtonEnabled = isButtonEnabled || arrIsInThisGroup[nameGroup2];
        }
        return isButtonEnabled;
    }
    return false;
}
// The below checks if the user exists in the group
function CheckUser(groupID, isInThisGroup) {
    //debugger;
    var context = SP.ClientContext.get_current();
    //I go to parent site if I'm in a subsite!
    var siteColl = context.get_site();
    web = siteColl.get_rootWeb();
    var groupCollection = web.get_siteGroups();
    // Get the Our Group's ID
    var _group = groupCollection.getById(groupID); // ID of the Group that we are checking
    var users = _group.get_users(); // Get all Users of the group we are checking
    context.load(_group);
    context.load(users);
    this._users = users;  //Get users of the checking group
    this._currentUser = web.get_currentUser(); // Get current login user
    this._isInThisGroup = isInThisGroup;
    context.load(this._currentUser);
    context.executeQueryAsync(Function.createDelegate(this, this.CheckUserSucceeded), Function.createDelegate(this, this.failed));
}
//The below Checks  if User is the member of the specified group
function CheckUserSucceeded() {
    //debugger;
    if (this._users.get_count() > 0) {
        var _usersEnum = this._users.getEnumerator();
        while (_usersEnum.moveNext()) { //Scan all the users in specific group and to see if the current user exists in these users
            var user = _usersEnum.get_current(); //Get current login user
            if (user.get_loginName() == this._currentUser.get_loginName()) { //compare user
                //debugger;
                arrIsInThisGroup[this._isInThisGroup] = true; //if the user exists in the group then set the flag to be true
            }
        }
    }
}
function failed(sender, args) { alert("error"); }  项目如下:
DSC0002.jpg
  说明:
1、关于如何让让用户定义的Ribbon引用外部Javascript文件,请参考此文Sharepoint学习笔记&#8212;Ribbon系列-- 9.如何让用户定义的Ribbon引用外部Javascript文件。
2、在CheckUserInGroup.js中,我们预定义了可以Enable此Button的Groups(groupid分别是4和5),如果要检查的goups不在此预定义的goups内,或者当前LoginUser也不在此预定义的goups内则此Button不可使用。CheckUserInGroup.js代码如下,你可能根据你自身情况进行修改。
  3、<CommandUIHandler>中的EnableScript属性为True则Button被Enable,否则Disable,而此值的设置是通过其内的Javascript {javascript:EnableScript(4,5)}返回值来实现的。
  

运维网声明 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-118642-1-1.html 上篇帖子: 我的第一个SharePoint WebPart 下篇帖子: Developing SharePoint 2010 Search Solutions (Fast and SharePoint)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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