创建过滤器而不是组过滤器

时间:2014-11-29 04:27:23

标签: orchardcms

我在Orchard中创建自定义模块,我想以编程方式创建查询。

    string  queryName= "Product";
    var item = _orchardServices.ContentManager.New("Query");
    item.As<TitlePart>().Title =queryName;
    _orchardServices.ContentManager.Create(item, VersionOptions.Draft);
    if (!item.Has<IPublishingControlAspect>() && !item.TypeDefinition.Settings.GetModel<ContentTypeSettings>().Draftable)
        _orchardServices.ContentManager.Publish(item);

    var queryPart = item.As<QueryPart>();
    queryPart.ContentItem.ContentType = queryName;
    string desc =" filter for the query";
    string contentType = "CommonPart.ChannelID.";
    var filterGroupRecord = new FilterGroupRecord();
    var filterRecord = new FilterRecord()
    {
        Category = "CommonPartContentFields",
        Type = contentType,
        Position = 0,
    };
    filterRecord.State = "<Form><Description>" + desc + "</Description><Operator>Equals</Operator><Value>ChannelId</Value></Form>";
    filterGroupRecord.Filters.Add(filterRecord);
    queryPart.FilterGroups.Insert(0, filterGroupRecord);

问题在于:我想设置查询的过滤器,而不是过滤器组。 你能告诉我如何改进我的代码吗?

1 个答案:

答案 0 :(得分:0)

数据库结构和类声明使其无法实现。你为什么需要它?

<强>更新

我的意思是你必须至少使用一个FilterGroupRecord。 但是当Query发布时,如果查询还没有Filter Group,则会自动创建Filter Group(请参阅QueryPartHandler)。您应该将过滤器添加到此组。并且不需要创建新组。

var existingFilterGroup = queryPart.FilterGroups[0];
existingFilterGroup.Filters.Add(filterRecord);

更新2:

为了避免draftable查询(以及其他一些潜在问题Orchard CMS: Adding default data to fields and then querying them)的问题,最好将调用Publish方法移到代码的末尾,代码的其他部分应保持不变。如果您总是在不检查IPublishingControlAspect和Draftable的情况下发布查询,那么在您的情况下会更好。