博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SharePoint【ECMAScript对象模型系列】-- 11. Enable/Disable Ribbon上的Button
阅读量:4486 次
发布时间:2019-06-08

本文共 4660 字,大约阅读时间需要 15 分钟。

这里我们想要达到的目标如下:

1、在Ribbon的Ribbon.Library.ViewFormat位置创建一个Button控件。
2、 根据当前登录用户是否在特定的Groups内来决定他是否有权使用(Enable)此Button。
3、 此Button的功能就是跳出一个简单的信息提示框。  

效果如下:

 

按钮工作效果如下

 

操作步骤如下:

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[i];
            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(thisthis.CheckUserSucceeded), Function.createDelegate(thisthis.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"); }

项目如下:

 

说明:

1、关于如何让让用户定义的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)}返回值来实现的。

     转载:

转载于:https://www.cnblogs.com/greeny/archive/2013/02/19/2917879.html

你可能感兴趣的文章
[转]CocoaChina上一位工程师整理的开发经验(非常nice)
查看>>
大数据时代侦查机制有哪些改变
查看>>
L1-047 装睡
查看>>
雷林鹏分享:jQuery EasyUI 菜单与按钮 - 创建链接按钮
查看>>
Apache Traffic Server服务搭建
查看>>
poj1990两个树状数组
查看>>
学习python-day1
查看>>
Zend_Db_Table->insert ()和zend_db_adapter::insert方法返回值不同
查看>>
递归问题
查看>>
Hyperledger下子项目
查看>>
Linq-查询上一条下一条
查看>>
常见前端开发的题目,可能对你有用
查看>>
BeautifulSoap库入门
查看>>
乐观锁与悲观锁
查看>>
Codeforces Round #328 (Div. 2)D. Super M 虚树直径
查看>>
Java判断是否为移动端
查看>>
chromedriver下载链接以及对应版本
查看>>
[SimplePlayer] 6. 音频同步
查看>>
把一个SVN项目的目录结构 导入到另外一个空白的SVN项目里
查看>>
Android之Adapter用法总结-(转)
查看>>