EPiWiki.se  - EPiServer notes shared with others
 

Searching in EPiServer CMS ObjectStore

[Edit]
The search functionality in EPiServer CMS ObjectStore is not finished yet, but here is an example how it is exposed to work.

***This functionality is not documented and probably not supported by EPiServer***


Create an indexed field in the class


Add the fields number to the item and mark it with the Indexed attribute.

[Serializable]
public class MyItem2 : IItem
{
    [Indexed(true)]
    private int number;
    public int Number
    {
        get { return number; }
        set {number = value;}
    }

    public string Title { get; set; }

    #region IItem Members
    private Guid _id;
    public object Id
    {
        get
        {
            if (_id == Guid.Empty)
            {
                _id = Guid.NewGuid();
            }

            return _id;
        }
    }

    public string Name
    {
        get { return String.Format("MyItem2_{0}", Id); }
    }

    #endregion
}

Register the new schema



static TestPage()
{
   if (EPiServer.BaseLibrary.Context.Repository.SchemaForType(typeof(MyItem2)) == null)
    {
        TypeSchemaBuilder.RegisterSchemaAndType("EPiServer.Templates.MyItem2", typeof(MyItem2));
    }
}

Search for values


The search functionality is executed by the functions:
session.ExecuteQueryObject<MyItem2>(Query query)
to get a list of realized items matching the search criteria
or
session.ExecuteQueryId (Query query)
to get a list of identities of items matching the search criteria.


protected void Page_Load(object sender, EventArgs e)
{
    ISession session = null;
    try
    {
        session = EPiServer.BaseLibrary.Context.Repository.CreateSession();

        //Create/load a root item
        IItem rootItem = session.LoadPath(Root);
        if (rootItem == null)
        {
            rootItem = session.CreatePath(Root);
        }

        MyItem2 newItem = new MyItem2();
        newItem.Number = new Random().Next(10);
        newItem.Title = String.Format("Created: {0}", DateTime.Now);
        //Save a new Item
        session.Save(newItem);
        //Connect it to the root path
        session.AddRelation(rootItem.Id, newItem.Id);

        //List saved items
        foreach (MyItem2 item in session.RelatedItemsFrom<MyItem2>(rootItem.Id))
        {
            Response.Write(item.Title + " " + item.Number);
            Response.Write("<br/>");
        }
        
        //Search for items with number betwwen or equal to 5
        foreach (MyItem2 item in session.ExecuteQueryObject<MyItem2>(
                                           new Query().Add(Expression.Between(
                                                           "number",
                                                           0, 5))))
        {
            Response.Write(item.Title);
            Response.Write(String.Format(": {0} is between 0 and 5 <br/>", item.Number));
        }
    }
    finally
    {
        if (session != null)
        {
            session.Close();
        }
    }
}

Version author:
Mattias Lövström

EPiServer version

All