Kentico 12: The page with workflow unable to populate the widget.

Chad Tran asked on December 15, 2021 11:37

Dear all,

I have faced some issues related to basic workflow, please give any advice or alternative solution.

In the CMS:

  1. Created a page type. E.g: Blog
  2. Create a basic workflow included check-in/check-out with 2 more steps. E.g: Workflow name: BS-WF, added standard steps: Review, Approve

In the Backend code:

  1. Register a DocumentEventHandler to raise events after DocumentEvents.Insert.After and DocumentEvents.Update.After for the Blog page type. E.g: DocumentEvents.Insert.After += Document_Insert_After; DocumentEvents.Update.After += Document_Update_After;

  2. Document_Insert_After(object sender, DocumentEventArgs e)// Initialize widgets by using NodeId to get DocumentCultureDataInfo (variable: docCultureInfo) of page and set DocumentPageBuilderWidgets with defined widgets as JSON string. Then call docCultureInfo.Update()

  3. Document_Update_After(object sender, DocumentEventArgs e)// Update the total words count when editing content on widgets of Blog. Then call e.Node.Update()

Here is the problems:

Scenario 1:

  1. Create a new Blog without apply workflow BS-WL. All widgets in new Blog are initialized.
  2. Check-out the Blog, edit and update properties of any widget.
  3. Apply widget and Save Blog → The page is updated with modified content and widgets → OK
  4. Check-in and move to next workflow → OK

Scenario 2:

  1. Create a new Blog and apply workflow BS-WL. All widgets in new Blog are initialized.
  2. Check-out the Blog, edit and update properties of any widget.
  3. Apply widget and Save Blog → The page is not updated with modified content. The widget is not updated → NOT OK
  4. Check-in and check-out again, then edit the modified widget again → The previous properties setting is not save → NOT OK

Scenario 3: Apply workflow BS-WL for other page types which not include in DocumentEventHandler → Working correctly as Scenario 1.

Questions:*

  1. Could you please tell me what exactly the problem for my current implementation? Is it related to the issues about Workflow and Versioning of page?

  2. As the requirement for the page:

    • Must apply workflow
    • Initialized defined widgets
    • Update the field of page when editing content on CMS

Any alternative solution for it?

Thanks

Correct Answer

Juraj Ondrus answered on December 22, 2021 12:51

Would it be possible to provide also the entire code you are using? I am testing this on the sample Dancing Goat MVC project. On the Landing page page type. Here are screen shots of my workflow setup:
Workflow steps
Scope
Pages app- to see that the scope is applied

Now, I am using this event handler code in my class library:

 protected override void OnInit()
        {
            base.OnInit();
            DocumentEvents.Insert.After += Insert_After; 
        }

        private void Insert_After(object sender, DocumentEventArgs e)
        {
            var page = e.Node;
            if (page.ClassName == "DancingGoatMvc.LandingPage")
            {
                var widgetsJson = page.GetValue("DocumentPageBuilderWidgets"); 
                    page.SetValue("DocumentPageBuilderWidgets","<JSON value, removed for brevity>");  
                    page.Update();
            }
        }

As you can see I am using bit different code since the DocumentPageBuilderWidgets column is not exposed by the TreeNode object I am using GetValue() and SetValue() methods to work with it. And it is working fine. Please see this short video - when I create the page, there are some widgets already placed and page is in the edit step of the workflow.
I hope it helps!

0 votesVote for this answer Unmark Correct answer

Recent Answers


Chad Tran answered on December 15, 2021 11:42 (last edited on December 15, 2021 12:32)

P/s: When I disable the Check-in/Check-out in Workflow setting. Use check-in/check-out = No, everything is working correctly as normal. So is it a bug of Kentico 12?

Thanks

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on December 16, 2021 08:44

Are you using the right API to update the latest edited version of the page? Please see the API example. You may need to call also the CheckOut and CheckIn to ensure proper versioning - in API it is not about content locking so even if it is disabled in the admin UI, you may need to use it in the API. See the code comments.

0 votesVote for this answer Mark as a Correct answer

Chad Tran answered on December 16, 2021 10:02

Hi @Juraj,

Thanks for your reply.

With my current issues, I think the content locking has a problem because I want to use CheckOut and CheckIn on my page but the page did not save my widget properties settings.

But while disabled CheckOut and CheckIn in the admin UI, the page did save my widget properties settings and apply workflow as normal.

Do you think the DocumentEvents.Update has a deathlock when I try to call (TreeNode)page.Update() in my event Document_Update_After?

Thanks,

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on December 16, 2021 10:54

Well, it depends on the code - the Update() method may invoke the handler again. I would maybe update the data in the before update event and in this case it might not be necessary to call page.Update(). And it also depends whether you want to keep track of these changes in the version history. If not, you do not need to use the CheckIn/Out in the code.

0 votesVote for this answer Mark as a Correct answer

Chad Tran answered on December 20, 2021 06:03

Hi @Juraj,

As your suggest idea, I have solved my issue by prevent the page.Update() by unnecessary. But now I have a new issues after that.

In the event DocumentEvents.Insert.After, I tried to initialized all necessary widgets for page by loading JSON string object to DocumentPageBuilderWidgets of DocumentCultureDataInfo as this article. Example:

    protected override void OnInit()
    {
        base.OnInit();

        DocumentEvents.Insert.After += PreLoadWidget;
    }
    private void PreLoadWidget(object sender, DocumentEventArgs e)
    {
        var docCultureInfo = DocumentCultureDataInfoProvider.GetDocumentCultureInfo(e.Node.DocumentID);
        var widgetsJson = docCultureInfo.DocumentPageBuilderWidgets;
        //Create widget json
        if (widgetsJson == null || widgetsJson == String.Empty)
        {
                docCultureInfo.DocumentPageBuilderWidgets = "{\"editableAreas\":" +
               "[{\"identifier\":\"areaWithSection\",\"sections\":[{\"identifier\":\"a5738704-5c6f-40e6-b335-61d376ea4da2\"," +
               "\"type\":\"NTUCIncome.Sections.Col5050\",\"properties\":null,\"zones\":[{\"identifier\":" +
               "\"d49bdbca-44fd-4e2b-8980-01f0333b5511\",\"widgets\":[{\"identifier\":\"68ec1058-ff21-41ee-8d73-e837b91788de\"," +
               "\"type\":\"BlogMenuBarWidget\",\"variants\":[{\"identifier\":\"77185730-4901-4ba9-a651-cfbb71a92542\"," +
               "\"properties\":null}]}]}]}]},{\"identifier\":\"areaWithSection1\",\"sections\":[{\"identifier\":" +
               "\"37657cab-0eb1-4e52-a2bb-d2e6ccefb1e9\",\"type\":\"NTUCIncome.Sections.Col5050\",\"properties\":null," +
               "\"zones\":[{\"identifier\":\"82135da4-bb59-41ef-a1cc-1b4859ead49c\"," +
               "\"widgets\":[{\"identifier\":\"ef290d65-cd04-4033-a123-10119a4ce66b\"," +
               "\"type\":\"BlogArticleWidget\",\"variants\":[{\"identifier\":\"8fdfe6f5-2209-4db7-bafc-d9b450bd2360\"," +
               "\"properties\":{\"subTitle\":\"\"}}]}]}]}]},{\"identifier\":\"areaWithSection2\",\"sections\":[{\"identifier\":" +
               "\"9c226759-a74f-4fc4-9b54-2ac072d30950\",\"type\":\"NTUCIncome.Sections.Col5050\",\"properties\":null,\"zones\":" +
               "[{\"identifier\":\"fe30c82a-60ce-427f-9ba5-9d75ef09a2b5\",\"widgets\":[{\"identifier\":\"4664d403-3983-439a-aa63-0b6b20c8ce70\"," +
               "\"type\":\"BottomTagsWidget\",\"variants\":[{\"identifier\":\"7a588bae-5aff-499d-b5e7-61f4af7c8de6\",\"properties\":{}}]}," +
               "{\"identifier\":\"5d35cea1-4f20-4333-b799-230c19f4599b\",\"type\":\"AuthorRelatedListWidget\",\"variants\":[{\"identifier\":\"af1c6d93-a77a-4938-8577-1690458fe11b\"," +
               "\"properties\":null}]},{\"identifier\":\"eb27c8ef-9a3b-4bf1-898d-271227c2f7bc\",\"type\":\"RelatedProductsWidget\"," +
               "\"variants\":[{\"identifier\":\"3aa67328-a85c-4ff1-90c0-f17bc3da6aa8\",\"properties\":null}]}]}]}]},{\"identifier\":\"areaWithSection3\"," +
               "\"sections\":[{\"identifier\":\"4ce67bd7-120f-450b-8d08-768da96a29cb\",\"type\":\"NTUCIncome.Sections.Col5050\",\"properties\":null," +
               "\"zones\":[{\"identifier\":\"039921c8-276a-4ce6-bba3-b8484d5dcc57\",\"widgets\":[{\"identifier\":\"0f79806c-3897-4392-8fed-4a7ce4562340\"," +
               "\"type\":\"RelatedArticlesWidget\",\"variants\":[{\"identifier\":\"ee852be2-49f6-4fcf-99a8-ab1f730425e3\",\"properties\":null}]}]}]}]}]}";
               docCultureInfo.Update();
        }
    }

But the widgets are not load correctly when creating new Blog with content locking.

There is some points from my investigate:

  1. This issue only occurs when using content locking Check-in/ Check-out
  2. With enable Check-in/ Check-out, the page cannot initialize all widgets when creating new Blog. → not OK
  3. With disable Check-in/ Check-out, all widgets are initialized when creating new Blog. → OK

Is my implementation missing anything? Could you please provide any correction?

Thanks

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on December 20, 2021 06:12

When using content locking it means there is a workflow. Are you editing the right version of the page? From the code it seems that you are editing just the latest published version of the page. While, in the admin UI the Pages app is displaying latest edited version and these an be different.

0 votesVote for this answer Mark as a Correct answer

Chad Tran answered on December 20, 2021 09:01

Hi @Juraj,

Thanks for your idea!

I have checked again and found that the page have 2 version 1.0 and 1.1 when creating new page. It also auto set in stage Checkout while I did nothing.

But when I tried to call page.UndoCheckout() in the end of method PreLoadWidget(), it still not working and throw exception message: "It no previous version to roll back".

What should I do in this case?

Thanks

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on December 21, 2021 13:30

would it be possible to provide some detailed and step by step description how to reproduce the issue? So I can debug it on my side. I think we are missing here something simple or maybe you are just using wrong events to do what you want to do.

0 votesVote for this answer Mark as a Correct answer

Chad Tran answered on December 22, 2021 04:39

Hi @Juraj,

Please following below steps.

  1. Add custom event to initialized some widgets in DocumentEvents.Insert.After
  2. Go to CMS, create a basic workflow with new step Approve and enable content locking Check-in/Check-out
  3. Create a Blogs folder and Blog page type
  4. Apply workflow for Blogs folder and check config "To this page and its children"
  5. Create new Blog, input contents then Save

→ Expected: New Blog page is created with initialized widgets.

Actual: New Blog page is created with blank, no widgets are initialized.

0 votesVote for this answer Mark as a Correct answer

Chad Tran answered on December 22, 2021 13:07 (last edited on December 22, 2021 13:07)

Hi @Juraj,

Thanks for your efforts and supports!

I think I almost reach the goal, I will try with your suggestion.

And one more thing, it seem like you had not enable Check-in/Check out on your video yet. Could you please confirm that even if enable content locking, it still work OK?

Thanks.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on December 22, 2021 13:12

Good catch! I totally forgot about it. However, in the admin UI, where it is used for content locking for exclusive access, it makes no difference. New video with check in/out enabled for the workflow here.

1 votesVote for this answer Mark as a Correct answer

Chad Tran answered on December 23, 2021 03:49

Hi @Juraj,

With your help, the issue is completely solvable. Again, thanks for your efforts and supports!

Best regards,

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.