Unable to programatically add a user to a SharePoint Group

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 1 hour 0 minutes ago
May 21st, 2015 5:54am

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

Free Windows Admin Tool Kit Click here and download it now
May 21st, 2015 10:58pm

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.

May 22nd, 2015 2:45am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics