CamlQueryApi 1.0.0

Class library with rules to make easy XML CAML Query based on POCO entity classes correctly Caml Query Xml schema

Install-Package CamlQueryApi -Version 1.0.0
dotnet add package CamlQueryApi --version 1.0.0
<PackageReference Include="CamlQueryApi" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CamlQueryApi --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

C# Caml Query Api

The focus with this API is not leak memory with string concat portions and generates with performance and givest simplest way to CAML Query String result only through an instance of a POCO (Plain Old CLR Object) class using native .NET System.Xml.Serialization namespace; this Api strictly follows the Query Schema of CAML documented at Microsoft docs.

To test and preview Caml Query Strings generation results by this Api, not needed download SharePoint Client Components SDK and the developer may reference this as a Assembly in your Visual Studio C# SharePoint projects like Farm Solution or SharePoint Add-ins Provider-Hosted.

But remeber: if you're work with Farm Solution on different SharePoint versions, switch/change .Net Framework version and using x64 build.

Follow the simple codes examples bellow to test and validate:
//This object means an record selection on
//SharePoint List by List Column Name and Value,
//with Farm Solution integration (SSOM)...
Query camlQueryObjSSOM = new Query()
{
    Where = new Where()
    {
        Eq = new Eq()
        {
            FieldRef = new FieldRef()
            {
                Name = "ColumnName"
            },
            Value = new Value()
            {
                Type = ValueType.Text,
                TextValue = "Column Text Value"
            }
        }
    }
};

//and catch the string result
string camlQueryStrSSOM = camlQueryObjSSOM.ToString();
<!--The result of camlQueryStrSSOM variable follow bellow.
Pear attention on XML result, the 'Query' node is removed by API.-->
<Where>
  <Eq>
    <FieldRef Name="ColumnName" Ascending="false" Explicit="false" LookupId="false" TextOnly="false" />
    <Value Type="Text" IncludeTimeValue="false">Column Text Value</Value>
  </Eq>
</Where>
//Use on Microsoft SharePoint Server Object Model (by 'Microsoft.SharePoint' directive):
SPQuery spQuery = new SPQuery()
{
   Query = camlQueryStrSSOM
};
//But at CSOM, the developer need to instantiate the View Object, like this:
View camlQueryObjCSOM = new View()
{
  Query = new Query()
  {
    Where = new Where()
    {
      Eq = new Eq()
      {
        FieldRef = new FieldRef()
        {
        	Name = "ColumnName"
        },
        Value = new Value()
        {
        	Type = ValueType.Text,
        	TextValue = "Column Text Value"
        }
      }
    }
  }
};

//Catch the string result...
string camlQueryStrCSOM = camlQueryObjCSOM.ToString();
<!--The result of camlQueryStrCSOM variable is:-->
<View>
  <Query>
    <Where>
      <Eq>
        <FieldRef Name="ColumnName" Ascending="false" Explicit="false" LookupId="false" TextOnly="false" />
        <Value Type="Text" IncludeTimeValue="false">Column Text Value</Value>
      </Eq>
    </Where>
  </Query>
</View>
//... and use on Microsoft SharePoint Client Object Model (by 'Microsoft.SharePoint.Client' directive):
CamlQuery spQuery = new CamlQuery()
{
   ViewXml = camlQueryStrCSOM
};
At these codes bellow, we have example of Comparison Operators:
Query comparisonOp = new Query()
{
    Where = new Where()
    {
        IsNull = new IsNull()
        {
            FieldRef = new FieldRef()
            {
                Name = "ComlumnName"
            }
        }
    }
};

Query comparisonOp = new Query()
{
    Where = new Where()
    {
        IsNotNull = new IsNotNull()
        {
            FieldRef = new FieldRef()
            {
                Name = "ComlumnName"
            }
        }
    }
};
Now, this more complex code will query a Calendar List, with Equality and Logical Operators (at this case, follow the CAML Query specifications about use until twice Equality Operators inner Logical Operators):
using SPUtility = Microsoft.SharePoint.Utilities.SPUtility;

int currentYear = DateTime.Now.Year;

DateTime startDateFxDT = new DateTime(currentYear, 1, 1),
         endDatFxDT = new DateTime(currentYear, 12, 31);

string startDateFx = SPUtility.CreateISO8601DateTimeFromSystemDateTime(startDateFxDT),
         endDateFx = SPUtility.CreateISO8601DateTimeFromSystemDateTime(endDatFxDT);

Query query = new Query()
{
    Where = new Where()
    {
        And = new And[1]
    }
};

query.Where.And[0] = new And()
{
    Geq = new Geq[1],
    Leq = new Leq[1]
};

query.Where.And[0].Geq[0] = new Geq()
{
    FieldRef = new FieldRef()
    {
        Name = "EventDate"
    },
    Value = new Value()
    {
        IncludeTimeValue = false,
        Type = CamlQueryApi.ValueType.DateTime,
        TextValue = startDateFx
    }
};

query.Where.And[0].Leq[0] = new Leq()
{
    FieldRef = new FieldRef()
    {
        Name = "EventDate"
    },
    Value = new Value()
    {
        IncludeTimeValue = false,
        Type = CamlQueryApi.ValueType.DateTime,
        TextValue = endDateFx
    }
}
Using OrderBy statement:
Query result = new Query
{
    Where = new Where()
    {
        IsNull = new IsNull()
        {
            FieldRef = new FieldRef()
            {
                Name = "ColumnName"
            }
        }
    },
    OrderBy = new OrderBy()
    {
        FieldRef = new FieldRef[1]
    }
};
result.OrderBy.FieldRef[0] = new FieldRef()
{
    Name = "AnotherColumnName",
    Ascending = true
};

License

View MIT license

C# Caml Query Api

The focus with this API is not leak memory with string concat portions and generates with performance and givest simplest way to CAML Query String result only through an instance of a POCO (Plain Old CLR Object) class using native .NET System.Xml.Serialization namespace; this Api strictly follows the Query Schema of CAML documented at Microsoft docs.

To test and preview Caml Query Strings generation results by this Api, not needed download SharePoint Client Components SDK and the developer may reference this as a Assembly in your Visual Studio C# SharePoint projects like Farm Solution or SharePoint Add-ins Provider-Hosted.

But remeber: if you're work with Farm Solution on different SharePoint versions, switch/change .Net Framework version and using x64 build.

Follow the simple codes examples bellow to test and validate:
//This object means an record selection on
//SharePoint List by List Column Name and Value,
//with Farm Solution integration (SSOM)...
Query camlQueryObjSSOM = new Query()
{
    Where = new Where()
    {
        Eq = new Eq()
        {
            FieldRef = new FieldRef()
            {
                Name = "ColumnName"
            },
            Value = new Value()
            {
                Type = ValueType.Text,
                TextValue = "Column Text Value"
            }
        }
    }
};

//and catch the string result
string camlQueryStrSSOM = camlQueryObjSSOM.ToString();
<!--The result of camlQueryStrSSOM variable follow bellow.
Pear attention on XML result, the 'Query' node is removed by API.-->
<Where>
  <Eq>
    <FieldRef Name="ColumnName" Ascending="false" Explicit="false" LookupId="false" TextOnly="false" />
    <Value Type="Text" IncludeTimeValue="false">Column Text Value</Value>
  </Eq>
</Where>
//Use on Microsoft SharePoint Server Object Model (by 'Microsoft.SharePoint' directive):
SPQuery spQuery = new SPQuery()
{
   Query = camlQueryStrSSOM
};
//But at CSOM, the developer need to instantiate the View Object, like this:
View camlQueryObjCSOM = new View()
{
  Query = new Query()
  {
    Where = new Where()
    {
      Eq = new Eq()
      {
        FieldRef = new FieldRef()
        {
        	Name = "ColumnName"
        },
        Value = new Value()
        {
        	Type = ValueType.Text,
        	TextValue = "Column Text Value"
        }
      }
    }
  }
};

//Catch the string result...
string camlQueryStrCSOM = camlQueryObjCSOM.ToString();
<!--The result of camlQueryStrCSOM variable is:-->
<View>
  <Query>
    <Where>
      <Eq>
        <FieldRef Name="ColumnName" Ascending="false" Explicit="false" LookupId="false" TextOnly="false" />
        <Value Type="Text" IncludeTimeValue="false">Column Text Value</Value>
      </Eq>
    </Where>
  </Query>
</View>
//... and use on Microsoft SharePoint Client Object Model (by 'Microsoft.SharePoint.Client' directive):
CamlQuery spQuery = new CamlQuery()
{
   ViewXml = camlQueryStrCSOM
};
At these codes bellow, we have example of Comparison Operators:
Query comparisonOp = new Query()
{
    Where = new Where()
    {
        IsNull = new IsNull()
        {
            FieldRef = new FieldRef()
            {
                Name = "ComlumnName"
            }
        }
    }
};

Query comparisonOp = new Query()
{
    Where = new Where()
    {
        IsNotNull = new IsNotNull()
        {
            FieldRef = new FieldRef()
            {
                Name = "ComlumnName"
            }
        }
    }
};
Now, this more complex code will query a Calendar List, with Equality and Logical Operators (at this case, follow the CAML Query specifications about use until twice Equality Operators inner Logical Operators):
using SPUtility = Microsoft.SharePoint.Utilities.SPUtility;

int currentYear = DateTime.Now.Year;

DateTime startDateFxDT = new DateTime(currentYear, 1, 1),
         endDatFxDT = new DateTime(currentYear, 12, 31);

string startDateFx = SPUtility.CreateISO8601DateTimeFromSystemDateTime(startDateFxDT),
         endDateFx = SPUtility.CreateISO8601DateTimeFromSystemDateTime(endDatFxDT);

Query query = new Query()
{
    Where = new Where()
    {
        And = new And[1]
    }
};

query.Where.And[0] = new And()
{
    Geq = new Geq[1],
    Leq = new Leq[1]
};

query.Where.And[0].Geq[0] = new Geq()
{
    FieldRef = new FieldRef()
    {
        Name = "EventDate"
    },
    Value = new Value()
    {
        IncludeTimeValue = false,
        Type = CamlQueryApi.ValueType.DateTime,
        TextValue = startDateFx
    }
};

query.Where.And[0].Leq[0] = new Leq()
{
    FieldRef = new FieldRef()
    {
        Name = "EventDate"
    },
    Value = new Value()
    {
        IncludeTimeValue = false,
        Type = CamlQueryApi.ValueType.DateTime,
        TextValue = endDateFx
    }
}
Using OrderBy statement:
Query result = new Query
{
    Where = new Where()
    {
        IsNull = new IsNull()
        {
            FieldRef = new FieldRef()
            {
                Name = "ColumnName"
            }
        }
    },
    OrderBy = new OrderBy()
    {
        FieldRef = new FieldRef[1]
    }
};
result.OrderBy.FieldRef[0] = new FieldRef()
{
    Name = "AnotherColumnName",
    Ascending = true
};

License

View MIT license

Release Notes

CAML Query String result through an instance of a POCO (Plain Old C# Object) class, following CAML Query xml schema specifications.

Dependencies

This package has no dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 92 5/17/2019