2

EPiServer only:

How do I search for pages with any value in a given property? I can do a search for pages with a specific value in the property, but I can't figure out how to search for "not empty".

For example, this doesn't work:

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

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

An exception is thrown, "The crieria value cannot be null or empty. Set the IsNull property to search for null."

Any ideas?

Jakob Gade
  • 12,319
  • 15
  • 70
  • 118

4 Answers4

2

Yeah, this is confusing. In case anyone else stumbles on this, here's how to search for pages with a certain PageReference property set to something:

new PropertyCriteria()
{
    createdCriteria.Name = "MyProperty";
    createdCriteria.Type = PropertyDataType.PageReference;
    createdCriteria.Condition = EPiServer.Filters.CompareCondition.NotEqual;
    createdCriteria.Value = "0";
    createdCriteria.IsNull = false;
}
Fredrik Stolpe
  • 881
  • 10
  • 14
1

Unless I'm missing a trick, this doesn't appear to be possible using the EPiServer PropertyCriteriaCollection.

I've had a dig around in reflector and here are my findings. The FPWC method eventually calls EPiServer.DataAccess.PropertySearchDB.FastFindPagesWithCriteria().

Within this method is the following:

    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.");
      }
      ...
    }

So its not possible to search for an empty string value, without setting IsNull to true. This is then fed down to the EPiServer.DataAccess.PropertySearchDB.ExecuteCriteria method, which constructs and formats the DB command. Because IsNull is true, the netPropertySearchNull stored proc is used. Searching for a string needs to use the netPropertySearchString stored proc.

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

My suggestion would be to load the full list of pages and filter using linq. Alternatively you could look into bypassing the API and implementing a direct DB query, or use some of the low level EPiServer data access methods (not recommended)

Apologies for my first answer - I really should test code before posting :)

tompipe
  • 949
  • 6
  • 8
0

I've seen something where the page has a a hidden bool property "Property X contains a value" that is set in the Saving event.

Then that bool property is used as a PropertyCriteria instead of the one you are REALLY interested in.

Johan Kronberg
  • 1,086
  • 7
  • 12
-2

To find empty values you need to specify the IsNull property on the PropertyCriteria and use the Equal compare condition.

E.g

var criterias = newPropertyCriteriaCollection
{
  new PropertyCriteria()
  { 
    Condition = CompareCondition.NotEqual, 
    Name = "MyProperty", 
    IsNull = true, 
    Type = PropertyDataType.String
  }
};
tompipe
  • 949
  • 6
  • 8
  • I need the NON-empty values. The PropertyCriteria you suggested just gives me a collection of all the pages that doesn't have the property set - I need those with the value set. – Jakob Gade Jan 13 '12 at 02:38
  • I made a typo, The CompareCondition should have been NotEqual. I've corrected it. Update - that still doesn't work. Hmmm. – tompipe Jan 16 '12 at 11:41