简体   繁体   English

从分类中获取Ektron 9中的所有SmartForm项目

[英]Get all SmartForm items from Ektron 9 in a Taxonomy

I'm using Ektron CMS version 9.0 我正在使用Ektron CMS 9.0版

I have smart form content which is allocated to taxonomies eg I might have five smart form content items (all of same) type allocated to a taxonomy, and another three to a different taxonomy: 我有智能表单内容,分配给分类法,例如,我可能有五个智能表单内容项(全部相同)分配给分类法,另外三个分配给不同的分类法:

I need to get all content of a smart form type from a taxonomy: 我需要从分类法中获取智能表单类型的所有内容:

public IEnumerable<T> GetListOfSmartFormFromTaxonomy<T>(long taxonomyId, bool isRecursive) where T : class
{
  // TODO
}

What I have working, based on links below, is this: 根据以下链接,我的工作是:

public IEnumerable<TaxonomyItemData> GetListOfSmartFormFromTaxonomy(long taxonomyId)
{
    TaxonomyItemCriteria criteria = new TaxonomyItemCriteria();
    criteria.AddFilter(TaxonomyItemProperty.TaxonomyId, CriteriaFilterOperator.EqualTo, taxonomyId);

    TaxonomyItemManager taxonomyItemManager = new TaxonomyItemManager();
    List<TaxonomyItemData> taxonomyItemList = taxonomyItemManager.GetList(criteria);

    return taxonomyItemList;
}

But this just gets the item's titles and ids, not the smart form data itself. 但这只是获取项目的标题和ID,而不是智能表单数据本身。

As an Ektron newbie, I don't know how to get all the items of one Smart Form type using only one call (instead of looping through each item and fetching it by ID which is not efficient) 作为一个Ektron新手,我不知道如何只使用一个调用来获取一个Smart Form类型的所有项目(而不是循环遍历每个项目并通过ID获取它是无效的)

What have I missed? 我错过了什么? I am working on this actively today and will post my findings here. 我今天积极致力于此,并将在此发表我的发现。

References used so far: 目前使用的参考文献:

EDIT 编辑

Posted my just-got-it-working solution below as an fyi and awarded closest answer as accepted. 将我刚刚获得的工作解决方案发布为fyi,并将最接近的答案视为已接受。 Thanks everyone for your help. 谢谢大家的帮助。 Please chime in with any improvements ;) 请提出任何改进;)

I'd recommend using the ContentTaxonomyCriteria with the ContentManager. 我建议将ContentTaxonomyCriteria与ContentManager一起使用。

long smartFormId = 42;
long taxonomyId = 127;
bool isRecursive = true;

var cm = new ContentManager();

var taxonomyCriteria = new ContentTaxonomyCriteria();
taxonomyCriteria.AddFilter(ContentProperty.XmlConfigurationId, CriteriaFilterOperator.EqualTo, smartFormId);
taxonomyCriteria.AddFilter(taxonomyId, isRecursive);

var content = cm.GetList(taxonomyCriteria);

UPDATE UPDATE

The ContentData object has a property called XmlConfiguration . ContentData对象具有一个名为XmlConfiguration的属性。 When the content is based on a smartform, this property will be non-null and have a positive (non-zero) Id: content[0].XmlConfiguration.Id for example. 当内容基于smartform时,此属性将为非null并且具有正(非零)Id: content[0].XmlConfiguration.Id

I often add an Extension Method to my code that will tell me whether a given ContentData is based on a smart form: 我经常在我的代码中添加一个扩展方法,告诉我给定的ContentData是否基于智能表单:

public static class ContentDataExtensions
{
    public static bool IsSmartFormContent(this ContentData content)
    {
        return content != null && content.XmlConfiguration != null && content.XmlConfiguration.Id > 0;
    }
}

That way I can take a content (or list of content) and check it very quickly in code to see if it's based on a smartform or not: 这样我就可以获取内容(或内容列表)并在代码中快速检查它,看它是否基于smartform:

foreach (var contentData in contentList)
{
    if (contentData.IsSmartFormContent())
    {
        // Do smart-form stuff here...
    }
}

Of course, if your content is coming from the framework api and you used a criteria object that is selecting based on a specific XmlConfigurationId, then in theory you wouldn't have to use that, but it still comes in handy quite often. 当然,如果您的内容来自框架api,并且您使用的是基于特定XmlConfigurationId选择的条件对象,那么理论上您不必使用它,但它仍然会经常派上用场。

I'm not quite sure I understand your organizational structure, but you do have the ability to do your own sub clauses that select directly against the database. 我不太确定我理解你的组织结构,但你确实有能力做你自己的直接针对数据库选择的子句。

In this case I wouldn't use the TaxonomyItemManager, I would use the ContentManager with a special criteria: 在这种情况下,我不会使用TaxonomyItemManager,我会使用具有特殊条件的ContentManager:

ContentManager cApi = new ContentManager();
var criteria = new ContentCriteria();
criteria.AddFilter(ContentProperty.Id, CriteriaFilterOperator.InSubClause, "select taxonomy_item_id where taxonomy_id = " + taxonomyId);
criteria.AddFilter(ContentProperty.XmlConfigurationId, CriteriaFilterOperator.EqualTo, smartformTypeId);
var yourContent = cApi.GetList(criteria);

That should do what you're asking for (grab the content specifically that is a member of a Taxonomy while only being of a specific SmartForm config). 这应该做你要求的(特别是抓住具有分类法成员的内容而只是特定的SmartForm配置)。 It's worth noting you don't need the second criteria piece (XmlConfigurationId) if your Taxonomy only contains that XmlConfiguration. 值得注意的是,如果您的Taxonomy仅包含该XmlConfiguration,则不需要第二个条件(XmlConfigurationId)。

For Information, this is what I came up with. 对于信息,这是我想出的。 Noted Brian Oliver's comment on List but using patterns from other devs, can refactor later. 注意到Brian Oliver对List的评论,但使用其他开发者的模式,可以稍后重构。

To clarify, we are creating classes from the XSDs generated from the smart forms, so have smart form types to play with. 为了澄清,我们正在从智能表单生成的XSD中创建类,因此可以使用智能表单类型。 Your use may be simpler that ours. 您的使用可能比我们的更简单。

public IEnumerable<T> GetListOfSmartFormFromTaxonomy<T>(long taxonomyId, bool isRecursive = false) where T : class
{
    long smartFormId = GetSmartFormIdFromType(typeof(T));
    // checks here for smartformid=0

    ContentManager contentManager = new ContentManager();
    ContentTaxonomyCriteria criteria = new ContentTaxonomyCriteria();

    // Smart Form Type
    criteria.AddFilter(ContentProperty.XmlConfigurationId, CriteriaFilterOperator.EqualTo, smartFormId);

    // Taxonomy
    criteria.AddFilter(taxonomyId, isRecursive);

    List<ContentData> contentDataList = contentManager.GetList(criteria);
    IEnumerable<T> smartFormList = ConvertToSmartFormList<T>(pressReleaseDataList);

    return smartFormList;
}

private IEnumerable<T> ConvertToSmartFormList<T>(List<ContentData> contentDataList) where T : class
{
    List<T> smartFormList = new List<T>();
    if (contentDataList != null && contentDataList.Count > 0)
    {
        foreach (ContentData contentData in contentDataList)
        {
            if (contentData.IsSmartFormContent())
            {
                T smartForm = GetDeserializedContent<T>(contentData.Html);
                if (smartForm != null)
                {
                    PropertyInfo property = smartForm.GetType().GetProperty("ContentId");
                    if (property != null)
                    {
                        property.SetValue(smartForm, contentData.Id, null);
                    }

                    smartFormList.Add(smartForm);
                }
            }
        }
    }

    return smartFormList;
}

private long GetSmartFormIdFromType(Type smartFormType)
{
    SmartFormConfigurationManager manager = new SmartFormConfigurationManager();
    SmartFormConfigurationCriteria criteria = new SmartFormConfigurationCriteria();

    // Note: Smart Form Title must match the type's name, i.e. no spaces, for this to work
    criteria.AddFilter(SmartFormConfigurationProperty.Title, CriteriaFilterOperator.EqualTo, smartFormType.Name);

    List<SmartFormConfigurationData> configurationData = manager.GetList(criteria);

    if (configurationData == null || configurationData.Count == 0)
    {
        return 0;
    }

    return configurationData.First().Id;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM