Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Error "The user does not exist or is not unique" has been occurred When add a user programmatically to a User Group in SharePoint 2013

$
0
0

I can able to add a user to a group using sharepoint user permission page but same does not work with programmatically.

string loginName = "i:0#.w|domain\id";
SPSecurity.RunWithElevatedPrivileges(delegate()
                   {                        
                        using (SPSite site = new SPSite(siteUrl))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                try
                                {                                    
                                    web.AllowUnsafeUpdates = true;                                   
                                    SPUser spUser = web.EnsureUser(loginName);
                                    if (spUser != null)
                                    {                                       
                                        SPGroup spGroup = web.Groups[ddlGroup.SelectedValue];
                                        if (spGroup != null)
                                        {                                           
spGroup.AddUser(spUser);// Error occurred when executing this line 
                                        }
                                    }                                    
                                }
                                catch (Exception ex)
                                {
                                    //Error handling logic here                                   
                                }
                                finally
                                {
                                    web.AllowUnsafeUpdates = false;
                                    web.Close();
                                }
                            }
                        }
                    });

Error msg : The user does not exist or is not unique.

Kindly help me to resolve this issue.

Thanks in advance

Saravanan

                                           

Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>