Update a custom module item (Module Builder)

Posted by Community Admin on 03-Aug-2018 16:38

Update a custom module item (Module Builder)

All Replies

Posted by Community Admin on 12-Apr-2012 00:00

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;


This works, but it doesn't publish:
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;


Any thoughts?

Posted by Community Admin on 13-Apr-2012 00:00

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);

Kind Regards
Faizal

Posted by Community Admin on 16-Apr-2012 00:00

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.

Posted by Community Admin on 16-Apr-2012 00:00

I found it...

The trick is to get the master from DynamicModuleManager.Lifecycle.Edit().

Here is the working version:
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;

Posted by Community Admin on 31-May-2012 00:00

Thanks Patrick this was just what i needed
JT

Posted by Community Admin on 27-Jul-2012 00:00

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 have recently upgraded Sitefinity to 5.1.

Posted by Community Admin on 29-Jul-2012 00:00

I'm getting the same error after upgrading to 5.1.
Can someone confirm please.

Posted by Community Admin on 23-Aug-2012 00:00

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.

Advice on solving this would be useful!

Thanks,
Rich

Posted by Community Admin on 17-Mar-2013 00:00

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;
    

Then I can do something like that:
var content = Get(new Guid("cde7ad3b-597e-4728-b6e3-245eac3382cf"), true);
content.SetValue("Title", "Some new title");
DynamicModuleManager.GetManager().PublishAndSave(content);

Thanks David for the code!! I couldn't find Lifecycle.Edit anywhere in the docs or Module Builder code references.


This thread is closed