Accessing Nested Properties in the Kentico Custom Object Macro Method

I am trying to write a single macro method in Kentico (v8.2.x or v9.0) that caches accordingly and provides POCO with several public members.

Debugging in Visual Studio, I see that the request is working fine, and the object instance is returned exactly the way I want. In addition, checking the cached items using the Kentico Debug application also shows that all POCO instance data is cached as expected.

However, when I call the macro, I am apparently able to read the string representation of the object.

This is a macro that extends the type of CurrentUserInfo , so I'm trying to call it like this:

 {% CurrentUser.ClientStatus() %} 

But attempts to extract any of the attached properties failed.

I am sure that I just could not do something (for example, correctly register these properties). But from the documentation I saw a lot of things that could be. For instance:

  • Named source
  • Named Callback Source
  • Anonymous source
  • Or register them as separate fields somehow

Here is the macro itself:

 /// <summary> /// A class containing custom user-extension macros. /// </summary> [assembly: RegisterExtension(typeof(CustomUserMacros), typeof(CurrentUserInfo))] public class CustomUserMacros : MacroMethodContainer { /// <summary> /// Retrieves data regarding user client. /// </summary> /// <param name="context">The context.</param> /// <param name="parameters">The parameters.</param> /// <returns>Data regarding user client information.</returns> [MacroMethod(typeof(ClientInfo), "Retrieves client info.", 1)] [MacroMethodParam(0, "user", typeof(CurrentUserInfo), "The user.")] public static object ClientStatus( EvaluationContext context, params object[] parameters) { ClientInfo retVal = null; if (parameters != null && parameters.Length > 0 && parameters[0].GetType() == typeof(CurrentUserInfo)) { var siteName = SiteContext.CurrentSiteName; var userGuid = ((CurrentUserInfo)parameters[0]).UserGUID; var uInfo = UserInfoProvider.GetUserInfoByGUID(userGuid); retVal = CacheHelper.Cache( cs => new ClientInfo(uInfo, siteName), new CacheSettings( 60, typeof(CustomUserMacros), "ClientStatus", userGuid)); } return retVal; } } 

And the ClientInfo class is pretty simple:

 public class ClientInfo { public string Summary { get; private set; } public CustomTableItem ClientRecord { get; private set; } public IEnumerable<string> MediaPaths { get; private set; } public ClientInfo(UserInfo userInfo, string siteCodeName) { // ... // Set properties, etc... } public override string ToString() { return Summary; } } 

What is the easiest way for me to access properties like the following?

 {% CurrentUser.ClientStatus().ClientRecord["< Column Name >"] %} 
+5
source share
3 answers

To have access to the properties of the method described by you, the parent object must be of the DataRow or DataRowView or implement one of the following interfaces: IVirtualHierarchicalObject , IHierarchicalObject , IDataContainer , ISimpleDataContainer .

In your case, ClientInfo should implement IDataContainer . The nested CustomTableItem already implements one of the interfaces, since it inherits from AbstractInfo .

You will need to implement several elements that mainly work with the columnName parameter, which identifies the element whose value you should return:

 public class ClientInfo : IDataContainer { ... public object GetValue(string columnName) { switch (columnName) { case "ClientRecord": return ClientRecord; case "Summary": return Summary; case "MediaPaths": return MediaPaths; } return null; } ... } 
+5
source

It probably wouldn't work in this case, but it's worth noting that you can return simple object values ​​using CMS.Base.DataContainer or IEnumerable<CMS.Base.DataContainer> :

 return myCollection.Select(myObj => new DataContainer { ["Value1"] = myObj.Value1, ["Value2"] = myObj.Value2 }); 
0
source

First of all, I would recommend that you use System -> Macros -> Console to test your macro - this is very useful. In your case, you should have access to the following properties:

 {% CurrentUser.ClientStatus().Summary %} 
-2
source

Source: https://habr.com/ru/post/1242079/


All Articles