Login

Can I do a property search using webservices?

Versions: n/a, FAQ number: 207, Old FAQ number: 958

Q: Can I do a property search using webservices?

A: Yes, see example below.


// Include the web references namespace. Here we have named it EPiServer4
// and it references the /WebServices/DataFactoryService.asmx file.
 
using TestApplication.EPiServer4;

// Initialize a DataFactoryService object for webservice calls
private EPiServer4.DataFactoryService _wsFactory =
                          new EPiServer4.DataFactoryService();

// Example function that searches for a substring in the MainBody property.

private RawPage[] SearchPages(int searchStartPageID, string searchText)
{
   PageReference pageRef = new PageReference();

   pageRef.ID = searchStartPageID; 

   PropertyCriteria criteria = new PropertyCriteria();
   criteria.Type = PropertyDataType.LongString;
   criteria.Name = "MainBody";
   criteria.Value = searchText;

   // Search for a substring in a string
   criteria.StringCondition =  StringCompareMethod.Contained;

   // If we want to search for numeric values we would set criteria.Condition
   // instead, for eg. criteria.Condition = CompareCondition.Equal;

   return _wsFactory.FindPagesWithCriteria(
                             pageRef, new PropertyCriteria[]{criteria});
}


private void SearchPages(string searchText)
{       
   RawPage[] matchingPages = SearchPages(30, searchText);

   foreach(RawPage page in pages)
   {  
      Response.Write("{0} - {1}<br>",
            GetPropertyValue(page, "PageLink"),
            GetPropertyValue(page, "PageName"));
   }      
}

// Helper function to get a property's value.     
private string GetPropertyValue(EPiServer4.RawPage rawPage, string propertyName)
{
   try
   {
      return rawPage.Property[GetPropertyIndex(rawPage, propertyName)].Value;
   }
   catch(Exception)
   {
      return String.Empty;
   }
}

// Helper function to find out a property's index.
private int GetPropertyIndex(RawPage rawPage, string propertyName)
{         
   for(int index = 0; index  < rawPage.Property.Length; index++)
      if (rawPage.Property[index].Name == propertyName)
         return index;

   return -1;        
}

List of property names

To find out the details of all searchable meta data, please see the EPiServer.Core.PageData.Property description in the EPiServer4 SDK.

Note: remember to configure your IIS security settings, otherwise you will get access denied errors when you try to access the web services. See the web services technote for more information.
EPiTrace logger