0

I'm using version 8 of Kentico and I have a custom document/page that has a unique numeric identity field, unfortunately this data from an existing source and because I cannot set the primary key ID of the page's coupled data when using the API I was forced to have this separate field.

I ensure the field is new and unique during the DocumentEvents.Insert.Before event using node.SetValue("ItemIdentifier", newIdentifier); if the the node's class name matches, etc. So Workflow is handled as well I also implemented the same method for WorkflowEvents.SaveVersion.Before.

This works great when creating a new item, whoever if we attempt to Copy an existing node the source Identifier remains unchanged. I was hoping I could exclude the field from being copied, but am as yet to find an example of that.

So I went ahead and implemented a solution to ensure a new identifier is created when a node is being copied by handling the DocumentEvents.Copy.Before and DocumentEvents.Copy.After.

Unfortunately in my case the e.Node from these event args are useless, I could not for the life of me get the field modified, when I opened IlSpy I realized why, the node copy method grabs a fresh copy of the node from the database always! Hence rendering DocumentEvents.Copy.Before useless if you want to modify fields before a node is copied.

So I instead pass the identifier along in a RequestStockHelper that the Insert, further down the cycle, handles to generate a new identifier for the cloned node.

Unfortunately, unbeknownst to me, if we copy a published node, the value on the database is correct, but the NodeXML value of it is not.

This IMO sounds like a Kentico bug, it's either retaining the source node's NodeXML/version, or for some reason node.SetValue("ItemIdentifier", newIdentifier); is not working properly on the WorkflowEvents.SaveVersion.Before since it's a published and workflowed node.

Anyone come across a similar issue to this? Is there any other way I can configure a field to be a unique numeric identity field, that is not the primary key, and is automatically incremented when inserted? Or exclude a field from the copy procedure?

2 Answers 2

1

As a possible solution, could you create a new document in DocumentEvents.Copy.Before and copy the values over from the copied document, then cancel the copy event itself?

4
  • that could work, but I think I now realize the issue, my interpertration of the WorkflowEvents.SaveVersion.Before. I'm in the process of testing a solution, if that doesn't work I'll definitely give that one a shot. thank you. Apr 10, 2018 at 17:13
  • as suspected, this was actually a problem of my interpretation of WorkflowEvents, I will post an answer with my solution. Apr 11, 2018 at 9:35
  • 1
    thanks for the help I dug a bit deeper, and I believe I could use your cancellation method when DocumentEvents.Copy.Before and then again on DocumentEvents.Insert.Before if the identifier were 0/null, preventing having to pass identifiers along in the RequestStock between events. Apr 11, 2018 at 10:57
  • as yours would have been a neater solution, you got dibs on the answer :D I may refactor to this later if need be. Apr 11, 2018 at 10:58
0

ok, turns out this is not a Kentico issue but the way versions are saved.

if you want to compute a unique value in DocumentEvents.Insert.Before you need to pass it along to WorkflowEvents.SaveVersion.Before because the node that is sent in the later is the same as the original from the former. e.g. whatever changes you do in the Insert node are not sent along to SaveVersion, you need to handle this manually.

So here's the pseudo code that handles the copy scenario and insert of a new item of compiled type CineDigitalAV:

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

    DocumentEvents.Insert.Before += Insert_Before;
    DocumentEvents.Copy.Before += Copy_Before;
    WorkflowEvents.SaveVersion.Before += SaveVersion_Before;

}

private void Copy_Before(object sender, DocumentEventArgs e)
{
    if (e.Node != null)
    {
        SetCopyCineDigitalIdentifier(e.Node);
    }
}

private void SaveVersion_Before(object sender, WorkflowEventArgs e)
{
    if (e.Document != null)
    {
        EnsureCineDigitalIdentifier(e.Document);
    }
}

private void Insert_Before(object sender, DocumentEventArgs e)
{
    if (e.Node != null)
    {
        EnsureCineDigitalIdentifier(e.Node);
    }
}


private void SetCopyCineDigitalIdentifier(TreeNode node)
{
    int identifier = 0;
    if (node.ClassName == CineDigitalAV.CLASS_NAME)
    {
        identifier = node.GetValue<int>("AVCreation_Identifier", 0);

        // flag next insert to create a new identifier
        if (identifier > 0)
            RequestStockHelper.Add("Copy-Identifier-" + identifier, true);
    }
}


private void EnsureCineDigitalIdentifier(TreeNode node)
{
    int identifier = 0;
    if (node.ClassName == CineDigitalAV.CLASS_NAME)
    {
        identifier = node.GetValue<int>("AVCreation_Identifier", 0);
    }

    if (identifier == 0 || (identifier != 0 && RequestStockHelper.Contains("Copy-Identifier-" + identifier)))
    {
        // generate a new identifier for new items ot those being copied
        RequestStockHelper.Remove("Copy-Identifier-" + identifier);
        int newIdentifier = GetNewCineDigitalIdentifierAV(node.NodeSiteName);
        node.SetValue("AVCreation_Identifier", newIdentifier);

        // store the newidentifier so that saveversion includes it 
        RequestStockHelper.Add("Version-Identifier-" + identifier, newIdentifier);
    }
    else if (RequestStockHelper.Contains("Version-Identifier-" + identifier))
    {
        // handle saveversion with value from the insert
        int newIdentifier = ValidationHelper.GetInteger(RequestStockHelper.GetItem("Version-Identifier-" + identifier), 0);
        RequestStockHelper.Remove("Version-Identifier-" + identifier);

        node.SetValue("AVCreation_Identifier", newIdentifier);
    }
}
private int GetNewCineDigitalIdentifierAV(string siteName)
{
    return (DocumentHelper.GetDocuments<CineDigitalAV>()
                            .OnSite(siteName)
                            .Published(false)
                            .Columns("AVCreation_Identifier")
                            .OrderByDescending("AVCreation_Identifier")
                            .FirstObject?
                            .AVCreation_Identifier ?? 0) + 1;
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.