2012-01-12 62 views
2

仅限EPiServer:搜索具有任何属性值的页面

如何在给定属性中搜索具有任何值的页面?我可以在属性中搜索具有特定值的页面,但我无法弄清楚如何搜索“不空”。

例如,这不起作用:

var criterias = newPropertyCriteriaCollection 
{ 
    new PropertyCriteria() 
    { 
    Condition = CompareCondition.NotEqual, 
    Name = "MyProperty", 
    IsNull = false, 
    Type = PropertyDataType.String, 
    Value = "" 
    } 
}; 

var pages = DataFactory.Instance.FindPagesWithCriteria(PageReference.StartPage, criterias); 

抛出一个异常,“该crieria值不能为null或空设置ISNULL属性搜索无效。”

任何想法?

回答

-2

要找到空值,您需要在PropertyCriteria中指定IsNull属性并使用等比较条件。

E.g

var criterias = newPropertyCriteriaCollection 
{ 
    new PropertyCriteria() 
    { 
    Condition = CompareCondition.NotEqual, 
    Name = "MyProperty", 
    IsNull = true, 
    Type = PropertyDataType.String 
    } 
}; 
+0

我需要非空值。你建议的PropertyCriteria只给了我没有属性集合的所有页面的集合 - 我需要那些设置了值的集合。 – 2012-01-13 02:38:43

+0

我犯了一个错字,CompareCondition应该是NotEqual。我纠正了它。 更新 - 仍然不起作用。嗯。 – tompipe 2012-01-16 11:41:49

1

除非我失去了一个把戏,这似乎没有使用EPiServer PropertyCriteriaCollection成为可能。

我已经在反射器周围挖了一圈,这里是我的发现。 FPWC方法最终调用EPiServer.DataAccess.PropertySearchDB.FastFindPagesWithCriteria()。

在这个方法如下:

foreach (PropertyCriteria criteria in criterias) 
    { 
     if (criteria.IsNull) 
     { 
     criteria.Value = null; 
     } 
     else if (string.IsNullOrEmpty(criteria.Value)) 
     { 
     throw new EPiServerException("The crieria value cannot be null or empty. Set the IsNull property to search for null."); 
     } 
     ... 
    } 

因此,它无法查找一个空字符串值,而不ISNULL设置为true。然后将其反馈给EPiServer.DataAccess.PropertySearchDB.ExecuteCriteria方法,该方法构造并格式化DB命令。由于IsNull为true,因此使用netPropertySearchNull存储过程。搜索一个字符串需要使用netPropertySearchString存储过程。

if (criteria.IsNull && !PageDB.IsMetaData(criteria.Name)) 
    { 
    cmd.CommandText = "netPropertySearchNull"; 
    } 

我的建议是加载完整的页面列表和使用linq过滤器。或者,您可以考虑绕过API并实施直接的数据库查询,或者使用一些低级EPiServer数据访问方法(不推荐)

我的第一个答案道歉 - 我真的应该在发布前测试代码:)

0

我已经看到了页面有一个隐藏的布尔属性“Property X contains a value”,在Saving事件中设置了一些东西。

然后将bool属性被用作PropertyCriteria,而不是一个你真正感兴趣的。

1

啊,这是令人困惑的。如果有人绊倒在这,下面是如何搜索页面的某些PageReference属性设置为:

new PropertyCriteria() 
{ 
    createdCriteria.Name = "MyProperty"; 
    createdCriteria.Type = PropertyDataType.PageReference; 
    createdCriteria.Condition = EPiServer.Filters.CompareCondition.NotEqual; 
    createdCriteria.Value = "0"; 
    createdCriteria.IsNull = false; 
}