none
why properties.CurrentUserId is returning “1” inside my event reciever RRS feed

  • Question

  • I have the following inside my event receiver which get fired when an item is updated:-

     public override void ItemUpdated(SPItemEventProperties properties)
            {
              base.ItemUpdated(properties);
              using (SPSite site = new SPSite(properties.SiteId))
    
                  {
    
                    string currenweburl = properties.RelativeWebUrl;
    
                    using (SPWeb spCurrentSite = site.OpenWeb(currenweburl))
                       {
                         var currentuserid = properties.CurrentUserId;
                         site.RootWeb.SiteGroups.Add(curItemSiteName, site.RootWeb.SiteUsers[currentuserid], site.RootWeb.SiteUsers[currentuserid], "Your Group Description"); 

    currently i was trying to add the user who updated an item to be the new Group owner and being added as a member. but the problem i am getting is that the currentuserid will be equal to 1, and the result is that the member which get added is not ME (the user who updated the item!!). so can anyone adivce on this please? why the properties.CurrentUserId does not return the current user id ?

    Thanks

    Friday, January 13, 2017 4:59 PM

Answers

  • Hi John,

    Yes, SharePoint 2013 event receiver supported to get user information in After evnet receiver.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by johnjohn11 Wednesday, January 18, 2017 12:31 PM
    Wednesday, January 18, 2017 6:41 AM

All replies

  • Well CurrentUserId does what it supposed to do i.e. returns the integer ID of the user whose action caused the event to occur.

    Get the SPUser object you can simply use spCurrentSite.Users.GetById(properties.CurrentUserId).




    Amit

    Friday, January 13, 2017 5:38 PM
  • Well CurrentUserId does what it supposed to do i.e. returns the integer ID of the user whose action caused the event to occur.

    Get the SPUser object you can simply use spCurrentSite.Users.GetById(properties.CurrentUserId).




    Amit

    ok you are 100% correct..

    First Question. But i find another appraoch to get the username who caused the event receiver to fire... So instead of having to deal with IDs, i find that i can directly get the current user name as follow:-

    var currentusername = properties.Web.CurrentUser.LoginName.ToString();

    where seems this will get the username who executed the event receiver... so can i proceed with my above code ? or you recommend to use the userID ??

    second question. now when i use :-

    properties.CurrentUserId

    what is this ID exactly ? is it a unique ID per user inside the web,site collection and the whole farm? and could users' IDs get changed , or users will have the same Ids even on 2 different site collections ?

    Third Question. on some links and documentation over the web they mentioned that for the After event receivers (such as the ItemUpdated in my case) i do not have access to the current user .. but based on my test and your reply the current user ID can be retrieved. here are some of the links which mentioed that the current user will not be avilable on the After event recievers:-

    https://social.msdn.microsoft.com/Forums/sharepoint/en-US/53c64bb1-6810-48ac-910a-9faa16c8e3b9/current-user-through-an-event-receiver?forum=sharepointdevelopmentlegacy

    http://www.careerride.com/moss-asynchronous-and-synchronous-event-receivers.aspx

    here is my element.xml inside my current event receiver:-

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Receivers>
    <Receiver>
    <Name>EventReceiver1ItemUpdated</Name>
    <Type>ItemUpdated</Type>
    <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
    <Class>CreateSubSiteforProjects.EventReceiver1.EventReceiver1</Class>
    <SequenceNumber>10000</SequenceNumber>
    </Receiver>
    </Receivers>
    </Elements>
    

    can you please advice on my above 2 questions?

    Thanks in advance.



    • Edited by johnjohn11 Sunday, January 15, 2017 1:52 AM
    Saturday, January 14, 2017 3:14 AM
  • Hi John,

    As I tested in my environment, there are two user accounts in the specific site collection, system administrator and my account Jerry, the user id for each one is 1 and 19.

    When I logged in as "Jerry", the user id in event receiver will be 19 and when logged in as system administrator, it will be 1.

    So I understand, the user id in the event receiver will be the current logged in user .

    Logged in as Jerry:

    And as tested, we can still get user information in after event receiver.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, January 16, 2017 8:49 AM
  • Hi John,

    As I tested in my environment, there are two user accounts in the specific site collection, system administrator and my account Jerry, the user id for each one is 1 and 19.

    When I logged in as "Jerry", the user id in event receiver will be 19 and when logged in as system administrator, it will be 1.

    So I understand, the user id in the event receiver will be the current logged in user .

    Logged in as Jerry:

    And as tested, we can still get user information in after event receiver.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    ok thanks for the reply and the test. but can you please adivce on my above Third Question ?

    Thanks

    Monday, January 16, 2017 12:04 PM
  • I could be wrong but I saw some threads mentioning that all event receivers in SP 2013 runs under W3WP.exe and hence you are able to get the current user ID. I have not validated this but you may want to check this out yourself.

    Amit


    • Edited by Amit V Monday, January 16, 2017 4:44 PM
    Monday, January 16, 2017 4:43 PM
  • I could be wrong but I saw some threads mentioning that all event receivers in SP 2013 runs under W3WP.exe and hence you are able to get the current user ID. I have not validated this but you may want to check this out yourself.

    Amit


    seems this is a confusing thing.. now i am thinking of just adding this inside my element.xml for the ItemUpdated event receiver:-

    <Synchronization>Synchronous</Synchronization>

    so in this case i can force my event receiver to run in Synchronous mode what do you think ?


    second question . Just a quick question.. now when you say (although you did not confirm this) that in 2013 all event receivers run under W3WP.exe .. so when you say under W£WP.exe this mean it runs under IIS worker process so it runs under the user identity ?
    • Edited by johnjohn11 Monday, January 16, 2017 6:41 PM
    Monday, January 16, 2017 6:28 PM
  • Hi John,

    In ItemUpdated event receiver, as tested we can still get the current user information. And about the synchronous question of event receiver, I will reply in another thread later.

    https://social.msdn.microsoft.com/Forums/office/en-US/24bb1c27-1f08-438d-8047-c2e0edcc0052/will-my-itemupdated-event-receiver-run-in-synchronous-or-in-asynchronous-mode-if-i-do-not-define-a?forum=sharepointdevelopment

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, January 17, 2017 5:37 AM
  • Hi John,

    In ItemUpdated event receiver, as tested we can still get the current user information. And about the synchronous question of event receiver, I will reply in another thread later.

    https://social.msdn.microsoft.com/Forums/office/en-US/24bb1c27-1f08-438d-8047-c2e0edcc0052/will-my-itemupdated-event-receiver-run-in-synchronous-or-in-asynchronous-mode-if-i-do-not-define-a?forum=sharepointdevelopment

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    seems on older versions compared to 2013 ,, retrieving the user ID and Username inside a Async Event Receivers was not available .. but starting from SP 2013 then both Sync and Async event receivers provide the user Id and username .. is this correct?
    Tuesday, January 17, 2017 11:49 AM
  • Hi John,

    Yes, SharePoint 2013 event receiver supported to get user information in After evnet receiver.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by johnjohn11 Wednesday, January 18, 2017 12:31 PM
    Wednesday, January 18, 2017 6:41 AM