Created
May 30, 2012 18:41
-
-
Save sergey-tihon/2838208 to your computer and use it in GitHub Desktop.
How to update SPListItem Created/Modified date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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