none
Current user through an Event Receiver RRS feed

  • Question

  • I created an Event Receiver for a list and I am trying to grasp the logged on user (not System Account) but when I attach to the w3wp process, the current user id shows as the system accounts id. I need to grasp the logged on user the retrieve values from the "User Information List".

                int _currentUser = properties.CurrentUserId;

     

    I am not passing this through the "RunwithelevatedPrivelages" method. Thank you.


    Victor Palma
    Wednesday, March 31, 2010 6:05 PM

All replies

  • Whether you can do this depends on the event.

    'Before' events such as Adding, Updating, deleting etc run synchronously and so you should be able to get the user pretty easily.

    'After' event such as Added, Updated, Deleted are run asynchronously under the system account, and so you won't have access to the user that triggered the event.

    You could always use the value in the item's Last Modified By field though to get a reference to that user.

    Wednesday, March 31, 2010 6:53 PM
  • Victor,

    there's a common pitfall:

    Never mind how you get the current user (event properties, SPContext.Current or HttpContext are possible) you cannnot be sure that the event was triggered by a user action.

    It could also be a workflow or another event receiver who has triggered the event!

     

     

    regards,

    Markus

    Thursday, April 1, 2010 10:12 AM
  • Okay so how would I go about authenticating or triggering some logic based on a user that the just added a list item?
    Victor Palma
    Thursday, April 1, 2010 2:58 PM
  • SPListItem["Author"] should give you the user.
    Thursday, April 1, 2010 3:11 PM
  • Look at Paul's suggestion.  Every SPListItem object has a CreatedBy or LastModifiedBy field, so on the ItemAdded event you can get the SPListItem object that fired that event by the properties parameter and then grab the createdby or lastmodifiedby property of that SPListItem.
    • Proposed as answer by .Net Frenzy Thursday, April 1, 2010 10:10 PM
    • Unproposed as answer by Mike Walsh FIN Wednesday, April 14, 2010 3:34 AM
    Thursday, April 1, 2010 3:14 PM
  • returning null.

     

          public override void ItemAdded(SPItemEventProperties properties)
            {
                string author = SPListItem["Author"]
    }


    Victor Palma
    Tuesday, April 13, 2010 4:40 PM
  • Victor,

    How are you getting the instance of your SPListItem? What sort of list is it? The following works for me for a standard custom list.

    public override void ItemAdded(SPItemEventProperties properties)
    {
        SPListItem item = properties.ListItem;
        string author = item["Author"].ToString();
    }
    

     

    Paul.

     

     

    Tuesday, April 13, 2010 11:31 PM
  • Paul,

    I am stepping through the code, and it reads "System Account" still based on your example. I think I should elaborate more on the way I am generating this list item.

     

    1) I have two lists. Based on a flag through an Event receiver and the first flag, then I generate a new item on the second list if the criteria has been met through that the Object Model on that Event Receiver which is the reason I think I am getting that System Account author.

     

    What I dont understand is that when I open up the list item that has been created on my second list on MOSS, the author is set to the individual whom created the item on the first list and not the System Account.


    Victor Palma
    Tuesday, April 20, 2010 4:59 PM
  • Hi Victor,

    Maybe you could try posting the code and I'll try to reproduce the issue.

    Paul.

    Tuesday, April 20, 2010 5:47 PM
  • public override void ItemAdded(SPItemEventProperties properties)
    {
     SPListItem item = properties.ListItem;
     SPField createdByField = item["Created By"];
     string createdBy = createdByField.GetfieldValueAsText(item[createdByField.Id]);
    }

    This should get you what you need.

    If not, then you need to look at the field XML in the SPListItem while debugging to find the field you need.  Replace "Created By" with the field Display value to get the field in the list.

    Thursday, April 22, 2010 11:34 AM
  • Still:

    10232823; #System Account

    coming up when steppin through on:

     

    SPField createdByField = item["Created By"];

    That item is being created within another event receiver that is set to ItemAdded on another list.

     SPSecurity.RunWithElevatedPrivileges(delegate()
              {
                using (SPSite site = new SPSite(properties.WebUrl))
                {
                  using (SPWeb web = site.OpenWeb())
                  {
                    SPListItemCollection accidentList = web.Lists[Lists.OpenWorkOrders].Items;
                    SPListItem item = accidentList.Add();
                    item[BFields.WorkOrdersTitle] = "Open Work Order: " + DateTime.Now.ToLongTimeString() + ", Incident ID: " + iIncidentID;
                    item[BFields.WorkOrdersIncidentID] = iIncidentID.ToString();
                    item[BFields.WorkOrdersDsc] = strWorkOrderDsc;
    
                    try
                    {
                      item.Update();
    
    
                    }
                    catch (SPException ex)
                    {
                      SPUtility.TransferToErrorPage(ex.Message);
                    }
                  }
                }
              });


    Victor Palma
    Tuesday, April 27, 2010 8:47 PM
  • As you run your code inside a block with elevated priveleges and get your web references there from within you will create the new list item as system user!

     

    If the one who creates the first item has also write permisisons for the second list (where the automatically generated item will be put into) you won't need a block with elevated priveleges, skip it and you will create the item within the current user#s context.

     

    regards,

    Markus

    Wednesday, April 28, 2010 7:22 AM
  • You can get the Current User by doing SPContext.Current.Web.CurrentUser or something like

    using (SPSite site = new SPSite("http://yoursite/"))

    {

        using (SPWeb web = site.OpenWeb())

        {

             SPUser userCurrent = web.CurrentUser;

        }

    }

     

    do this before you do the RunWithElevatedPrivileges() and you can capture who is current running an action.

    Wednesday, April 28, 2010 12:17 PM
  • Hi,

    I Agree with Markus.But if you Current User has not the permission on the Second list then

     int _currentUser = properties.CurrentUserId;

    SPUser  spCurrentUser=web.Alluser[currentUser];

    SPSecurity.RunWithElevatedPrivileges(delegate()
              {
                using (SPSite site = new SPSite(properties.WebUrl))
                {
                  using (SPWeb web = site.OpenWeb())
                  {
                    SPListItemCollection accidentList = web.Lists[Lists.OpenWorkOrders].Items;
                    SPListItem item = accidentList.Add();
                    item[BFields.WorkOrdersTitle] = "Open Work Order: " + DateTime.Now.ToLongTimeString() + ", Incident ID: " + iIncidentID;
                    item[BFields.WorkOrdersIncidentID] = iIncidentID.ToString();
                    item[BFields.WorkOrdersDsc] = strWorkOrderDsc;
                   item["Author"] =spCurrentUser;
                    try
                    {
                      item.Update();


                    }
                    catch (SPException ex)
                    {
                      SPUtility.TransferToErrorPage(ex.Message);
                    }
                  }
                }
              });

     

    Hope this will solve your problem.

     


    Thanks Shrivallabh Kulkarni
    Wednesday, April 28, 2010 1:39 PM