2011-06-21 67 views
4

我正在嘗試獲取它們所屬的當前用戶的SharePoint組名。我一直無法找到提供該信息的方法/屬性。我只能得到當前用戶的用戶名。有沒有一個屬性爲我提供了我沒有看到的這些信息?獲取當前用戶組使用SP 2010 javascript客戶端對象模型

+0

我正在通過同樣的問題,因爲兩天後,我無法獲得當前用戶的角色。你有沒有得到解決方案?請分享。 – 2011-06-22 12:20:44

回答

4

沒有直接的方法通過javascript爲當前用戶返回組。

這是一個post到MSDN討論組,描述瞭解決此問題的方法。 如果您想知道用於檢查權限的組名,解決方法是here

所以基本上:

context = new SP.ClientContext.get_current(); 
web = context.get_web(); 
var value = web.get_effectiveBasePermissions(); 

如果您需要的組名,可惜的是這樣做的直接方式。但是我們可以獲得當前用戶並獲得一個組的用戶集合。然後,您可以從一個組中檢查用戶集合,看它是否包含當前用戶。

  1. 獲取當前用戶:example

  2. 獲取組集合當前web:example

  3. 獲得指定的組

    var groupCollection = clientContext.get_web().get_siteGroups(); 
    // Get the visitors group, assuming its ID is 4. 
    visitorsGroup = groupCollection.getById(4); 
    
  4. 獲取用戶的組

    var userCollection = visitorsGroup.get_users(); 
    
  5. 檢查用戶集合以查看它是否包含指定的用戶。

對於一個簡單的演示,你可以看到以下document

2

正如瓦迪姆Gremyachev表示here可以獲取當前用戶var currentUser = currentContext.get_web().get_currentUser()然後讓所有的羣體var allGroups = currentWeb.get_siteGroups();

從這裏可以遍歷組,看看你的用戶是當前小組。因此,如果您有要檢查的組的列表,成員,所有者,查看者,則只需使用此方法檢測它們是否在每個組中。

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

// example use 
window.IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup){ 
    if(isCurrentUserInGroup){ 
     console.log('yep he is'); 
    } else { 
     console.log('nope he aint'); 
    } 
}); 
相關問題