What are the differences btw “properties.Web.CurrentUser;” & “web.Users.GetByID(properties.CurrentUserId);” inside my Event Receiver
-
09-12-2020 - |
Question
I added the following Event Receiver inside my sharepoint server 2013:- 1. Using VS >> add a new Event Receiver >> Type "List Item Events" >> when "item was created". 2. the idea of the event receiver is that i want to check if the user who created a list item , is within a specific user group or not? and set a site column named "Team" accordingly. 3. so i tried the following :-
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = properties.OpenWeb())
{
try
{
web.AllowUnsafeUpdates = true;
SPListItem currentItem = properties.ListItem;
SPUser user = //properties.Web.CurrentUser;
web.Users.GetByID(properties.CurrentUserId);
SPGroup group = web.SiteGroups["CustomerA"];
foreach (var user2 in group.Users)
{
if (user2.ToString() == user.ToString())
{
currentItem["Team"] = "IT";
currentItem.Update();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
- then i deploy the event receiver.
but the above did not wok as the site column value will not be updated. and when i debug the event receiver inside VS i got the following error "User can not be found" on web.Users.GetByID(properties.CurrentUserId);
.
so i replace the web.Users.GetByID(properties.CurrentUserId);
with this SPUser user = properties.Web.CurrentUser;
and it worked well.
Q1) so can anyone adivce why i got an error on the first code (using "Web.Users.GetByID(properties.CurrentUserId);
")?? while i did not get any error when using properties.Web.CurrentUser;
??are they the same ?
q2) second question. now i want to get the user who created the item? so which method i should use properties.Web.CurrentUser;
or web.Users.GetByID(properties.CurrentUserId);
??
Solution
You got "User can not be found"
because the current user who adds the item does not have explicitly permission at the current list that means the current user not assigned to this list directly.
So in this case , you should use web.SiteUsers
or properties.Web.CurrentUser
instead of web.users
Also, you should be aware of the difference between
web.SiteUsers
means get the collection of all users who belong to the site collection.web.Users
means get the collection of users who explicitly assigned permission in the current web.
are the Web.Users.GetByID(properties.CurrentUserId);
and properties.Web.CurrentUser;
the same ?
Yes, the two statement should return the current user who adds the item . but the first one failed because the current user is not explicitly assigned permission in the current web.
web.Users
output :
web.SiteUsers
output :
properties.Web.CurrentUser
output :
now i want to get the user who created the item? so which method i should use properties.Web.CurrentUser; or web.Users.GetByID(properties.CurrentUserId); ??
the following statements have the same functionality , that it returns the current user who Add the item
properties.Web.CurrentUser
= web.Users.GetByID(properties.CurrentUserId);
= web.SiteUsers.GetByID(properties.CurrentUserId);
but to avoid any error related to explicit permission , you should use
web.SiteUsers.GetByID(properties.CurrentUserId);
or properties.Web.CurrentUser
So the final code should look like
public override void ItemAdded(SPItemEventProperties properties)
{
//base.ItemAdded(properties);
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = properties.OpenWeb())
{
try
{
web.AllowUnsafeUpdates = true;
SPUser user = web.SiteUsers.GetByID(properties.CurrentUserId);
SPGroup group = web.SiteGroups["EPM Sys Owner"];
foreach (var user2 in group.Users)
{
if (user2.ToString() == user.ToString())
{
properties.ListItem["Level"] = "IT";
properties.ListItem.Update();
break;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}