CAML Query Auto-Created in C# with Loops

This is a function in order to create a CAML Query specified for Filters.
Once we have the filters, we’ve to create the parameters for the function, following this template:
Parameters Template

Name1|Type1|Operation1|Value1,Name2|Type2|Operation2|Value2...
[Operation property can only be "Equal" or "Contains", 'cause that's what I need, but it can be modified]

Example

Title|Text|Contains|Document,Category|Choice|Equal|Docs
[Filter items that has "Document" in its title and their category are "Docs"]

Function

[sourcecode language="csharp"]private string createCAMLQuery(string filtersOriginal)
{
  string caml = String.Empty;
  if (filtersOriginal.Length > 0)
  {
    string[] filters = filtersOriginal.Replace("Equal","Eq").Split(',');
    int numberOfFilters = filters.Length;
    caml += "<Where>";
                if (numberOfFilters == 1)
                {
                    caml += "<" + filters[0].ToString().Split('|')[2] + ">";
                    caml += "<FieldRef Name='" + filters[0].ToString().Split('|')[0] + "' />";
                    caml += "<Value Type='" + filters[0].ToString().Split('|')[1] + "'>" + filters[0].ToString().Split('|')[3] + "</Value>";
                    caml += "</" + filters[0].ToString().Split('|')[2] + ">";
                }
                else
                {
                    caml += "<And>";
                    caml += "<" + filters[0].ToString().Split('|')[2] + ">";
                    caml += "<FieldRef Name='" + filters[0].ToString().Split('|')[0] + "' />";
                    caml += "<Value Type='" + filters[0].ToString().Split('|')[1] + "'>" + filters[0].ToString().Split('|')[3] + "</Value>";
                    caml += "</" + filters[0].ToString().Split('|')[2] + ">";
                    caml += "</And>";
                    for (int i = 0; i < numberOfFilters - 1; i++)
                    {
                        caml += "<And>";
                    }
                    caml += "<" + filters[0].ToString().Split('|')[2] + ">";
                    caml += "<FieldRef Name='" + filters[0].ToString().Split('|')[0] + "' />";
                    caml += "<Value Type='" + filters[0].ToString().Split('|')[1] + "'>" + filters[0].ToString().Split('|')[3] + "</Value>";
                    caml += "</" + filters[0].ToString().Split('|')[2] + ">";
                    caml += "<" + filters[1].ToString().Split('|')[2] + ">";
                    caml += "<FieldRef Name='" + filters[1].ToString().Split('|')[0] + ">" + "' />";
                    caml += "<Value Type='" + filters[1].ToString().Split('|')[1] + "'>" + filters[0].ToString().Split('|')[3] + "</Value>";
                    caml += "</" + filters[1].ToString().Split('|')[2] + ">";
                    caml += "</And>";
                    for (int i = 2; i < numberOfFilters; i++)
                    {
                        caml += "<" + filters[i].ToString().Split('|')[2] + ">";
                        caml += "<FieldRef Name='" + filters[i].ToString().Split('|')[0] + "' />";
                        caml += "<Value Type='" + filters[i].ToString().Split('|')[1] + "'>" + filters[0].ToString().Split('|')[3] + "</Value>";
                        caml += "</" + filters[i].ToString().Split('|')[2] + ">";
                        caml += "</And>";
                    }
                }
                caml += "</Where>";
  }
  return caml;
}[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.