如何检查Sharepoint用户是否在安全组中,无论它是否在AD组CSOM中

时间:2015-07-31 15:02:09

标签: javascript sharepoint csom sharepoint-online

我有一个SharePoint托管应用程序,我需要检查当前用户是否在SharePoint安全组中,无论它是否在组内的AD中。以下代码检查用户是否在安全组中,但前提是它们是否明确位于安全组中。代码不会查看安全组中的子组。

    function getItems() {

    // only execute this function if the script has been loaded
    if (ready) {

        // the url to use for the REST call.
        var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
            "/web/lists/getbytitle('" + TituloListaEventos + "')/items?$select=Title,Category,StartDate,EndDate,EncodedAbsUrl,ID,LinkSolicitud&$filter=Category eq '" + Categoria + "'" +
            "&@target='" + SPHostUrl + "'";

        // create  new executor passing it the url created previously
        var executor = new SP.RequestExecutor(SPAppWebUrl);

        // execute the request, this is similar although not the same as a standard AJAX request
        executor.executeAsync(
            {
                url: encodeURI(url),
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" },
                success: function (data) {
                    IsCurrentUserMemberOfGroup("Solicitantes", function (isSolicitante) {
                    ...
                },
                error: function (data) {
                    ...
                }
            });

    }
}

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

1 个答案:

答案 0 :(得分:1)

SharePoint客户端对象模型不支持读取活动目录组。您可能必须创建自定义Web服务。如果您可以选择使用.NET,我强烈建议您使用.NET,因为它在访问SharePoint数据和交叉引用Active Directory数据时效果很好。