Friday, February 8, 2013

SharePoint 2010 SPContext.Current.Web.EnsureUser(LogonName); issue.

Microsoft's documentation says "SPContext.Current.Web.EnsureUser(LogonName); Checks whether the specified logon name belongs to a valid user of the website, and if the logon name does not already exist, adds it to the website.". But when you check same user with following code:
SPUser user = SPContext.Current. Web.Users.GetByID(userID);
user not found exception occurs if the user is never logged in to the site.

But you can find same user information in SPContext.Current.Web.SiteUserInfoList  using following code:

SPListItem user = SPContext.Current.Web.SiteUserInfoList.GetItemById(userID);
string name = user.Name;

No comments:

Post a Comment