How To Set Modified By User Back To The Original User When Impersonating A User In An Event Receiver



TODO:

You impersonate a user in an event receiver, and the result of calling Update() on the list item means that the "Modified By" user is that of the impersonated account, and not the original user that was logged into the site.  The solution is pretty simple, just use the code below to set that user back to the original user.

 

SOLUTION:

private void ApplyPermissions(SPItemEventProperties properties)
{
    string debugMessage = string.Empty;
    Guid webId = properties.ListItem.Web.ID;
    Guid siteId = properties.SiteId;
    SPUserToken sysToken = null;
    SPUser currentUser = null;

    try
    {
        currentUser = properties.Web.CurrentUser;

        //open site, and get elevated user
        using (SPSite site = new SPSite(siteId))
        {
            //use our site collection
            using (SPWeb myWeb = site.OpenWeb(webId))
            {
                sysToken = myWeb.AllUsers["i:0#.w|mydomain\\impusr"].UserToken;
            }
        }

        bool allowUnsafeUpdates = false;

        //open site with elevated user
        using (SPSite site = new SPSite(properties.SiteId, sysToken))
        {
            //get the web
            using (SPWeb myWeb = site.OpenWeb(properties.ListItem.Web.ID))
            {
                try
                {
                    allowUnsafeUpdates = myWeb.AllowUnsafeUpdates;  //save unsafe update setting
                    myWeb.AllowUnsafeUpdates = true;  //allow unsafe updates for now

                    SPListItem elevatedListItem = null;
                    elevatedListItem = myWeb.Lists[properties.ListId].Items[properties.ListItem.UniqueId];  //get elevated list item
                            
                    //DO ELEVATED ACTIONS HERE 

                    base.EventFiringEnabled = false;
                    elevatedListItem["Editor"] = currentUser; //set current user to editor and author  
                    elevatedListItem["Author"] = currentUser; 
                    elevatedListItem.Update(); //update the item                 
                    base.EventFiringEnabled = true;
                }
                catch (Exception x)
                {
                    throw x;
                }
                finally
                {
                    myWeb.AllowUnsafeUpdates = allowUnsafeUpdates;  //put back original setting    
                }
            }//using web   
        }//using site
    }
    catch (Exception x)
    {
        properties.ErrorMessage = x.ToString();
    }
}

 

 

NOTES:

This code ran in an ItemAdded and ItemUpdated Event Receiver

How To Get The Value Of A Lookup Field Column In SharePoint



TODO:

Have you ever had a lookup column, and you want to get its value programatically using C#?

1.  I have a column that is called, Task Id.  

2.  "Task Id" in #1, is a lookup to the "ID" column of a list called "Tasks"

3.  I have an event receiver that needs to get the "Task Id" from the form.

 

SOLUTION:

 

//my event receiver code....
public override void ItemAdded(SPItemEventProperties properties)
{
     SPFieldLookup lookupField = properties.ListItem.Fields["Task ID"] as SPFieldLookup;
     SPFieldLookupValue taskIdVal = null;

     object obj = properties.ListItem["Task ID"];
     if (lookupField != null)
          taskIdVal = lookupField.GetFieldValue(obj.ToString()) as SPFieldLookupValue;

     string taskId = taskIdVal.LookupValue;
}

 

 

NOTES:

There are no notes on this topic.

How To Fix The NativeStack Error In SharePoint 2010 When Deploying An Event Receiver



TODO:

Have you ever had the dreaded "Error occurred in deployment step 'Activate Features': <nativehr>0x80070002</nativehr><nativestack></nativestack>" error when deploying an Event Receiver that targets a specific list?

 

SOLUTION:

In my case, this was caused by an invalid 'ListURL' setting in my elements.xml file.  Be sure the 'Site URL' property of your project points to the site that the List resides.  If this is pointed to the level above, the list will not be found, and you will get the error above.  ListURL needs to be 'Lists/'{your list name}  ex. List/Customers, thus you see why it is important for the Site URL to be set properly.

 

NOTES:

There are no notes on this topic.