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