Skip to main content
 首页 » 编程设计

sharepoint之使用 javascript 客户端对象模型检查当前用户是否属于 SP 组

2024年08月12日9lovecherry

我还没有找到如何获取当前用户,然后检查它是否属于特定共享点组的具体示例,因为我还没有找到任何无法提供代码的内容,

感谢您提供正确方向的帮助。

请您参考如下方法:

SharePoint 2013 CSOM

Prerequisites: compatible with SharePoint 2013 CSOM API only since SP.GroupCollection.getByName Method is not available in SharePoint 2010

如何通过 CSOM (JavaScript) 检查当前用户是否属于 SharePoint 组:

function IsCurrentUserMemberOfGroup(groupName, OnComplete) { 
 
        var currentContext = new SP.ClientContext.get_current(); 
        var currentWeb = currentContext.get_web(); 
 
        var currentUser = currentContext.get_web().get_currentUser(); 
        currentContext.load(currentUser); 
 
        var allGroups = currentWeb.get_siteGroups(); 
        currentContext.load(allGroups); 
 
        var group = allGroups.getByName(groupName); 
        currentContext.load(group); 
 
        var groupUsers = group.get_users(); 
        currentContext.load(groupUsers); 
 
        currentContext.executeQueryAsync(OnSuccess,OnFailure); 
 
        function OnSuccess(sender, args) { 
            var userInGroup = false; 
            var groupUserEnumerator = groupUsers.getEnumerator(); 
            while (groupUserEnumerator.moveNext()) { 
                var groupUser = groupUserEnumerator.get_current(); 
                if (groupUser.get_id() == currentUser.get_id()) { 
                    userInGroup = true; 
                    break; 
                } 
            }   
            OnComplete(userInGroup); 
        } 
 
        function OnFailure(sender, args) { 
            OnComplete(false); 
        }     
} 

使用

IsCurrentUserMemberOfGroup("Approvers", function (isCurrentUserInGroup) { 
   if(isCurrentUserInGroup) 
   { 
      //... 
   } 
}); 

SharePoint 2010/2013 CSOM

function isUserMemberOfGroup(userId, groupId, success,error) { 
 
     var ctx = SP.ClientContext.get_current();  
     var allGroups = ctx.get_web().get_siteGroups(); 
     var group = allGroups.getById(groupId); 
     ctx.load(group,'Users'); 
 
     ctx.executeQueryAsync( 
        function(sender, args) { 
            var userInGroup = findUserById(group.get_users(),userId); 
            success(userInGroup); 
       }, 
       error);     
 
       var findUserById = function(users,id){ 
           var found = false; 
           var e =  group.get_users().getEnumerator(); 
           while (e.moveNext()) { 
                var user = e.get_current(); 
                if (user.get_id() == id) { 
                    found = true; 
                    break; 
                } 
           }  
           return found;  
       }; 
} 

使用

var currentUserId = _spPageContextInfo.userId; 
var groupId = 4; 
isUserMemberOfGroup(currentUserId, groupId,  
      function (isCurrentUserInGroup) { 
          if(isCurrentUserInGroup) 
              console.log('Current user is a member of Owners group');  
          else 
              console.log('Current user is not a member of Owners group');  
      }, 
      function(sender,args){ 
         console.log(args.get_message()); 
      });