0

My requirement is when someone login to site and if he belong to 'owner' group it need to navigate immediately to specific site and if he belong to member group then need to navigate to another specified site. Like if i open google site and i belong to admin then immediately i get navigate to bing site and if i below to member then i navigate to sharepoint site.

Do anyone have javascipt for the same

2

You can embed script web parts (Content Editor or Form web part) targeted at these groups with each containing a javascript instruction to redirect (change location.href) or open the needed site in a new window. Use the Web Part Properties Pane Advanced section's Target Audience picker and enter the group names for each web part.

  • The one problem with this approach as opposed to code based suggestions is that you cannot predict where the user will be redirected in case he/she is a member of both groups. – Alexey Krasheninnikov Dec 18 '13 at 10:19
1

Note: This is client Object model approach:

You can get the group collection of a site and enumerate over it to find a specific group. You can then enumerate the members of the group to find the current user. And if a user belongs to that group you can redirect the user using window.location. Hope it helps:

     function onGetSharePointGroup() {
     var context = SP.ClientContext.get_current();
     var groupsEnum = this.groupCollection.getEnumerator();
     while (groupsEnum.moveNext()) {
        var group = groupsEnum.get_current();
        if (group.get_title() == "GROUP NAME TO FIND") {                    
            this.members = group.get_users();
            context.load(members);
        }
    }
    if (members != null) {
        context.executeQueryAsync(Function.createDelegate(this, this.FindCurrentUser), Function.createDelegate(this, this.onFailure));
    }
}
  • where i need to put url to which site need to navigate – Brishal Dec 17 '13 at 10:44
0

You can try this code snippet to target your functionality through ECMA script:

var adminUsers;
var vCurrUserName;
var groupCollection;
var _group;

function checkUser() {
var currentContext = new SP.ClientContext.get_current();

//get current user

vCurrUserName = currentContext.get_web().get_currentUser();

currentContext.load(vCurrUserName);

// get Admin group

groupCollection = currentContext.get_web().get_siteGroups();
currentContext.load(groupCollection);

//var _group = groupCollection.getById(10); // ID of the Group

currentContext.executeQueryAsync(Function.createDelegate(this, this.checkUserSuccess),
  Function.createDelegate(this, this.checkUserFailure));

}
var vIsAdmin = false;
function checkUserSuccess() {
var currentContext = new SP.ClientContext.get_current();

var grpEnum = groupCollection.getEnumerator();

while(grpEnum.moveNext()){
var grpItem = grpEnum.get_current();
currentContext.load(grpItem);

// here you can add one more if condition to check for "Member" group.

if(grpItem.get_title() == "Owner"){
    _group = grpItem;
    break;
 }
}

adminUsers = _group.get_users();
currentContext.load(adminUsers);

currentContext.executeQueryAsync(Function.createDelegate(this, this.findUserInGroup), 
Function.createDelegate(this, this.checkUserFailure));
}

function findUserInGroup(){
var listEnumerator = adminUsers.getEnumerator();

while (listEnumerator.moveNext()) {

var item = listEnumerator.get_current();

//check current user login name and user login name from admin group

if (vCurrUserName.get_loginName() == item.get_loginName()) {

vIsAdmin = true;

break;

    }

}

if (vIsAdmin) {
  // Owner group Url.
}
else
{
  // Member group Url.
}
  • Hi, Code is not working I have done below changes 1.Uncomment //var _group = groupCollection.getById(10); and added var _group = groupCollection.getById(5146); 2.Edited , if(grpItem.get_title() == "Owner") to if(grpItem.get_title() == "Issue tracker") where Issue tracker is group name 3. // Owner group Url to "url.com" Let me know where i am doing wrong – Brishal Dec 17 '13 at 10:40
  • have you debug this, where did you get error, Please mention error message here, so that i can identify. – Rishi Jagati Dec 18 '13 at 7:02
  • There is no error in code , it is just not working :( – Brishal Dec 18 '13 at 9:45
0
<script type="text/javascript">
    $(document).ready(function () {
    alert(1);
        SP.SOD.executeOrDelayUntilScriptLoaded(LoadScriptForProfAndContacts, 'SP.UserProfiles.js');
    });

    //function LoadScriptForProfAndContacts() {

        //SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'sp.js');
    //}
    function LoadScriptForProfAndContacts() {
    alert(1);
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', Function.createDelegate(this, function () {      

            var clientContext = SP.ClientContext.get_current();
            //get the users domain name and account name.
            var website = clientContext.get_web();  
            this.currentUser = website.get_currentUser(); 
            clientContext.load(this.currentUser);
            this.spuserGroups = this.currentUser.get_groups();
            clientContext.load(this.spuserGroups);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.GetLoginName), Function.createDelegate(this, this.onQueryFailed));
    }));
    }

    function GetLoginName(sender, args) {   
                    alert(1);
        var loginName = this.currentUser.get_loginName();
            alert(loginName);
        this.domainName = loginName.substring(loginName.indexOf("|")+1, loginName.indexOf("\\"))        
        this.currUserWWID = loginName.substring(loginName.indexOf("\\")+1);

        var clientContext = SP.ClientContext.get_current();

        this.IsSuperAdmin = false; 
                                if (this.spuserGroups.get_count() > 0) {       
                        var groupsEnumerator = this.spuserGroups.getEnumerator();
                        while (groupsEnumerator.moveNext()) {
                                var group = groupsEnumerator.get_current();  
                                GroupName = document.createElement("div");
                GroupName .setAttribute("class", "nopadding col-md-4 text-align-left");
                GroupName .appendChild(document.createTextNode(group.get_title())); 
                document.getElementById("1").appendChild(GroupName)            
                            if(group.get_title() == "Group1") {

                                //redirect
                             }
                             else if(group.get_title() == "Group2")
                             {
                             //redirect

                             }

                            }
                            }

    }


</script>
<div id ="1">hello</div>
  • 1
    Just code does not help in understanding. Would you please add some brief description about it :) – Asad Refai Aug 14 '15 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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