SPWeb.EnsureUser Method
Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Requires that the current user is a Site Admin and that the authentication method is AD
Here's the reflected code for the method:
publicSPUserEnsureUser(string loginName) { if ((this.CurrentUser == null) || !this.CurrentUser.IsSiteAdmin) { SPGlobal.HandleUnauthorizedAccessException(newUnauthorizedAccessException()); } boolisADAccountCreationMode = this.IsADAccountCreationMode; SPPrincipalInfoinfo = SPUtility.ResolvePrincipalInternal(this, null, null, loginName, SPPrincipalType.SecurityGroup | SPPrincipalType.User, SPPrincipalSource.All, null, false, true); if (info == null) { thrownewSPException(SPResource.GetString("UserCouldNotBeFound", newobject[] { loginName })); } if (info.PrincipalId < 0) { this.SiteUsers.Add(info.LoginName, info.Email, info.DisplayName, string.Empty); } returnthis.SiteUsers[info.LoginName]; }
- 12/17/2008
- austegard