Giter Site home page Giter Site logo

spqueryutil's Introduction

SPQueryUtil - CamlBuilder

SPQUeryUtil now contains CamlBuilder which is designed to build CAML query without any CAML xml, but only code.

To build CAML query by CamlBuilder:

using(SPSite site = new SPSite("http://mysiteurl"))
{
  using(SPWeb web = site.OpenWeb())
  {
    SPList listToQuery = web.Lists["myList"];
    string queryStr = CamlBuilder.BuildQuery(listToQuery, new And(new Eq("Status", "Completed"), new Eq("Id", "2")));
    SPQuery query = new SPQuery();
    query.Query = queryString;
    var items = listToQuery.GetItems(query);
  }
}

1, Support operators and joins (not all currently):

CamlBuilder.BuildQuery(listToQuery, new Eq("status", "Completed"))

generates

<Where>
  <Eq>
    <FieldRef Name="Status"></FieldRef>
    <Value Type="Text">Completed</Value>
  </Eq>
</Where>

2, Combine operators by joins:

CamlBulder.BuildQuery(listToQuery, new And(new Eq("status", "Completed"), new Eq("Id", "2"))

generates:

<Where>
  <And>
    <Eq>
      <FieldRef Name="Status"></FieldRef>
      <Value Type="Text">Completed</Value>
    </Eq>
    <Eq>
      <FieldRef Name="ID"></FieldRef>
      <Value Type="Counter">2</Value>
    </Eq>
  </And>
</Where>

If join contains more than two child elements, join element will be nested automatically:

CamlBulder.BuildQuery(listToQuery, new And(new Eq("status", "Completed"), new Eq("title", "Task1"), new Eq("id", "1"))

generates:

<Where>
  <And>
    <And>
      <Eq>
        <FieldRef Name="Status"></FieldRef>
        <Value Type="Text">Completed</Value>
      </Eq>
      <Eq>
        <FieldRef Name="Title"></FieldRef>
        <Value Type="Text">Task1</Value>
      </Eq>
    </And>
    <Eq>
      <FieldRef Name="ID"></FieldRef>
      <Value Type="Counter">2</Value>
    </Eq>
  </And>
</Where>

3, You need to specify the field title and the value only, for examle: new Eq("status", "Completed"), the field title is "status", which is not case sensitive, and the field name can be either Title or InternalName, CamlBuilder will handle everything to generate a valid CAML for this expression.

spqueryutil's People

Contributors

shrenky avatar

Watchers

 avatar  avatar

Forkers

casparcui

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.