We tried several options to update created by column of a list item with a value using event handler
We found the way to do it .... Its below
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
ChangeCreatedBy(properties);
}
private void ChangeCreatedBy(SPItemEventProperties properties)
{
string strAuthor = @"domain\first.last";
try
{
Guid siteID = properties.ListItem.ParentList.ParentWeb.Site.ID;
Guid webID = properties.ListItem.ParentList.ParentWeb.ID;
Guid listID = properties.ListItem.ParentList.ID;
Guid newItemID = properties.ListItem.UniqueId;
SPSecurity.RunWithElevatedPrivileges(delegate() {
using (SPSite site = new SPSite(siteID))
{
using (SPWeb web = site.OpenWeb(webID))
{
SPUser usrAuthor = web.EnsureUser(strAuthor);
SPList questionList = web.Lists[listID];
SPListItem listItem = questionList.Items[newItemID];
listItem["Author"] = usrAuthor ;
listItem.SystemUpdate(false);
}
}
});
}
catch (Exception ex)
{
//call log error routine
}
}
No comments:
Post a Comment