Sharepoint学习笔记—ECMAScript对象模型系列-- 9、组与用户操作(二)

   接着上面的继续,这里我们描述的关于User与Group的操作如下:

     6、 向指定Group中添加指定User
     7、 获取指定Group的Owner
     8、 把当前登录用户添加到指定Group中
     9、 判断当前登录用户是否有EditPermission权限
    10、判断当前登录用户是否在某特定的Group中 

  分别描述如下:

     6、 向指定Group中添加指定User

   var siteUrl = '/';
    function addUserToSharePointGroup(groupID) {
        //var clientContext = new SP.ClientContext(siteUrl);
        var clientContext = new SP.ClientContext.get_current();
        var collGroup = clientContext.get_web().get_siteGroups();
        var oGroup = collGroup.getById(groupID);
        var userCreationInfo = new SP.UserCreationInformation();
        userCreationInfo.set_email('helpdesk@GLSTAR.COM.AU);
        userCreationInfo.set_loginName('GLSTAR\\helpdesk');
        userCreationInfo.set_title('helpdesk');
        this.oUser = oGroup.get_users().add(userCreationInfo);  //add user into group
//        var userInfo = '\nUser: ' + oUser.get_title() +
//            '\nEmail: ' + oUser.get_email() +
//            '\nLogin Name: ' + oUser.get_loginName();
//        alert(userInfo);
        clientContext.load(oUser);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededaddUserToSharePointGroup),
        Function.createDelegate(this, this.onQueryFailedaddUserToSharePointGroup));
    }

    function onQuerySucceededaddUserToSharePointGroup() {
        alert(this.oUser.get_title() + " added.");
    }
    function onQueryFailedaddUserToSharePointGroup(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

 

  7、 获取指定Group的Owner

  // Get The Group Owner Name in SharePoint 2010 Using ECMAScript
     var group;
     function getGroupOwnerName(groupID) {
         var clientContext =  new SP.ClientContext();
         var groupCollection = clientContext.get_web().get_siteGroups();
        group = groupCollection.getById(groupID);
        clientContext.load(group);
        clientContext.executeQueryAsync(Function.createDelegate( thisthis.onQuerySucceededgetGroupOwnerName),
                                        Function.createDelegate( thisthis.onQueryFailedgetGroupOwnerName));

    }

     function onQuerySucceededgetGroupOwnerName() {
        alert("GroupTitle:  " + group.get_title() + "\nGroupOwnerTitle : " + group.get_ownerTitle());
    }

     function onQueryFailedgetGroupOwnerName(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

     8、 把当前登录用户添加到指定Group中

  // adds the current user to the specific group on the current website
     var currentUser;
     var visitorsGroup;
     function addUserToSpecificGroupInCurrWeb(groupID) {

         var clientContext =  new SP.ClientContext();
         var groupCollection = clientContext.get_web().get_siteGroups();
        visitorsGroup = groupCollection.getById(groupID);
        currentUser = clientContext.get_web().get_currentUser();
         var userCollection = visitorsGroup.get_users();
        userCollection.addUser(currentUser);

        clientContext.load(currentUser);
        clientContext.load(visitorsGroup);
        clientContext.executeQueryAsync(Function.createDelegate( thisthis.onQuerySucceededaddUserToSpecificGroup),
        Function.createDelegate( thisthis.onQueryFailedaddUserToSpecificGroup));

    }

     function onQuerySucceededaddUserToSpecificGroup() {
        alert(currentUser.get_title() + " added to group " + visitorsGroup.get_title());
    }

     function onQueryFailedaddUserToSpecificGroup(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

     9、 判断当前登录用户是否有EditPermission权限

   // Check current user has Edit Permission
     var theCurrentUser;
     var theWeb;
     function checkifUserHasEditPermissions() {
        //  debugger;
         var context =  new SP.ClientContext.get_current();
        theWeb = context.get_web();
        theCurrentUser = theWeb.get_currentUser();
        context.load(theCurrentUser);
        context.load(theWeb, 'EffectiveBasePermissions');
        context.executeQueryAsync(Function.createDelegate( thisthis.onSuccessMethodcheckEditPermissions),
                                  Function.createDelegate( thisthis.onFailureMethodcheckEditPermissions));
    }
     function onSuccessMethodcheckEditPermissions() {
         // debugger;
         if (theWeb.get_effectiveBasePermissions().has(SP.PermissionKind.editListItems)) {
             // User Has Edit Permissions
            alert(theCurrentUser.get_loginName()+"Has Edit Permission on current website");
        }
         else {
            alert("No Edit Permission");
        }
    }
     function onFailureMethodcheckEditPermissions() {
        alert("Failed to check permission");
    }

    10、判断当前登录用户是否在某特定的Group中   

    var IsInThisGroupFlag =  false;
     //  The below checks if the user exists in the group
     function checkIfCurrentUserIsInGroup(groupID) {
         var context = SP.ClientContext.get_current();
         // I go to parent site if I'm in a subsite!
         var siteColl = context.get_site();
         var 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
        context.load(_group);
        context.load(users);
         this._users = users;
         this._currentUser = web.get_currentUser();  //  Get current user
        context.load( this._currentUser);
        context.executeQueryAsync(Function.createDelegate( thisthis.CheckUserSucceededUserIsInGroup),
        Function.createDelegate( thisthis.CheckUserfailedUserIsInGroup));
    }

     // The below Checks  if User is the member of the specified group
     function CheckUserSucceededUserIsInGroup() {
         // debugger;
        IsInThisGroupFlag =  false;
         if ( this._users.get_count() > 0) {
             var _usersEnum =  this._users.getEnumerator();
             while (_usersEnum.moveNext()) {
                 var user = _usersEnum.get_current();
                 if (user.get_loginName() ==  this._currentUser.get_loginName()) {
                     // debugger;
                    IsInThisGroupFlag =  true;
                }

                 if (IsInThisGroupFlag) {
                    alert(user.get_loginName() + "  exist in the checked group " + IsInThisGroupFlag.toString());
                }
                 else {
                    alert(user.get_loginName() + "  not exist in the checked group ");
                }
            }
        }
    }

     function CheckUserfailedUserIsInGroup() {
            alert('failed');
        }

 

 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值