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

  • SPContext will not be available in EventReceiver, hence your Current is returning null.. instead use Properties – Gaurravs Mar 28 '16 at 8:51
up vote 2 down vote accepted

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

  • 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 Sep 7 '10 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 Sep 7 '10 at 10:51
  • 1
    Have you also tried properties.CurrentUserId? – James Love Sep 7 '10 at 11:06
  • Yes, it worksm thank you! Do you also know how to get SPUser from a CurrentUserId? – Drake Sep 7 '10 at 12:19
  • 1
    No worries - SPWeb.Users.GetByID() I believe. – James Love Sep 7 '10 at 12:21

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.

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

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

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
  • 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 Sep 7 '10 at 12:14
  • how to get the current logged in user in the itemadded eventreceiver event ? – SaMolPP Mar 15 '16 at 6:51

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

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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