I was searching for a method which returns SPUser
object based on the display name of the user, and I found the EnsureUser()
method. as I want to use this method inside my SharePoint server 2013 event receiver, as follow:-
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPUser spUser = properties.Site.RootWeb.EnsureUser(approver);//You can get SPUser from here
currentItem["ProjectApprover"] = spUser;
currentItem.SystemUpdate();
now based on MSDN documentation the method will not just return SPUser
from a user display name but will also do the following:-
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.
so can anyone advice how EnsureUser()
actually works? I have the following 4 points which I can not understand:-
I thought that
EnsureUser()
will check if the user has any permission on the site, and if so it will return the relatedSPUser
. but as the documentation mentioned that the method will add the user to the website if it can not validate it? so where the user will be added (under which group)?now let say I pass a test string to the
EnsureUser()
method as follow
SPUser spUser = properties.Site.RootWeb.EnsureUser("ONLY For Testing");//You can get SPUser from here
so, in this case, the method will not find any user named "ONLY For Testing", so will it add the "ONLY For Testing" user to the site?
inside the official documentation it mentioned that the method will check "..whether the specified logon name belongs to a valid user of the website..", so what does belong exactly means?. let say we have a user named
john.smith
inside our active directory, and this user does not have any permission to access theproperties.Site.RootWeb
, so will this method:-SPUser spUser = properties.Site.RootWeb.EnsureUser("john.smith");//You can get SPUser from here
return that the user is valid ? or the user should have at least read permission on the properties.Site.RootWeb
to be able to pass the EnsureUser()
validation?
- now since I am using this method inside an event receiver.. so should the user who run the event receiver have any special permission to be able to run the
EnsureUser
method on theproperties.Site.RootWeb
?, I mean should he/she be able to access theproperties.Site.RootWeb
permission section (in other words to be a site admin??