0

I have the following event receiver inside my sharepoint server 2013, the event recevier get fired when an item is updated and i am running it with Elevated Privileges :-

public override void ItemUpdated(SPItemEventProperties properties)
        {

            base.ItemUpdated(properties);
            SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(properties.SiteId))
                    {
                        string currenweburl = properties.RelativeWebUrl;
                        using (SPWeb spCurrentSite = site.OpenWeb(currenweburl))
                        { 

now i was reading this MSDN question link, which mentioned that for the After event receiver (as in the ItemUpdated case) i can not get the user who cause the event receiver to fire, here is one of the answers :-

'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.

but in my case i am able to get the ID of the user who cause the event receiver to fire as follow:-

var currentusername = properties.CurrentUserId;

Also i am able to get the loginName as follow:-

var test = properties.Web.CurrentUser.LoginName.ToString();

Question 1) so can anyone advice why inside the MSDN link they mentioned that on the After event receivers i can not get the user who cause the event receiver to fire,, but based on my test i am able to do so !!

now one note i have is that the MSDN link is back to 2010,, which means that it is not talking about sharepoint 2013, where in my case i am using sharepoint 2013..

Final note here is my Element.xml file inside my event receiver project:-

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers>
<Receiver>
<Name>EventReceiver1ItemUpdated</Name>
<Type>ItemUpdated</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>CreateSubSiteforProjects.EventReceiver1.EventReceiver1</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
</Receivers>
</Elements>

which does not explicitly define the <Synchronization>. Question 2) so i am not sure what is the defualt? could my event receiver execute synchronous ?

can anyone advice on my above 2 questions please?

  • John...like i was telling you on your other post. For you to get the correct user that fired the event, you have to set this synchronous. Ref: sharepoint.stackexchange.com/questions/5313/… – jpollar Jan 15 '17 at 2:21
  • @jpollar but as i mentioned also that my event receiver is having all the synchronous behaviors .. for example i am able to get the current user ID and username... also the event receiver is running under the current user account and not under the "system admin" account !! so it have the sync properties and not the async properties is this correct ? – john Gu Jan 15 '17 at 2:58
2
+150

Question 1)

Inside the MSDN link you attached, someone mentioned that on the After event receivers you cannot get the user who cause the event receiver to fire. That's incorrect, you can access the user info using properties.CurrentUserId or properties.Web.CurrentUser (The post you mentioned was marked and unmarked as correct answer...).

Also, you must consider that:

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!

Question 2)

The default for After events is to run asynchronously. If you want it to execute synchronous you have to add the following statement to the Elements.xml file: <Synchronization>Synchronous</Synchronization>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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