1

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;
                    }
                }
            }  
        }
  1. 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); ??

1

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 :

enter image description here

web.SiteUsers output :

enter image description here

properties.Web.CurrentUser output :

enter image description here

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;
            }
        }
    }

}
  • thanks a lot for your great reply. but can you please explain what do you mean by explicit permission on the list ? now i have a site collection , which contain 11 sub sites , and each sub-site have 4 lists. and i am managing the permission at the site collection level , in other words when i created the subsite i chose to inherit the permission from their site collection.. so this mean my subsites does not have explicit permissions ? – john Gu Oct 3 '16 at 17:40
  • 1
    yes you are right that what it mean , if you break inheritance and set explicitly permission for the user to a list or web directly , so the user have explicitly permission – Mohamed El-Qassas MVP Oct 3 '16 at 17:43
  • so in my case since i am inheriting the permission of the subsites from its site collection, then the web.Users.GetByID(properties.CurrentUserId); will always be empty ? – john Gu Oct 3 '16 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.