【问题标题】:Query custom table in form builder auto responder在表单生成器自动响应器中查询自定义表
【发布时间】:2016-02-21 18:01:20
【问题描述】:

我有一个使用 Kentico 表单生成器构建的自定义表单。在我的表单中,我通过外键引用了一个自定义表......这意味着我的表单存储了对我的自定义表数据的引用的 id。在我的自动响应器中,有什么方法可以使用表单中的 id 值从我的自定义表中检索实际值?

【问题讨论】:

    标签: c# forms macros kentico


    【解决方案1】:

    您可以通过creating a custom macro 执行此操作,该creating a custom macro 接受FK 值作为参数,在您的自定义表中查找相应的记录,并将该记录上您需要的字段作为字符串返回。然后,您只需在自动回复编辑器中使用宏 - 就像在其他任何地方一样 - 并传入 $$label:FieldName$$ 作为参数。

    其中最困难的部分是使用 Kentico 的 API 从自定义表中获取数据。 Here are some examples for managing custom table data using their API。这并不是真的那么难,只是需要比应有的代码多一点。

    就我个人而言,我更喜欢为我的自定义表创建 Entity 或 LINQtoSQL 类并使用它们,特别是如果它是我将要经常使用的表。您真正失去的唯一一件事是对其他 CMS 功能的一些内置调用,例如更新智能搜索索引,我认为将内容记录到暂存模块,如果需要,您可以稍后添加。

    【讨论】:

    • 您可能会在自定义表的代码选项卡上查看 Kentico 自动生成的代码,而不是使用实体或 LINQ。您不仅会丢失 CMS 功能,还会丢失内置缓存。恕我直言,使用 API 是一种更好的方法,因为您可以获得它带来的所有其他好处。使用自定义宏方法的答案很好。
    • @BrendenKehren 我同意,坚持使用 API。 Jerreck re 的好答案:自定义宏,我提供了一些代码来演示该解决方案。
    【解决方案2】:

    Jerreck 是正确的,您可以使用自定义宏来做到这一点。

    如果您要制作自定义宏,我建议您通过传递一些额外的参数来使自定义宏尽可能可重用。例如;

    customTableItemID = 231;    
    customTableCodeName = "customtable.TableName";    
    customTableReturnColumnName = "DisplayName";
    

    至少这样您就可以为其他自定义表重复使用相同的自定义宏并从表中返回不同的字段。

    假设您使用的是 Kentico v8,以下代码适用于我;

    CustomMacroMethods.cs

    using System;
    using CMS.Base;
    using CMS.MacroEngine;
    using CMS.Helpers;
    using CMS.CustomTables;
    
    //declare CustomMacroNamespace
    [Extension(typeof(CustomMacroMethods))]
    public class CustomMacroNamespace : MacroNamespace<CustomMacroNamespace>
    {
    }
    
    //register CustomMacroNamespace into the macro engine
    [MacroNamespaceLoader]
    public partial class CMSModuleLoader
    {
       /// <summary>
       /// Attribute class that ensures the registration of custom macro namespaces.
       /// </summary>
       private class MacroNamespaceLoaderAttribute : CMSLoaderAttribute
       {
          /// <summary>
          /// Called automatically when the application starts.
          /// </summary>
          public override void Init()
          {
             // Registers "CustomNamespace" into the macro engine
              MacroContext.GlobalResolver.SetNamedSourceData("CustomMacroNamespace", CustomMacroNamespace.Instance);
          }
       }
    }
    
    //declare custom macro methods
    public class CustomMacroMethods : MacroMethodContainer
    {
    
       [MacroMethod(typeof(object), "Returns the Value of a Column in a Custom Table as an object.", 3)]
       [MacroMethodParam(0, "CustomTableCodeName", typeof(string), "The Custom Table code name. Eg customtable.TableName")]
       [MacroMethodParam(1, "CustomTableItemID", typeof(int), "The ID of the to return.")]    
       [MacroMethodParam(2, "CustomTableReturnColumnName", typeof(string), "The field name of the column containing the value to return")]
       public static object GetCustomTableValue(EvaluationContext context, params object[] parameters)
      {
        if (parameters.Length == 3)
        {
    
            string customTableCodeName = ValidationHelper.GetString(parameters[0], "");
            int customTableID = ValidationHelper.GetInteger(parameters[1], -1);
            string customTableReturnFieldName = ValidationHelper.GetString(parameters[2], "");
    
            CustomTableItem cti = CustomTableItemProvider.GetItem(customTableID, customTableCodeName);
            if (cti != null)
            {
                return cti.GetValue(customTableReturnFieldName);
            }
            else
            {
                return null;
            }
        }
        else
        {
            throw new NotSupportedException("Custom macro GetCustomTableValue() requires three parameters");
        }
    
      }
    
    }
    

    用法

    {% CustomMacroNamespace.GetCustomTableValue("customtable.TableName", 1, "DisplayName")  #%}
    

    有关详细信息,请参阅文档。 https://docs.kentico.com/display/K8/Registering+custom+macro+methods https://docs.kentico.com/display/K8/Creating+macro+namespaces

    【讨论】:

      猜你喜欢
      • 2016-04-27
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      相关资源
      最近更新 更多