Update a List Item Without Anyone Looking


So lets say you want to change a list item but you don’t want to impact any versioning or having to deal with check-in or check-out, and you don’t want to change the last modified or modified by values. You can do this with the bit of code shown below. Grab the item reference and update the item just as you normally would however rather than calling SPItem.Update() you want to call SPItem.SystemUpdate(). You can check out the TechNet reference if you want to see additional code samples.

using (SPSite site = new SPSite("http://team"))
{
  SPList list = site.RootWeb.Lists.TryGetList("Annoucements");
  if (list != null)
  {
    SPListItem item = list.Items[0];
    item["Title"] = DateTime.Now.ToLongTimeString();
    item.SystemUpdate(false);
  }
}

There are two variations of this method and the one I called here passes false to indicate that I don’t want to increment the version count of the object for which I am updating, useful if you have versioning enabled on the list and ignored if you don’t. The second variation of the method takes no parameters and is the same as calling SystemUpdate(true) which does increment the version on the item.

So after executing the code above here is what my Announcements list looks like. Note the lack of change of the Modified date and the fact the title was set to the current date/time. Keep in mind too that although you made this change and it appears to be transparent to the list the auditing logs (if enabled) will pick up the change just as if you had called the Update() method so this is not a way to get around auditing.

One thought on “Update a List Item Without Anyone Looking

  1. Your high quality articles are so great, and can we buy some ads from you? If you agree, just emial me the ad type and fee per month. If you own some other high quality related blogs, selling ads would be welcomed.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s