API Queries

From Sense/Net 6.0 Wiki

Jump to: navigation, search

Contents

[edit] Syntax

The code of creating a query is the following:

NodeQuery query = new NodeQuery();
   // Add Operators and Expressions
   // Add Order expressions (optional)
   // Set Top, PageSize and/or PageNumber properties (optional)
IList<Node> result = query.Execute(); // Execute the query

[edit] Prerequisites

Sense/Net 6.0 supports searching and filtering via the NodeQuery class. The prerequisites of using the node query are:

  1. Reference the Storage.dll in your project
  2. Import (with the "using" keyword) the SenseNet.ContentRepository.Storage.Search namespace in your .cs file

[edit] Expression Types

[edit] Query Expressions

Within an Query the following expressions can be used:

  • TypeExpression specifies the type of a node
  • StringExpression specifies criteria for fields that have String RepositoryDataType (e.g. ShortText and LongText fields)
  • IntExpression specifies criteria for fields which are mapped to Int RepositoryDataType (e.g. Boolean and [[Integer fields)
  • DateTimeExpression specifies criteria for fields which are mapped to Int DateTime (e.g. DateTime field)
  • ReferenceExpression specifies criteria for fields which are mapped to Reference RepositoryDataType (e.g. Reference field)
  • CurrencyExpression specifies criteria for fields which are mapped to Currency RepositoryDataType (e.g. Number field)

[edit] Query Operators

Within an XML Query three operators can be used on expressions. These are:

  • And operator (instanced as new ExpressionList(ChainOperator.OrAnd))
  • Or operator (instanced as new new ExpressionList(ChainOperator.Or))
  • NotExpression

Expression lists and the NotExpression can be nested within one another. Top level operator - even if not marked - is always And.

[edit] Paging and Ordering properties

Paging and ordering can set via properties of the NodeQuery object. These properties are:

  • Orders property - a List<SearchOrder> property where search order can be specified
  • Top property - the query can be instructed to only return the first few elements. If set StartIndex and PageSize have no effect.
  • StartIndex - from where the query should return results
  • PageSize - the number of results to return starting from StartIndex

[edit] Query Expressions

[edit] TypeExpression

TypeExpression have 2 properties that can be set in the constructor:

  • NodeType nodeType" - required, specifies the type of the node that search will be narrowed to. All content types registered in the system can be used. Use the ActiveSchema.NodeTypes[] array to retreive the appropriate NodeType.
  • bool exactMatch" - If false, child types of the node type will be returned as well; otherwise only objects with the exact type. Default value is true.

Examples

Create a type expression to query only Page nodes:

NodeQuery query;
query.Add(new TypeExpression(ActiveSchema.NodeTypes["Page"], true));

Create a type expression to query Folder nodes and their descendants:

NodeQuery query;
query.Add(new TypeExpression(ActiveSchema.NodeTypes["Folder"], false));

[edit] StringExpression

A StringExpression specifies criteria for fields that have String RepositoryDataType.

String expressions have 3 attributes to be set in the constructor:

  • StringAttribute property - the name of the field on which the operation should be executed. This is either a static member of the StringAttribute class (e.g. StringAttribure.Path) or a member of the ActiveSchema.PropertyTypes[] array.
  • StringOperator operator - the operation to be performed on the string, e.g. exact match, not equal
  • string value - the value of the expression to be matched against

Examples

  • Find items within the /Root/IMS path
NodeQuery query;
query.Add(new StringExpression(StringAttribute.Path, StringOperator.StartsWith, "/Root/IMS"));
  • Find items having their Title property containing "King":
NodeQuery query;
query.Add(new StringExpression(ActiveSchema.PropertyTypes["Title"], StringOperator.Contains, "King"));
  • Find items having the name "George":
NodeQuery query;
query.Add(new StringExpression(StringAttribute.Name, StringOperator.Equal, "George"));
  • Find items within the /Root/IMS path which do not have the Administrator name:
NodeQuery query;
query.Add(new StringExpression(StringAttribute.Path, StringOperator.StartsWith, "/Root/IMS"));
query.Add(new NotExpression(new StringExpression(StringAttribute.Name, StringOperator.Equal, "Administrator ")));

[edit] IntExpression

IntExpression specifies criteria for fields that have Int RepositoryDataType.

IntExpressions have 3 attributes to be set in the constructor:

  • IntAttribute property - the name of the field on which the operation should be executed. This is either a static member of the IntAttribute class (e.g. IntAttribure.Id) or a member of the ActiveSchema.PropertyTypes[] array.
  • ValueOperator operator - the operation to be performed on the string, e.g. exact match, not equal
  • int? value - the value of the expression to be matched against

Examples

Find contents that are locked:

NodeQuery query;
query.Add(new IntExpression(IntAttribute.Locked, ValueOperator.Equal, (int?)1));

Find those contents whose "FailedExams" integer property is greater or equal to 2:

NodeQuery query;
query.Add(new IntExpression(ActiveSchema.PropertyTypes["FailedExams"], ValueOperator.GreaterThanOrEqual, (int?)2));

[edit] CurrencyExpression

CurrencyExpression specifies criteria for fields that have Currency RepositoryDataType.

CurrencyExpressions have 3 attributes to be set in the constructor:

  • PropertyType property - the name of the field on which the operation should be executed. This is a member of the ActiveSchema.PropertyTypes[] array.
  • ValueOperator operator - the operation to be performed on the string, e.g. exact match, not equal
  • decimal? value - the value of the expression to be matched against

Examples

Find contents that have a property of TotalSavings greater than or equal to 215.5:

NodeQuery query;
query.Add(new CurrencyExpression(ActiveSchema.PropertyTypes["TotalSavings"], ValueOperator.GreaterThanOrEqual, (decimal?)215.5));

[edit] DateTimeExpression

A DateTimeExpression specifies criteria for fields that have DateTime RepositoryDataType.

String expressions have 3 attributes to be set in the constructor:

  • DateTimeAttributeproperty - the name of the field on which the operation should be executed. This is either a static member of the DateTimeAttributeclass (e.g. StringAttribure.CreationDate) or a member of the ActiveSchema.PropertyTypes[] array.
  • ValueOperator operator - the operation to be performed on the string, e.g. exact match, not equal
  • DateTime? value - the value of the expression to be matched against

Examples

  • Find items which were created within the last 2 days:
NodeQuery query;
query.Add(new DateTimeExpression(DateTimeAttribute.CreationDate, ValueOperator.GreaterThanOrEqual, DateTime.Now.AddDays(-2)));
  • Find items which have a Birth property earler than 1990-01-01:
NodeQuery query;
query.Add(new DateTimeExpression(ActiveSchema.PropertyTypes["Birth"], ValueOperator.LessThanOrEqual, Convert.ToDateTime("1990.01.01."))); // DateTime attributes

[edit] ReferenceExpression

A ReferenceExpression specifies criteria for fields that have Reference RepositoryDataType.

String expressions have 3 attributes to be set in the constructor:

  • PropertyType refererProperty - the name of the reference field. This is a member of the ActiveSchema.PropertyTypes[] array or a member of the ReferenceAttribute enum.
  • Node referencedNode - the node that should be referenced

Examples

  • Find children of a Page
SenseNet.Portal.Page page;
NodeQuery query;
query.Add(new ReferenceExpression(ReferenceAttribute.Parent, page));
  • Find items which have an "Owner" property set to the current user:
NodeQuery query;
query.Add(new ReferenceExpression(ActiveSchema.PropertyTypes["Owner"], (Node)User.Current));
  • Find items which have an "Category" property set to /Root/Categories/MyFirstCategory node
NodeQuery query;
query.Add(new ReferenceExpression(ActiveSchema.PropertyTypes["Category"], Node.Load<Node>("/Root/Categories/MyFirstCategory")));

[edit] Logical Expressions

[edit] And

An And expression contains a set of Expressions that will be evaluated with a logical AND.

Information:
When creating a new NodeQuery and adding expressions via NodeQuery.Add() these top-level experssions are treated as AND expressions.

Examples

And empty And expression:

NodeQuery query;
query.Add(new ExpressionList(ChainOperator.And));

Query for elements within the /Root/IMS folder that are locked:

NodeQuery query;
ExpressionList expressionList = new ExpressionList(ChainOperator.And);
expressionList .Add(new StringExpression(StringAttribute.Path, StringOperator.StartsWith, "/Root/IMS"));
expressionList .Add(new IntExpression(IntAttribute.Locked, ValueOperator.Equal, (int?)1));
query.Add(expressionList);

[edit] Or

An Or expression contains a set of Expressions that will be evaluated with a logical OR.

Examples

And empty Or expression:

NodeQuery query;
query.Add(new ExpressionList(ChainOperator.Or));

Query for elements which have a Name starting with System or Admin

NodeQuery query;
ExpressionList expressionList = new ExpressionList(ChainOperator.Or);
expressionList .Add(new StringExpression(StringAttribute.Name, StringOperator.StartsWith, "System"));
expressionList .Add(new StringExpression(StringAttribute.Name, StringOperator.StartsWith, "Admin"));
query.Add(expressionList);


[To be continued]

[edit] Related Articles

Personal tools