HXH
HXH
Jun 30, 2010
  5395
(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
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024