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

Drake
  • 435
  • 2
  • 10
  • 19
  • SPContext will not be available in EventReceiver, hence your Current is returning null.. instead use Properties – Gaurravs Mar 28 '16 at 08:51

5 Answers5

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.

Alexander Ulmaskulov
  • 706
  • 1
  • 10
  • 24
2

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

James Love
  • 25,512
  • 2
  • 45
  • 77
  • 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 07 '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 07 '10 at 10:51
  • 1
    Have you also tried properties.CurrentUserId? – James Love Sep 07 '10 at 11:06
  • Yes, it worksm thank you! Do you also know how to get SPUser from a CurrentUserId? – Drake Sep 07 '10 at 12:19
  • 1
    No worries - SPWeb.Users.GetByID() I believe. – James Love Sep 07 '10 at 12:21
  • This answer is misleading - it suggests that when Receiver is set to Synchronous then SPContext.Current != null which is not true. properties.CurrentUserId is also available when Receiver is not set to Synchronous but in either case it doesn't return SharePoint user ID but some internal number. – Mariusz Ignatowicz Aug 08 '14 at 14:28
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

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/

Amit Kumawat
  • 6,689
  • 2
  • 22
  • 33
0

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

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
Jeroen Ritmeijer
  • 4,897
  • 1
  • 23
  • 33
  • 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 07 '10 at 12:14
  • how to get the current logged in user in the itemadded eventreceiver event ? – samolpp2 Mar 15 '16 at 06:51