Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to update SPListItem Created/Modified date
using System;
using Microsoft.SharePoint;
namespace EPAM.USPS.SharePoint.Core.Utilities.Specific
{
public class ListItemPropertySet
{
public ListItemPropertySet()
{
}
public ListItemPropertySet(SPListItem item)
{
Validation.ArgumentNotNull(item, "item");
var users = item.ParentList.ParentWeb.SiteUsers;
Created = (DateTime)item[CreatedFieldName];
CreatedBy = users.GetByID(Convert.ToInt32(item[CreatedByFieldName].ToString().Split(';')[0]));
Modified = (DateTime)item[ModifiedFieldName];
ModifiedBy = users.GetByID(Convert.ToInt32(item[ModifiedByFieldName].ToString().Split(';')[0]));
}
public void Apply(SPListItem item)
{
Validation.ArgumentNotNull(item, "item");
item[CreatedFieldName] = Created;
item[ModifiedFieldName] = Modified;
item[CreatedByFieldName] = GetStringByUser(CreatedBy);
item[ModifiedByFieldName] = GetStringByUser(ModifiedBy);
item.Update();
}
public ListItemPropertySet Clone()
{
return MemberwiseClone() as ListItemPropertySet;
}
public DateTime Created { get; set; }
public SPUser CreatedBy { get; set; }
public DateTime Modified { get; set; }
public SPUser ModifiedBy { get; set; }
public long Revision { get; set; }
private const string CreatedFieldName = "Created";
private const string CreatedByFieldName = "Author";
private const string ModifiedFieldName = "Modified";
private const string ModifiedByFieldName = "Editor";
private static string GetStringByUser(SPUser user)
{
Validation.ArgumentNotNull(user, "user");
return user.ID + ";#" + user.Name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment