Module Builder -> Updating fields programmatically

Posted by Community Admin on 04-Aug-2018 01:33

Module Builder -> Updating fields programmatically

All Replies

Posted by Community Admin on 20-Feb-2012 00:00

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:

Cannot publish an item that is not a master.

This is very confusing. I have tried a number of methods to get around this, but none seem to be working (for example, I get a similar message but with 'temp' instead). 

Can you provide me with a solution to this problem?

Thanks,
Rich

Posted by Community Admin on 22-Feb-2012 00:00

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);
After this I get the item Checkout the item and change the value with SetValue
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();
To use SetValue you will need using Telerik.Sitefinity.Model;

The full code:

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

Greetings,
Stanislav Velikov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

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

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;


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 04-Feb-2015 00:00

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!

 

 

 

This thread is closed