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

Forums / Developing with Sitefinity / Unable to unpublish a module builder dynamic content item using API

Unable to unpublish a module builder dynamic content item using API

5 posts, 0 answered
  1. Boe
    Boe avatar
    8 posts
    Registered:
    17 Nov 2011
    27 Sep 2012
    Link to this post
    Unable to unpublish a module builder dynamic content item using API.
    I receive no error messages. This happens no matter what user I have logged in.

    var dynamicModuleManager = DynamicModuleManager.GetManager();
      
    using (ElevatedModeRegion elevatedModeRegion = new ElevatedModeRegion(dynamicModuleManager))
    {
        var MyDataTypeType =
            TypeResolutionService.ResolveType(
                "Telerik.Sitefinity.DynamicTypes.Model.MyDataTypes.MyDataType");
        var data = dynamicModuleManager.GetDataItem(MyDataTypeType, commentId);
        dynamicModuleManager.Lifecycle.Unpublish(data);
        data.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Unpublished");
        dynamicModuleManager.SaveChanges();
    }
  2. Patrick Dunn
    Patrick Dunn avatar
    237 posts
    Registered:
    03 Nov 2014
    29 Sep 2012
    Link to this post
    Hello,

     In order to unpublish a dynamic content item we need to make sure that we are opening it for editing and making sure it is published to begin with. Please see the following example:

    var dynamicModuleManager = DynamicModuleManager.GetManager();
       
    using (ElevatedModeRegion elevatedModeRegion = new ElevatedModeRegion(dynamicModuleManager))
    {
        var MyDataTypeType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.MyDataTypes.MyDataType");
     
        var data = dynamicModuleManager.GetDataItem(MyDataTypeType, commentId);
         
        DynamicContent master = (DynamicContent)dynamicModuleManager.Lifecycle.Edit(data);
        if (master != null)
        {
            dynamicModuleManager.Lifecycle.Unpublish(master);
             
            data.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Unpublished");
             
            dynamicModuleManager.SaveChanges();
        }
    }

    Let me know if you have any additional problems! Greetings,
    Patrick Dunn
    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. Rico
    Rico avatar
    88 posts
    Registered:
    30 Aug 2012
    09 Dec 2014 in reply to Patrick Dunn
    Link to this post

    Hi there. I realize this is an old post but can't find any alternate method that attempts to 'unpublish' and dynamic content item.

     

    I receive the following error "Cannot unpublish an item that is not published" on the following line: dynamicModuleManager.Lifecycle.Unpublish(master)

     

     

     

     

     

     

     

     

     

  4. Rico
    Rico avatar
    88 posts
    Registered:
    30 Aug 2012
    10 Dec 2014 in reply to Rico
    Link to this post

    I was able to resolve my issue as follows:

    foreach(var p in projectsToUnpublish)

    {

    using (ElevatedModeRegion elevatedModeRegion = new ElevatedModeRegion(dynamicModuleManager))

    {

    var contentType = "MyContentType";

    var data = dynamicModuleManager.GetDataItem(contentType, p.Id);

    DynamicContent master = (DynamicContent)dynamicModuleManager.Lifecycle.Edit(data);

    if (master != null)

     

     

    {

    // Check out the master to get a temp version.

    var temp = dynamicModuleManager.Lifecycle.CheckOut(master) as DynamicContent;

    temp.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Unpublished");

    temp.ApprovalWorkflowState = "Unpublished";

    // Check in the temp and get the updated master version.

    // After the check in the temp version is deleted.

    master = dynamicModuleManager.Lifecycle.CheckIn(temp) as DynamicContent;

    // Publish the master.

    dynamicModuleManager.Lifecycle.Publish(master);

    dynamicModuleManager.SaveChanges();

    }

    }

    }

     

     

  5. Merritt
    Merritt avatar
    31 posts
    Registered:
    11 Mar 2011
    12 Jun 2015 in reply to Rico
    Link to this post
    Let's start with a master dynamic content, or status = 0 in sf_dynamic_content, and call it production.

    Let's say its current approval_workflow_State_ = "Published", and visible = 0 (it will always be 0 because you never see the master). We should, less ye have been hacking around in the system, have another record in this table with same url_name_ and approval_workflow_State_ = "Published", status= 2, and visible = 1. That will be the Live version.

    var dynamicModuleManager = DynamicModuleManager.GetManager();<br>var checkedOutProduction = (DynamicContent) dynamicModuleManager.Lifecycle.CheckOut(production);

    These lines will let us to edit the master, but it doesn't give us the live version, just a temp version you can modify a bit. Honestly I don't see the point of using it unless you are worried about concurrency or other issues that could exist. What I mean here is that you can make edits directly on the master record, but bear in mind if dynamicModuleManager.SaveChanges() somewhere else in the code concurrently, I believe your changes will be collected and persisted. I don't really know exactly how they are doing this, but OpenAccess system is very similar to older ORM system that don't use poco objects and it doesn't manually have you declare what you want to save, meaning something like this dynamicModuleManager.SaveChanges(production). Therefor the check-in and check-out pattern.

    checkedOutProduction.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Unpublished");

    this line just sets the master to unpublished, making approval_workflow_State_ = "Unpublished". 

    var checkedInProduction = (DynamicContent) dynamicModuleManager.Lifecycle.CheckIn(checkedOutProduction);

    These lines check in the temp, replacing the existing master record (in memory). Any calls to dynamicModuleManager.SaveChanges() will now persist the changes.

    Now for the important code:

    var liveProduction = dynamicModuleManager.Lifecycle.GetLive(checkedInProduction);
    if (liveProduction != null)
         dynamicModuleManager.Lifecycle.Unpublish(liveProduction);

    This will change visible = 0 but keeps approval_workflow_State_ = "Published". This is what you want. If you don't believe me, interact with a dynamic content type from its list view page. Look at the 2 records for an item in the database before and after selecting it for unpublishing.

    dynamicModuleManager.SaveChanges();
     
     Now you have persisted your changes.
     
     A side note: if you are trying to unpublish an item with no Live version, like a draft that have never been published, you'll notice there is only 1 record in the database. The code above checks for that by verifying that live version is not null. If if is, technically you are breaking the workflow and it shouldn't be a valid operation. What I would do here, is verify that your master record has approval_workflow_State_ = "Published" before doing any of this this, even before you check it out, because you don't really want to set your master record to unpublished from the draft state. 
5 posts, 0 answered