SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
    
SPContext will not be available in EventReceiver, hence your Current is returning null.. instead use Properties – Gaurravs Mar 28 at 8:51
up vote 3 down vote accepted

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

share|improve this answer
    
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.

share|improve this answer

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();
  }
}
share|improve this answer

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

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
share|improve this answer
    
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 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/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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