Publish via code not behaving as expected

Regis Wengel asked on December 4, 2018 17:21

I have a client who has been making an accidental mistake when they create articles. When the editor has created an article they have been entering an extra whitespace at the end of the FirstName. For example:

FirstName: 'Regis '

LastName: 'Wengel'

The issue was noticed when they realized some articles were not linking properly with their author page. This is a result of the ArticleAuthors entry in the database have the tailing white space and thus not finding a match when searching for the author. I've created code to account for this in the future, but now I'm creating a scheduled task to fix all of the mistakes in the past. The client is using a simple save -> submit for approval -> publish workflow. I've tested the code when publishing a current article with the extra whitespace and it works as expected. The issue I'm having now is when I'm using the same code (different location) to create a scheduled task to fix all the past instances. My code is as follows.

var documents = DocumentHelper.GetDocuments().Types("article");

foreach (TreeNode document in documents) {
    if (document.IsPublished && !document.IsCheckedOut) {        
        // Remove leading and trailing white spaces from the academic name
        var firstName = document.GetValue<String>("FirstName", string.Empty).Trim();
        var lastName = document.GetValue<String>("LastName", string.Empty).Trim();

        document.SetValue("FirstName", firstName);
        document.SetValue("LastName", lastName);
        document.Update();
    }

    document.Publish("Published via RemoveWhitespaces scheduled task.");
}

string detail = "Executed from '/App_Code/CustomScheduledTasks.cs'. Task data:" + ti.TaskData;
EventLogProvider.LogInformation("RemoveWhitespaces", "Execute", detail);

Originally I thought my issue might be that .Update() was somehow getting called after .Publish() so I separated the .Publish() call into a second scheduled task and ran it after the first which remove the whitespaces. However this doesn't work either. It seems as though the whitespaces are being removed because when I open the article in Kentico Form the FirstName no longer has the tailing whitespace, but it doesn't seem to be publishing properly as the ArticleAuthors column in the database is still has the extra whitespace which is being used to match article and authors.

Using Kentico 11.0.27

Correct Answer

Zach Perry answered on December 4, 2018 18:43

I think your issue is versioning, Try something like this:

var documents = DocumentHelper.GetDocuments().Types("article");

foreach (TreeNode document in documents) {
    if (document.IsPublished && !document.IsCheckedOut) {        
        // Remove leading and trailing white spaces from the academic name
        var firstName = document.GetValue<String>("FirstName", string.Empty).Trim();
        var lastName = document.GetValue<String>("LastName", string.Empty).Trim();

        document.CheckOut();
        document.SetValue("FirstName", firstName);
        document.SetValue("LastName", lastName);
        document.Update();
        document.CheckIn();
    }

    document.Publish("Published via RemoveWhitespaces scheduled task.");
}

string detail = "Executed from '/App_Code/CustomScheduledTasks.cs'. Task data:" + ti.TaskData;
EventLogProvider.LogInformation("RemoveWhitespaces", "Execute", detail);
1 votesVote for this answer Unmark Correct answer

Recent Answers


Regis Wengel answered on December 4, 2018 20:35

Thank you Zach, that ended up solving that problem and another issue I was having. I appreciate it!

0 votesVote for this answer Mark as a Correct answer

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