Тоже самое на русском

Project Description

CAML Query Creator is a set of tools for fast and easy creation of texts CAML- queries via the lambda expression.

Advantages

  • Creating CAML-query without having to know all the details of its syntax
  • Easy to use tool based on lambda expressions
  • Can be used as a farm-level solutions (. NET 3.5), and the SandBox-level decisions, such as Silverlight
  • Can be used directly ( for the field indicated by its name and type ) , and using class field descriptions of your infrastructure components added to this :
    • Enumeration of the types of fields for Sharepoint
    • Base class descriptions list field Sharepoint

Using CamlQueryCreator

Suppose you want to create a CAML-query:

<OrderBy>
   <FieldRef Name='TextField1'/>
   <FieldRef Name='IntegerField'/>
</OrderBy>
<Where>
   <And>
      <And>
         <Eq>
            <FieldRef Name='UserField'/>
            <Value Type='Integer'><UserID Type='Integer'/></Value>
         </Eq>
         <Eq>
            <FieldRef Name='TextField1'/>
            <Value Type='Text'>TextValue1</Value>
         </Eq>
      </And>
      <Or>
         <Eq>
            <FieldRef Name='IntegerField'/>
            <Value Type='Integer'>100</Value>
         </Eq>
         <Eq>
            <FieldRef Name='TextField2'/>
            <Value Type='Text'>TextValue2</Value>
         </Eq>
      </Or>
   </And>
</Where>


ie, knowing the names of the fields,
    const string userFieldName = "UserField";
    const string textFieldName1 = "TextField1";
    const string textFieldName2 = "TextField2";
    const string integerFieldName = "IntegerField";
as well as their types, we want to get here the following line:
<OrderBy><FieldRef Name='TextField1'/><FieldRef Name='IntegerField'/></OrderBy><Where><And><And><Eq><FieldRef Name='UserField'/><Value Type='Integer'><UserID Type='Integer'/></Value></Eq><Eq><FieldRef Name='TextField1'/><Value Type='Text'>TextValue1</Value></Eq></And><Or><Eq><FieldRef Name='IntegerField'/><Value Type='Integer'>100</Value></Eq><Eq><FieldRef Name='TextField2'/><Value Type='Text'>TextValue2</Value></Eq></Or></And></Where>


Connect to our project the appropriate assembly:
  • If the decision is the farm level, connect CamlQueryCreator.Net35
  • If it's SandBox-solution, connect CamlQueryCreator.Portable

We obtain the result:
return Caml.GetQuery(
    b => b.Where()
          .And(i => i.And(j => j.CurrentUserEq(userFieldName),
                          j => j.Eq(textFieldName1, FieldTypeKind.Text, textValue1)),
               i => i.Or(j => j.Eq(integerFieldName, FieldTypeKind.Integer, integerValue),
                         j => j.Eq(textFieldName2, FieldTypeKind.Text, textValue2))),
    b => b.OrderBy(textFieldName1)
          .AndOrderBy(integerFieldName));

All together will look like this:
public static string GetQuery()
{
    const string userFieldName = "UserField";
    const string textFieldName1 = "TextField1";
    const string textFieldName2 = "TextField2";
    const string integerFieldName = "IntegerField";

    const string textValue1 = "TextValue1";
    const string textValue2 = "TextValue2";
    const int integerValue = 100;

    return Caml.GetQuery(
        b => b.Where()
              .And(i => i.And(j => j.CurrentUserEq(userFieldName),
                              j => j.Eq(textFieldName1, FieldTypeKind.Text, textValue1)),
                   i => i.Or(j => j.Eq(integerFieldName, FieldTypeKind.Integer, integerValue),
                             j => j.Eq(textFieldName2, FieldTypeKind.Text, textValue2))),
        b => b.OrderBy(textFieldName1)
              .AndOrderBy(integerFieldName));
}

Now consider the case when the project already exist classes describing field lists Sharepoint. To query creator could use them, these classes must be inherited from ListFieldInfoBase, which contains the internal name of the field and its type. In our example, this class will have only one:
public class ListFieldInfo : ListFieldInfoBase
{
    public ListFieldInfo(string name, FieldTypeKind fieldType): base(name, fieldType)
    {
    }
}

Descriptions of the fields in this example will be here are:
var userFieldInfo = new ListFieldInfo(userFieldName, FieldTypeKind.User);
var textFieldInfo1 = new ListFieldInfo(textFieldName1, FieldTypeKind.Text);
var textFieldInfo2 = new ListFieldInfo(textFieldName2, FieldTypeKind.Text);
var integerFieldInfo = new ListFieldInfo(integerFieldName, FieldTypeKind.Integer);

In this case, our request is based is even simpler:
return Caml.GetQuery(
    b => b.Where()
          .And(i => i.And(j => j.CurrentUserEq(userFieldInfo),
                          j => j.Eq(textFieldInfo1, textValue1)),
               i => i.Or(j => j.Eq(integerFieldInfo, integerValue),
                         j => j.Eq(textFieldInfo2, textValue2))),
    b => b.OrderBy(textFieldInfo1)
          .AndOrderBy(integerFieldInfo));

All together will look like this:
public static string GetQuery()
{
    const string userFieldName = "UserField";
    const string textFieldName1 = "TextField1";
    const string textFieldName2 = "TextField2";
    const string integerFieldName = "IntegerField";

    const string textValue1 = "TextValue1";
    const string textValue2 = "TextValue2";
    const int integerValue = 100;

    var userFieldInfo = new ListFieldInfo(userFieldName, FieldTypeKind.User);
    var textFieldInfo1 = new ListFieldInfo(textFieldName1, FieldTypeKind.Text);
    var textFieldInfo2 = new ListFieldInfo(textFieldName2, FieldTypeKind.Text);
    var integerFieldInfo = new ListFieldInfo(integerFieldName, FieldTypeKind.Integer);

    return Caml.GetQuery(
        b => b.Where()
              .And(i => i.And(j => j.CurrentUserEq(userFieldInfo),
                              j => j.Eq(textFieldInfo1, textValue1)),
                   i => i.Or(j => j.Eq(integerFieldInfo, integerValue),
                             j => j.Eq(textFieldInfo2, textValue2))),
        b => b.OrderBy(textFieldInfo1)
              .AndOrderBy(integerFieldInfo));
}

Last edited Jan 5, 2014 at 8:08 AM by AVBaklan, version 16