2012-01-12 58 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; 
}