4

I need to get the information about the user actually logged (as SPUser) inside an event receivers.

The following line:

Dim currentUser As SPUser = SPContext.Current.Web.CurrentUser

does not work because Current is set to Nothing (null).

I read around that this is due to the asynchronous nature of event receivers but then, how to get the user information I need?

EDIT: I am using ItemCheckingIn receiver to try block a user to check in a file checked out by another user.

Thank you in advance for any help

1
  • SPContext will not be available in EventReceiver, hence your Current is returning null.. instead use Properties
    – Gaurravs
    Commented Mar 28, 2016 at 8:51

5 Answers 5

2

Use the synchronous method in your event receiver, and you should be able to retrieve the current user from the context.

6
  • Thank you for the fast answer. I edited my question to give more details. I am not sure I can use a synchronous event receiver to implement this logic, or yes?
    – Drake
    Commented Sep 7, 2010 at 10:27
  • Synchronous is the only way to go if you want to stop an operation, as you are doing. Strange however, as CurrentContext should be available at this stage...
    – James Love
    Commented Sep 7, 2010 at 10:51
  • 1
    Have you also tried properties.CurrentUserId?
    – James Love
    Commented Sep 7, 2010 at 11:06
  • Yes, it worksm thank you! Do you also know how to get SPUser from a CurrentUserId?
    – Drake
    Commented Sep 7, 2010 at 12:19
  • 1
    No worries - SPWeb.Users.GetByID() I believe.
    – James Love
    Commented Sep 7, 2010 at 12:21
2

Use this:

public override void ItemCheckingIn(SPItemEventProperties properties)
{
    base.ItemCheckingIn(properties);
    var currentUser = properties.Web.CurrentUser;
}

As you can see, properties object contain SPWeb, that have CurrentUser property.

1

You can use this code.

using (SPSite originalsite = new SPSite(properties.SiteId, properties.OriginatingUserToken))
{
  //To get the current user
  using (SPWeb web = originalsite.OpenWeb())
  {
    string strUserName = web.CurrentUser.LoginName.ToString();
  }
}
0

Not 100% sure what the behaviour is with async event receivers, but did you have a look at

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
2
  • This code return me an exception: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
    – Drake
    Commented Sep 7, 2010 at 12:14
  • how to get the current logged in user in the itemadded eventreceiver event ?
    – samolpp2
    Commented Mar 15, 2016 at 6:51
0

With 2010, the event property bag contains the OriginatingUserToken ,UserDisplayName , and UserLoginName, which you can use to get the original user.

Check the section 5. Impersonation enhancements here : http://extreme-sharepoint.com/2011/12/27/event-receivers-sharepoint-2010/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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