none
Unable to programatically add a user to a SharePoint Group RRS feed

  • Question

  • Hi,

    I am having this issue in my SharePoint 2013 environment. I have a few SharePoint groups and respectively assigned each as owner to a sub site.

    E.g.

    Group1 owns => Sub Site A

    Group2 owns => Sub Site B

    I deployed a custom page on each website (uses the same master page and common codes) to manage members to each group. The form simply ask for the new group member's Windows Username (can accept multiple usernames) then press "Save" button.

    Once the save button is clicked, the following code is executed

    public void AddUsersToGroup(List<User> users, SPGroup group)
    {
        foreach (User user in users)
        {
            try
            {        
                SPClaimProviderManager cpm = SPClaimProviderManager.Local;
                SPClaim userClaim = cpm.ConvertIdentifierToClaim(user.LoginName, SPIdentifierTypes.WindowsSamAccountName);        
                string claimLoginName = userClaim.ToEncodedString();

                group.AddUser(web.EnsureUser(claimLoginName));        
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
        }
    }

    Note: The "User" class is a custom domain class.

    Our custom code did not catch an exception but after i checked the SharePoint log, I found this error message

    +++++++++++++++++++++++++++++++++++++

    The user does not exist or is not unique.
    Stack trace: onetutil.dll: (unresolved symbol, module offset=00000000000A2337) at 0x000007FE0AE62337 onetutil.dll: (unresolved symbol, module offset=00000000000A2281) at 0x000007FE0AE62281 owssvr.dll: (unresolved symbol, module offset=0000000000054D05) at 0x000007FE0A414D05 owssvr.dll: (unresolved symbol, module offset=00000000001363A1) at 0x000007FE0A4F63A1 Microsoft.SharePoint.Library.ni.dll: (unresolved symbol, module offset=00000000000B3F2A) at 0x000007FE0B123F2A
    SPRequest.UpdateMembers: UserPrincipalName=, AppPrincipalName= ,bstrUrl=https://devsharepoint.local/sites/Site_A ,dwObjectType=0 ,bstrObjId= ,lGroupID=112 ,lGroupOwnerId=20 ,bRemoveFromCurrentScopeOnly=False ,bSendEmail=True
    System.Runtime.InteropServices.COMException: The user does not exist or is not unique.

    +++++++++++++++++++++++++++++++++++++

    Has anyone experienced this before? Greatly appreciate your inputs and feedback.

    Thank you.




    • Edited by keny2010 Friday, May 22, 2015 6:39 AM
    Thursday, May 21, 2015 9:51 AM

All replies

  • Hi,

    According to your description, my understanding is that you want to add user to SharePoint Group based on Claim authentication.

    For the error message, I suggest you can debug the custom code to check if the user object is valid. 

    For the requirement, here is a sample code for your reference:

    Add users to SP Group Programatically for a Claims Based Web Application

    Thanks

    Best Regards


    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, May 22, 2015 2:55 AM
  • Thanks for the response.

    I got a typo error on this part of the code. Its suppose to be passing an SPUser object and not string (user loginname).

    [Wrong]

     group.AddUser(claimLoginName);     

    [Correct]  

     group.AddUser(web.EnsureUser(claimLoginName));  

    ++

    for some reasons this issue happen to certain users only and most of the time works. so i'm quite puzzled what really goes on in the SharePoint back-end.

    Friday, May 22, 2015 6:42 AM
  • Hi,

    The "AddUser" method needs the SPUser Object as Parameter. So the string "ClaimLoginname" will not be correct.

    More information, please refer the article:

    SPGroup.AddUser method

    Thanks

    Best Regards


    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.


    Monday, May 25, 2015 10:01 AM
  • Hi Jerry,

    Yes, you are correct. That is why I replied to your previous response with the following

    +++++

    I got a typo error on this part of the code. Its suppose to be passing an SPUser object and not string (user loginname).

    [Wrong]

     group.AddUser(claimLoginName);     

    [Correct]  

     group.AddUser(web.EnsureUser(claimLoginName));  

    +++++

    for some reasons this issue happen to certain users only and most of the time works. so i'm quite puzzled what really goes on in the SharePoint back-end.

    Is there any other way to find out more details on this error?

    System.Runtime.InteropServices.COMException: The user does not exist or is not unique.

    Thanks.

    Monday, May 25, 2015 11:02 AM
  • Hi,

    As you said, this issue occurs for some certain users, I suggest you can check the certain users in the Active Directory to see if they are valid.

    And in your code, you can add the judgement logic like below to avoid this exception:

    if(web.EnsureUser(claimLoginName)!=null)
    {
      //Add user action
    }        

    Thanks

    Best Regards


    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.

    Monday, May 25, 2015 11:13 AM
  • Hi Jerry,

    Yes, the user account is active in AD. It can access the top-level site.

    Is there any other way to find out more details on this error? Like in the Windows Event Viewer, etc.

    System.Runtime.InteropServices.COMException: The user does not exist or is not unique.

    THanks.

    Tuesday, May 26, 2015 2:33 AM