0

My requirement is when someone login to site and if he belong to 'owner' group it need to redirect immediately to specific site and if he belong to member group then need to navigate/redirect to same specified site other page.

Like if I open SharePoint site and I belong to Administrators then immediately I get redirected to SharePoint site home page and if I belong to Memebers then I get redirected to another page. Logic should be based on user name in SharePoint server 2010.

Do anyone have functionality for this JavaScript/any other code/programmatically for the same.

2 Answers 2

1

You can add a script editor webpart to the home page and add following script. This script checks if user belongs Members group and then redirects it to some other page. You can enhance it to check if user belongs to other groups.

<script type="text/javascript">
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);
        }    
}

function CheckCurrentUerGroup() 
{
  IsCurrentUserMemberOfGroup("Members", function (isCurrentUserInGroup) {
    if(isCurrentUserInGroup)
    {
        //Redirect to a certain page
        window.location = "http://addressOfPage;
    }
  });

}
ExecuteOrDelayUntilScriptLoaded(CheckCurrentUerGroup, 'SP.js');
</script>

Source: Check if user is in a specified group

6
  • thanks nadeem for this help and one more think i want starting to end functionality. Please explain Feb 6, 2015 at 12:23
  • @PrasanthNellore What do you mean by start to end functionality? Feb 6, 2015 at 12:34
  • where i can add this script and how to uplaod this script to my sharepoint site give steps please Feb 9, 2015 at 7:52
  • @PrasanthNellore, it is mentioned in the answer, you can add in ScripEditorWebpat or you can also edit the page where you need it in sharepoint designer and add the script there. Feb 9, 2015 at 8:07
  • in sps 2010 no script editor webpart in ribbon how to add and i want to add visual webpart for that? Feb 9, 2015 at 9:14
0

Suppose you have two pages : 'OwnerPage' & 'MemberPage'

Write below code on your 'OwnerPage' Page_Load event :

SPUser currentUser = web.CurrentUser;
bool isExist = false;
string groupName = "Owner Group"; // Owner Group Name
isExist = IsUserMemberOfGroup(currentUser, groupName); // call function
if(isExist)
{
   // stay on OwnerPage
}
else
{
   // redirect to MemberPage
}

Calling Function :

// Check that user in the Group or not
public static bool IsUserMemberOfGroup(SPUser user, string groupName)
        {
            bool result = false;

if (!String.IsNullOrEmpty(groupName) && user != null) { foreach (SPGroup group in user.Groups) { if (group.Name == groupName) { // found it result = true; break; } } } return result; }
1
  • Welcomes @Prasanth Nellore , You can mark as 'Accept Answer' if you find the solution with this answer. Feb 20, 2015 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.