Having an issue with inserting custom table item from a custom Event handler

40 views Asked by At

I am having an issue that I hope someone can help me with. We are using Kentico 12 sp with windows authentication. We have a need to log when a user who belongs to a specific role authenticates. I created a custom event handler for SecurityEvents.Authenticate.Execute.

 if (e.User.IsInRole("bsi-sfr-lg-hcwp_patientroster", "Internal") || e.User.SiteIndependentPrivilegeLevel.Equals(UserPrivilegeLevelEnum.GlobalAdmin))
 {
     string EIN = e.User.UserName.Substring(4, 6);
     U.SamAccountName = EIN;
     PrincipalSearcher searcher = new PrincipalSearcher(U);
     UserPrincipal principal = (UserPrincipal)searcher.FindOne();
     if (principal != null)
     {
         Login = principal.DisplayName;
     }
     else
     {
         Login = "Error getting Name";
     }
     DataClassInfo classInfo = DataClassInfoProvider.GetDataClassInfo(AuthenticationLogTable);
     if (classInfo != null)
     {
         CustomTableItem NewAuthLog = new CustomTableItem(AuthenticationLogTable);
         NewAuthLog.SetValue("Login", e.User.UserName);
         NewAuthLog.SetValue("DisplayName", Login);
         if (e.User.SiteIndependentPrivilegeLevel.Equals(UserPrivilegeLevelEnum.GlobalAdmin))
             NewAuthLog.SetValue("UserRole", "Global Admin");
         else if (e.User.IsInRole("bsi-sfr-lg-hcwp_patientroster", "Internal"))
             NewAuthLog.SetValue("UserRole", "bsi-sfr-lg-hcwp_patientroster");
         // Add additional roles here as an else if
         
         NewAuthLog.Insert();
     }
 }

When it gets to the NewAuthLog.Insert I get the error

Description:at CMS.CustomTables.CustomTableItem.InsertData()
at CMS.DataEngine.AbstractInfoProvider`3.SetInfo(TInfo info)
at CustomAuthenticationHandler.OnAuthentication(Object sender, AuthenticationEventArgs e) in C:\Sites\internal-test.vitalant.org\server\CMS\App_Code\_CustomHandlers\CustomAuthenticationHandler.cs:line 68

I did add an LogEvent entry before trying to insert the record by getting the various fields in the NewAuthLog and they populated the description in the event log Like I expected.

1

There are 1 answers

0
Josh Cossiboon On

Brian - What's the actual error you are getting? You provided only part of the stack trace.

To create a new CustomTableItem instance:

CustomTableItem NewAuthLog = CustomTableItem.New(AuthenticationLogTable);

Also, ensure you add data to all fields requiring data.

https://docs.xperience.io/api12sp/content-management/custom-table-data#Customtabledata-Addingdatarecordstoacustomtable