Find membership for an user

To check the membership for an user is probably a common task in SharePoint development. People are in general asking to hide or show some things on a page if a user is a member or not of some SharePoint groups. Target audience might be an option, but is not always what you are looking for. If this is the case, you can use JavaScript to determine the membership of an user.

Type.registerNamespace("Shp");


Shp.Users = function () {
    /// <summary>Shp Users class</summary>
    throw "Shp.Users is a static class and cannot have an instance.";
}


Shp.Users.get_groups = function (userId, webUrl, success, fail) {
    /// <summmary>Get groups for current specified user</summary>
    /// <param name="user" type="String" optional="false" mayBeNull="true">NT Account</param>
    /// <param name="web" type="SP.Web optional="false" mayBeNull="true">Web URL</param>
    /// <param name="success" type="Function" optional="false" mayBeNull="false">Success callback</param>
    /// <param name="fail" type="Function" optional="true" mayBeNull="false">Fail callback</param>
    var e = Function.validateParameters(arguments, [{ name: 'userId', type: Number, optional: false, mayBeNull: true },
                                                    { name: 'webUrl', type: String, optional: false, mayBeNull: true },
                                                    { name: 'success', type: Function, optional: false, mayBeNull: false },
                                                    { name: 'fail', type: Function, optional: true, mayBeNull: false }], true);
    if (e) throw e;

    var web = webUrl === null ? SP.ClientContext.get_current().get_web() : new SP.ClientContext(webUrl).get_web();
    var fail = fail || function (err) { alert(err); };
    Shp.Users._get_groups(userId, web, success, fail);
}

Shp.Users._get_groups = function (userId, web, success, fail) {
    var groups = (userId === null) ? web.get_currentUser().get_groups() : web.getUserById(userId).get_groups();
    web.get_context().load(groups);
    web.get_context().executeQueryAsync(function () {
        var g = [];
        var enumerator = groups.getEnumerator();
        while (enumerator.moveNext()) {
            var group = enumerator.get_current();
            g.push(group.get_title());
        }
        success(g);
    }, function (sender, args) {
        fail(args.get_message());
    });
}
Shp.Users.registerClass("Shp.Users");

For example, to perform an action on the page if current user is in some group on current web, you can use the code below:

Shp.Users.get_groups(null, null, function (groups) {
    if (Array.contains(groups, 'SharePoint Site Owners') === true || Array.contains(groups, 'SharePoint Site Members') === true) {
        // Do something here
    }
},
function (err) {
    alert('Cannot get current user groups:' + err);
});

If first parameter is null, the function returns groups for current user as an array of strings. The second parameter can be also null, if you do not need to get the data from another sub-site from your side collection.

Are a lot of things that can be added to this code. Once I will organize it better and improve it, I will share new articles and eve a JavaScript library will be published here.

Leave a Reply

Your email address will not be published. Required fields are marked *