Module Builder -> Updating fields programmatically
Hello,
I am trying to update a value of one of my custom module data rows. I am following the code set out via the examples provided with the custom module page. However, the following error occurs:
Hi,
I have created a dynamic module with the name "Tito" (used a strange name here). This cretes new item for this module with :
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(); Type titoType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Tito.Tito"); DynamicContent titoItem = dynamicModuleManager.CreateDataItem(titoType); // This is how values for the properties are set titoItem.SetValue("Title", "Some Title"); titoItem.SetValue("Owner", SecurityManager.GetCurrentUserId()); titoItem.SetValue("PublicationDate", DateTime.Now); // First we will publish the item dynamicModuleManager.Lifecycle.Publish(titoItem);dynamicModuleManager.Lifecycle.CheckOut(titoItem); // We can now change any values of the item titoItem.SetValue("Title", "Checked out item"); // Now we need to check in, so the changes apply //titoItem.Lifecycle.(titoItem); //Finnaly we publish the item again dynamicModuleManager.Lifecycle.Publish(titoItem); // You need to call SaveChanges() in order for the items to be actually persisted to data store dynamicModuleManager.SaveChanges();DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(); Type titoType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Tito.Tito"); DynamicContent titoItem = dynamicModuleManager.CreateDataItem(titoType); // This is how values for the properties are set titoItem.SetValue("Title", "Some Title"); titoItem.SetValue("Owner", SecurityManager.GetCurrentUserId()); titoItem.SetValue("PublicationDate", DateTime.Now); // First we will publish the item dynamicModuleManager.Lifecycle.Publish(titoItem); // Then we check it out dynamicModuleManager.Lifecycle.CheckOut(titoItem); // We can now change any values of the item titoItem.SetValue("Title", "Checked out item"); // Now we need to check in, so the changes apply //titoItem.Lifecycle.(titoItem); //Finnaly we publish the item again dynamicModuleManager.Lifecycle.Publish(titoItem); // You need to call SaveChanges() in order for the items to be actually persisted to data store dynamicModuleManager.SaveChanges();I'm having a similar issue, I'm getting the same error. I can't get it to update a record. Creating one is fine, but I need to update a record. This is what I have:
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();Type productType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Recipes.Recipe");DynamicContent c = dynamicModuleManager.GetDataItem(productType, this.ID);if (c != null) try // first we check it out dynamicModuleManager.Lifecycle.CheckOut(c); // We can now change any values of the item c.SetValue("Votes", this.Votes); c.SetValue("Rating", this.Rating); // Now we need to check in, so the changes apply dynamicModuleManager.Lifecycle.CheckIn(c); //Finnaly we publish the item dynamicModuleManager.Lifecycle.Publish(c); // You need to call SaveChanges() in order for the items to be actually persisted to data store dynamicModuleManager.SaveChanges(); catch return false; return true;DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();Type productType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Recipes.Recipe");DynamicContent c = dynamicModuleManager.GetDataItem(productType, this.ID);if (c != null) try // first we check it out // We can now change any values of the item c.SetValue("Votes", this.Votes); c.SetValue("Rating", this.Rating); // You need to call SaveChanges() in order for the items to be actually persisted to data store dynamicModuleManager.SaveChanges(); catch return false; return true;Have you checked out this article? ( =
docs.sitefinity.com/for-developers-edit-content
I believe you should create a temp var to store what you are checking out, make the changes and then check in that temp var:
// first we check it out
var tempItem = dynamicModuleManager.Lifecycle.CheckOut(c);
// We can now change any values of the item
tempItem.SetValue("Votes", this.Votes);
tempItem.SetValue("Rating", this.Rating);
// Now we need to check in, so the changes apply
dynamicModuleManager.Lifecycle.CheckIn(tempItem);
//Finally we publish the item
dynamicModuleManager.Lifecycle.Publish(c);
// You need to call SaveChanges() in order for the items to be actually persisted to data store
dynamicModuleManager.SaveChanges();
Hope this helps!