How to programaticly update item fields in Sitecore
March 9, 2010 Leave a comment
It is quaite streat forword, there is one thing thou that you should take notice of..
if you just get tour item from master database you can get up getting the wrong version of the item in a wrong language ,.. so this eksample will compile but jeald falsy results.
string guid ="{............}" Database database = Factory.GetDatabase("master"); Item item = database.GetItem(guid); item.Editing.BeginEdit(); using(new EditContext(item)) { item.Fields["fieldname"].Value = "new value"; } item.Editing.EndEdit();
So to get the right version from the right language, you should do something like this.
Database database = Factory.GetDatabase("master"); Item item = database.GetItem(guid,Sitecore.Globalization.Language.Parse("da"),Sitecore.Data.Version.Latest); item.Editing.BeginEdit(); using(new EditContext(item)) { item.Fields["fieldname"].Value = "new value"; } item.Editing.EndEdit();
Notice that i am using Sitecore.Globalization.Language.Parse(“da”) to get the language i wanted and Sitecore.Data.Version.Latest to get the newserst version of my item. (c:
an that is all she wrote (c;