Update a custom module item (Module Builder)
I'm trying to update a custom module item. I can't get it to update a record; it throws an error about not being able to checkin/checkout/publish a non-master record. On standard modules you have to use the manager to get a master record, but I don't see that method in the DynamicModuleManager.
I can create a record just 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;Patrick,
Been having the same problem for the last hour and saw you post. I think I have the solution now, try adding this after your .SaveChanges();
var bag = new Dictionary<string, string>("ContentType", productType.FullName);WorkflowManager.MessageWorkflow(c.Id, productType , null, "Publish", false, bag);This kind of works... It still throws an error "Cannot publish an Item that is not a master". However it does seem to be publishing it. In other words, I'm still getting the same error, but the values are now persisted.
I found it...
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();Type productType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Recipes.Recipe");DynamicContent c = dynamicModuleManager.GetDataItem(productType, this.ID);DynamicContent master = dynamicModuleManager.Lifecycle.Edit(c) as DynamicContent;DynamicContent temp = dynamicModuleManager.Lifecycle.CheckOut(master) as DynamicContent;if (master != null) try // We can now change any values of the item temp.SetValue("Votes", this.Votes); temp.SetValue("Rating", this.Rating); // Now we need to check in, so the changes apply master = dynamicModuleManager.Lifecycle.CheckIn(temp) as DynamicContent; //Finnaly we publish the item dynamicModuleManager.Lifecycle.Publish(master); // You need to call SaveChanges() in order for the items to be actually persisted to data store dynamicModuleManager.SaveChanges(); catch return false; return true;Thanks Patrick this was just what i needed
JT
Hi,
I am using the same code (without the try catch) for updating a dynamic content.
But I get a Object Null reference error on dynamicModuleManager.savechanges();
I'm getting the same error after upgrading to 5.1.
Can someone confirm please.
I'm getting the same error with 5.1. I could create items with an old system design, but then I converted the code over to a new system design, and now the SaveChanges method doesn't seem to work. This might be do with me (i.e. properties not set correctly/manager references not set etc), or it could be to do with the way SF is set up.
Publishing has always been convoluted and inconsistent from version to version. In the latest 5.4, this is what I did:
/// <summary>/// Gets the specified content by id./// </summary>/// <param name="id">The id.</param>/// <param name="checkout">if set to <c>true</c> checkout.</param>/// <returns></returns>public static DynamicContent Get(Guid id, bool checkout = false) //VALIDATE INPUT if (id == Guid.Empty) return null; //GET LIVE ITEM FROM STORAGE var manager = DynamicModuleManager.GetManager(); var content = manager.GetDataItem(GetDynamicType(), id); //CHECK OUT ITEM FOR UPDATE IF APPLICABLE if (checkout) //VALIDATE INPUT if (content == null) return null; //EDIT MODE ON CONTENT AND RETURNED CHECKED OUT ITEM var master = manager.Lifecycle.Edit(content) as DynamicContent; return manager.Lifecycle.CheckOut(master) as DynamicContent; return content;/// <summary>/// Publishes and saves the specified data item./// </summary>/// <param name="dataItem">The data item.</param>public static bool PublishAndSave(this DynamicModuleManager manager, DynamicContent dataItem) //CODE TAKEN FROM DEFAULT CODE REFERENCE OF MODULE BUILDER try //UPDATE PUBLICATION DATE dataItem.PublicationDate = DateTime.UtcNow; //HANDLE NEW ITEM IF APPLICABLE if (dataItem.OriginalContentId == Guid.Empty) // We can now call the following to publish the item manager.Lifecycle.Publish(dataItem); //You need to set appropriate workflow status dataItem.SetWorkflowStatus(manager.Provider.ApplicationName, "Published"); else //HANDLE UPDATES ON EXISTING ITEM // Now we need to check in, so the changes apply var master = manager.Lifecycle.CheckIn(dataItem); // We can now call the following to publish the item manager.Lifecycle.Publish(master); // You need to call SaveChanges() in order for the items to be actually persisted to data store manager.SaveChanges(); return true; catch (Exception ex) //TODO: LOG ERROR return false; var content = Get(new Guid("cde7ad3b-597e-4728-b6e3-245eac3382cf"), true);content.SetValue("Title", "Some new title");DynamicModuleManager.GetManager().PublishAndSave(content);