Search This Blog

Wednesday, August 27, 2014

Difference between SpListItem.Update and SpListItem.SystemUpdate

To update a list item we have two ways. One of them is SPListitem.Update and its is most popular way. But this method is change version history and trigger all alerts. Some times, some reason you can want to avoid change version history or triggering allerts. Like this case, SpListitem.SystemUpdate medhod help us. Because that SystemUpdate() is not changing version history.
For Example; I have a list name Personel and I want to change secont record by a console application with SPitem.Update method. Before Run this code sample my list item history like this.
 using (SPSite site = new SPSite("http://testsite"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Personel"];
                    SPListItem item = list.Items[1];
                    item["Title"] = "Sample Update with Versions.";
                    item.Update();
                    list.Update();
                }
            }
After work this Consle Application secont item version history changin like this;

The outher mathod which SystemUpdate() don't add new version history.
using (SPSite site = new SPSite("http://testsite"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Personel"];
                    SPListItem item = list.Items[1];
                    item["Title"] = "Sample Update with out add Versions";
                    item.SystemUpdate();
                    list.Update();
                }
            }
Finnaly My item history changing but not a new version number.

No comments:

Post a Comment