+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Module Builder -> Updating fields programmatically

Module Builder -> Updating fields programmatically

4 posts, 0 answered
  1. Richard
    Richard avatar
    15 posts
    Registered:
    17 Aug 2012
    20 Feb 2012
    Link to this post
    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
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    06 Dec 2016
    22 Feb 2012
    Link to this post
    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
  3. Bernstein-Rein
    Bernstein-Rein avatar
    30 posts
    Registered:
    12 Mar 2010
    11 Apr 2012
    Link to this post
    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?
  4. Lee
    Lee avatar
    6 posts
    Registered:
    31 Dec 2007
    04 Feb 2015 in reply to Bernstein-Rein
    Link to this post

    Have you checked out this article? ( =

    http://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!

     

     

     

4 posts, 0 answered