Try our conversational search powered by Generative AI!

HXH
HXH
Jun 30, 2010
  5338
(0 votes)

CreatedBy Not Set When Jobs run on a Schedule – Oracle Error

I recently stumbled across a bug in EPiServer that relates directly to Oracle. It’s somewhat obscure, so I figured it would be a good idea to post how we dealt with it here…

When a page is created through the SDK’s DataFactory.Instance.Save method, EPiServer automatically sets CreatedBy to the person who is logged in and executing the code. So for manual page creation through the SDK, the following works just fine…

PageData myPage = EPiServer.DataFactory.Instance.GetDefaultPageData(
                      parent, "Standard page");
myPage.PageName = "My new page";
myPage.Property["MainBody"].Value = "<p>This is produced programmatically.</p>";
myPage.VisibleInMenu = false;
myPage.StartPublish = DateTime.Now.AddMinutes(-1);
DataFactory.Instance.Save(myPage, EPiServer.DataAccess.SaveAction.CheckIn, AccessLevel.NoAccess);

But when this is set up to be run in a job that is scheduled, the CreatedBy is set to NULL. For the vast majority of sites that run EPiServer on SQL Server, this isn’t an issue. But those sites that run on Oracle will find that, upon logging into their CMS, the application throws a lovely “Unable to Cast object of type ‘System.DBNull’ to type ‘System.String’.” error.

 

image

 

What’s happening here is that EPiServer is calling several FindPageActivities methods, and looking at the Activities associated with a particular user name. In this case, it sees that a page needs to be displayed, but it has NULL for CreatedBy. In SQL Server, an empty string is displayed. In Oracle, an exception is thrown.

This is a bug, and will likely be addressed by EPiServer at some later point.. but in the interim, a workaround needs to be found for those who might be experiencing this issue.

… which is where Ted Nyberg’s blog post came in. I went ahead and created a process account, checked if this was being run by a scheduled job or if it was running by a person, and then defaulted to a process account in a situation when no CurrentPrincipal.Identity.Name existed… this creates the page programmatically on a schedule and sets the default CreatedBy to whatever account you specify. Back in business, no oracle errors.

            public static string Execute()
            {
                if (PrincipalInfo.CurrentPrincipal.Identity.Name == string.Empty)
                {
                    PrincipalInfo.CurrentPrincipal = PrincipalInfo.CreatePrincipal("test_account");
                }

                    int Records = GetRecords();
                    return "Content succesfully imported. " + Records + " blog comments entered into the system.";
            }

Incidentally, if you do have this error and need to get rid of it.. you can either delete the pages that were created programmatically, re-save and publish them (which will set the CreatedBy value to your name) or you can edit tblWorkPage where FKPageID=the new page ID and change the ChangedByName to your username.. any of these will fix it.

Special thanks to Deane Barker for helping to come up with the solution to this problem

Jun 30, 2010

Comments

Sep 21, 2010 10:33 AM

Good blog! Nice work by both you and Deane. :)
/ Jeff Wallace

Please login to comment.
Latest blogs
Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024