Thursday, November 11, 2021

Sitecore CM: Limiting the number of versions in Sitecore

" If you found yourself searching for a solution for limiting the number of versions that get created by your content authors, you probably have come across the old blog post by John West (Rules Engine Actions to Remove Old Versions in the Sitecore ASP.NET CMS). The blog post is awesome like all blog posts that John has written. It is over 8 years ago and some things did change in Sitecore since then. So I figured I would write an update to it as I wasn't able to find one.

The code part is still valid. I did run into an exception in the "while" loop, so I changed the code in the Apply function to the following:

public override void Apply(T ruleContext) { 

    Assert.ArgumentNotNull(ruleContext, "ruleContext");     

    Assert.ArgumentNotNull(ruleContext.Item, "ruleContext.Item"); 

    // for each language available in the item 

    foreach (Language lang in ruleContext.Item.Languages) { 

        var item = ruleContext.Item.Database.GetItem(ruleContext.Item.ID, lang); 

        if (item == null) { continue; } 

        var versionsToProcess = item.Versions.GetOlderVersions().OrderByDescending(i => i.Version.Number).Skip(this.MinVersions); 

        foreach(var version in versionsToProcess) { 

            Assert.IsNotNull(version, "version"); 

            if (this.MinUpdatedDays < 1 || version.Statistics.Updated.AddDays(this.MinUpdatedDays) < DateTime.Now) {

                this.HandleVersion(version); 

            

        

    

}

There are a few things changed in the Rules area.

After you create all four classes for the rule actions (MinVersionsAction, RecycleOldVersions, ArchiveOldVersions, and DeleteOldVersions) the following actions will need to be performed in Sitecore:

  • Go to "/sitecore/system/Settings/Rules/Definitions/Elements" and duplicate any of the elements. Name the new element "Item Version Limiting" or any other way you choose.
  • Remove all conditions and actions if you duplicated an existing element that had them.
  • Create three Actions using Action data template as John described: Archive Versions Beyond, Delete Versions Beyond, Recycle Versions Beyond.
  • In the Text field put the values that John provided:
Archive Versions Beyond:
archive versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days

Delete Version Beyond:
delete versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days

Recycle Versions Beyond:
recycle versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days
  • In the Type field put the reference to the corresponding class you created.

  • To get the actions to appear in the rule selection window you'll have to add a new Tag called "Item Version Limiting" under "/sitecore/system/Settings/Rules/Definitions/Tags/Item Version Limiting". You can duplicate an existing tag for that.
  • In "Default" item under "/sitecore/system/Settings/Rules/Definitions/Elements/Item Version Limiting/Tags" update the reference to the newly created tag.

  • In the Default item under "/sitecore/system/Settings/Rules/Item Saved/Tags" in the Tags field add a reference to the newly created "Item Version Limiting" tag.

  • Go to the "/sitecore/system/Settings/Rules/Item Saved/Rules" and add a new rule. Define the rule condition and action in the Rules field.



Condition is mandatory and the most suitable one I found without writing my own was the "where the number of versions compares to number" one from the Item Version CM.


Friday, November 5, 2021

Sitecore CDP: Triggered Experience with decision model needs offers

 I was fortunate to be able to take Sitecore CDP training with Sarah O'Reilly (https://www.sitecore.com/products/customer-data-platform). There were a lot of interesting features in Sitecore CDP that we got to play with during this training. If you have a chance to take, do so, you won't regret it.

During day four of the training we ran into an issue when our triggered full stack experience was failing with "NO DECISION" message. The full stack experience used a decision model that simply returned a list of products in abandoned cart and had only one programmable that returned that list. Without a decision model the experience works fine and send the email, but if a decision model is added it fails.



After further investigation into the log for each failed execution it was clear that the experience is expecting the offers even though there were no offers to return. All we wanted to do is to generate a list of products in abandoned cart and send guest an email with the list of those products.

Clicking on the "View Log" brings back the details about a specific execution which allowed me to debug it further. As as you can see below the decision model returns results, and everything should be working, but it didn't. 


I figured why not to compare the executions without the decision models that succeeded with the ones with the model that are returning "NO DECISION". And I noticed that one particular property is null in the second case:



The next step was adding logging to my programmable. To do that I added "print()" statement to render the guest session details and made this variant of the decision model live. In the log property of my failed execution I started seeing values and one of those values was "status=ERROR, error=No offers returned, errorCategory=NO_DECISION".

I started suspecting that the experience expects to get some sort of offers from the decision model. So I added a decision model to the programmable that always returns an offer. It doesn't matter what the offer is as long as it is an offer. After I made that variant of the decision model live my triggered experience started working.

Sarah O'Reilly confirmed that while this limitation of Sitecore CDP was recently removed in interactive experience, it still exists in triggered one. Just something to be aware of.