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');
        }

 

 


 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM