CamlQueryApi 1.0.0

dotnet add package CamlQueryApi --version 1.0.0                
NuGet\Install-Package CamlQueryApi -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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                
#r "nuget: CamlQueryApi, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CamlQueryApi as a Cake Addin
#addin nuget:?package=CamlQueryApi&version=1.0.0

// Install CamlQueryApi as a Cake Tool
#tool nuget:?package=CamlQueryApi&version=1.0.0                

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();

<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();

<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

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 1,683 5/17/2019

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