I've got an EventReceiver
in my SharePoint application where I'm overriding ItemAdding
and ItemUpdating
methods:
public override void ItemAdding(SPItemEventProperties properties)
{
SPWeb web = properties.OpenWeb();
switch (properties.ListTitle)
{
//some cases
case "Employees":
if (properties.AfterProperties["User"] != null)
{
SPUser user = web.SiteUsers
[
properties.AfterProperties["User"].ToString().Substring
(
properties.AfterProperties["User"].ToString().IndexOf(";#") + 2
)
];
properties.AfterProperties["Title"] = user.Name;
}
break;
}
base.ItemAdding(properties);
}
Code in both methods is exactly the same. But sometimes Title field remains empty after adding an item to list. And if I update that item (without any changes) Title field is filled by username correctly.
I see two potential issues in your code: