3
我想在ActiveDirectory中添加用戶。錯誤:當在AD中添加用戶時出現「錯誤:用戶不存在或不唯一」
我使用此代碼
private SPUser CreateUser(string strLoginName, string strEMail,
string strName, string strNotes, string strSiteURL)
{
SPUser spReturn = null;
SPSite spSite = null;
SPWeb spWeb = null;
try
{
//Open the SharePoint site
spSite = new SPSite(strSiteURL);
spWeb = spSite.OpenWeb();
//Assign role and add user to site
SPRoleAssignment spRoleAssignment =
new SPRoleAssignment(strLoginName, strEMail, strName, strNotes);
//Using Contribute, might need high access
SPRoleDefinition spSPRoleDefinition =
spWeb.RoleDefinitions["Contribute"];
spRoleAssignment.RoleDefinitionBindings.Add(spSPRoleDefinition);
spWeb.RoleAssignments.Add(spRoleAssignment);
//Update site
spWeb.Update();
spReturn = spWeb.AllUsers[strLoginName];
}
catch(Exception)
{
}
finally
{
spWeb.Close();
spSite.Close();
}
return spReturn;
}
時spWeb.RoleAssignments.Add(spRoleAssignment); 錯誤:「錯誤:用戶不存在或不是唯一的」
編輯
以下爲我工作:
SPUser user = spWeb.EnsureUser(strLoginName);
SPRoleAssignment spRoleAssignment =
new SPRoleAssignment(user);