locked
SPServices AddUserToGroup by MAIL ID RRS feed

  • Question

  • I can able to add an user into SharePoint group by LoginName using this code,

    function AddUserToGroup(userLoginName)
    {

        alert(userLoginName);  //Getting domain\username

        $().SPServices({
            operation: "AddUserToGroup",
            async: false,
            groupName: "GroupName",
            userLoginName: userLoginName,
            webURL:'/SiteURL/',
            completefunc: function(data,status){
               //...

            }
        });
    }

    But not able to add user into SharePoint group by MAIL ID using below code,

    function AddUserToGroupUsingMailId(userEmail)
    {
        alert(userEmail);  //getting mail id

        $().SPServices({
            operation: "AddUserToGroup",
            async: false,
            groupName: "GroupName",
            userEmail: userEmail,
            webURL:'/SiteURL/',
            completefunc: function(data,status){
               //...

            }
        });
    }

    I am getting only single value from respective code file, it could be LoginName or Mail ID so based on this value we need to add in SharePoint group.

    FYI - User based on LoginName or MAIL ID that present in AD.

    Please help me for this. 

                            

    sharadpatil

    Thursday, February 25, 2016 3:13 PM

Answers

  • Hi sharadpatil,

    I tested the script in my environment, and I got the error when using userEmail: The parameter loginName cannot be empty or bigger than 251 characters..

    From the error message, it means that the userLoginName parameter should not be empty or bigger than 252 characters, so we can only use userLoginName parameter with SPServices AddUserToGroup function.

    I recommend to get user login name by email ID first and then call SPServices with user login name parameter.

    http://sharepoint.stackexchange.com/questions/22693/how-do-i-retrieve-the-name-of-the-user-using-spservices-given-the-email-address

    https://msdn.microsoft.com/en-us/library/websvcusergroup.usergroup.getuserloginfromemail(v=office.14).aspx

    Best regards,

    Victoria

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Friday, February 26, 2016 3:44 AM
  • Excellent!!!

    Thank you much!!! Victoria Xia...For giving a valuable inputs.

    First get a login name by id using SP Service "GetUserLoginFromEmail" then adding userLoginName parameter to SPServices "AddUserToGroup" function.

    I did some troubleshooting as,

    function convertEmailIdToUserName(domainUserName, action){
    alert(domainUserName + ":"+ action);
    var userName;
        $().SPServices({
            operation: "GetUserLoginFromEmail",
            emailXml: "<Users><User Email='"+domainUserName+"'/></Users>",
            
            completefunc: function(xData, Status){
                console.log(xData.responseText);
                $(xData.responseXML).find("User").each(function() {
                   userName= $(this);
                   console.log("username = " + userName);
                   if(action=='remove' )
                      {
                          RemoveUserFromGroup(userName.attr("Login"));
                      }
                      else{
                       AddUserToGroup(userName.attr("Login"));
                   }
                })
            
            }
        });
        console.log(userName);
    }


    sharadpatil


    Tuesday, March 1, 2016 1:15 PM

All replies

  • Hi sharadpatil,

    I tested the script in my environment, and I got the error when using userEmail: The parameter loginName cannot be empty or bigger than 251 characters..

    From the error message, it means that the userLoginName parameter should not be empty or bigger than 252 characters, so we can only use userLoginName parameter with SPServices AddUserToGroup function.

    I recommend to get user login name by email ID first and then call SPServices with user login name parameter.

    http://sharepoint.stackexchange.com/questions/22693/how-do-i-retrieve-the-name-of-the-user-using-spservices-given-the-email-address

    https://msdn.microsoft.com/en-us/library/websvcusergroup.usergroup.getuserloginfromemail(v=office.14).aspx

    Best regards,

    Victoria

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Friday, February 26, 2016 3:44 AM
  • Excellent!!!

    Thank you much!!! Victoria Xia...For giving a valuable inputs.

    First get a login name by id using SP Service "GetUserLoginFromEmail" then adding userLoginName parameter to SPServices "AddUserToGroup" function.

    I did some troubleshooting as,

    function convertEmailIdToUserName(domainUserName, action){
    alert(domainUserName + ":"+ action);
    var userName;
        $().SPServices({
            operation: "GetUserLoginFromEmail",
            emailXml: "<Users><User Email='"+domainUserName+"'/></Users>",
            
            completefunc: function(xData, Status){
                console.log(xData.responseText);
                $(xData.responseXML).find("User").each(function() {
                   userName= $(this);
                   console.log("username = " + userName);
                   if(action=='remove' )
                      {
                          RemoveUserFromGroup(userName.attr("Login"));
                      }
                      else{
                       AddUserToGroup(userName.attr("Login"));
                   }
                })
            
            }
        });
        console.log(userName);
    }


    sharadpatil


    Tuesday, March 1, 2016 1:15 PM