Wednesday, June 19, 2013

How to check current user in particular group in JavaScript using SPServices

Add two JavaScript file in your Style Library.

1. Javascript 1.7.1.js (Download from here)

2. jquery.SPServices-0.7.1a.min.js (Download from here)

<script type="text/javascript" src="/Style%20Library/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/Style%20Library/js/jquery.SPServices-0.7.1a.min.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function () {        
        $().SPServices({
            operation: "GetGroupCollectionFromUser",
            userLoginName: $().SPServices.SPGetCurrentUser(),
            async: false,
            completefunc: function (xData, Status) {
                if ($(xData.responseXML).find("Group[Name='SPGroup1']").length == 1) {
                    location.href = location.href.replace('/EditFormMKT.aspx', '/EditFormUW.aspx');
                }
                else if ($(xData.responseXML).find("Group[Name='SPGroup2']").length == 1) {
                    location.href = location.href.replace('/EditFormMKT.aspx', '/EditFormLegal.aspx');
                }
                else if ($(xData.responseXML).find("Group[Name='SPGroup3']").length == 1) {
                    location.href = location.href.replace('/EditFormMKT.aspx', '/EditFormDesigner.aspx');
                }
                else if ($(xData.responseXML).find("Group[Name='SPGroup4']").length == 1) {
                    location.href = location.href.replace('/EditFormMKT.aspx', '/EditFormRequester.aspx');
                }
            }
        });
    });
</script>

Group1, Group2 and Group3 etc are SharePoint Groups. Replace your group name with this group name and what ever you want to do write your code below of it.

Here I am replacing the URL and redirect the user based on his role. You can also call particular JavaScript function based on user's group.

Hope this is helpful to you.

3 comments:

  1. I think this is one of the most important info for me. And i'm
    glad reading your article. But wanna remark on few general things, The web site style is ideal,
    the articles is really nice : D. Good job, cheers

    ReplyDelete
  2. I'm glad that it turned out so effectively and I hope it will continue in the future because it is so worthwhile and meaningful to the community.

    ReplyDelete
  3. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. script cek mutasi

    ReplyDelete