#region Dynamic caml Qeury class
/// <summary>
/// Create the class to dynamic CAML Query.
/// </summary>
public class CamlQueryElements
{
public string LogicalJoin { get; set; } // like <Or>,
<And>
public string ComparisonOperators { get; set; } // like <Eq>,
<Contains>
public string FieldName { get; set; } // Like Title
public string FieldType { get; set; } // Like Text
public string FieldValue { get; set; } // some value
}
/// <summary>
/// Class name CamlQuery
/// </summary>
public class CamlQuery
{
/// <summary>
/// Passing the Filter field and Filter value in datatable
/// </summary>
/// <param
name="dtFilterQeury"></param>
/// <returns></returns>
public IList<CamlQueryElements> AddElement(DataTable dtFilterQeury)
{
IList<CamlQueryElements> lstOfElement = new List<CamlQueryElements>();
foreach (DataRow dtRow in dtFilterQeury.Rows)
{
lstOfElement.Add(new CamlQueryElements
{
ComparisonOperators = "Eq",
FieldName = Convert.ToString(dtRow["FilterField"]),
FieldType = GetFilterType(Convert.ToString(dtRow["FilterField"])),
FieldValue = Convert.ToString(dtRow["FilterValue"]),
LogicalJoin = "And"
});
}
return lstOfElement;
}
/// <summary>
/// switch statement to get filterText value
/// </summary>
/// <param
name="FilterType">column name</param>
/// <returns></returns>
public string GetFilterType(string FilterType)
{
String FilterText = "Text";
switch (FilterType)
{
case "ID":
return FilterText = "Integer";
case "PriorityLevel":
return FilterText = "Text";
case "AssignTo":
return FilterText = "GroupColumn";
case "ExpectedDueDate":
return FilterText = "DateTime";
case "MatterDefined":
return FilterText = "Text";
case "LinkTitleNoMenu":
return FilterText = "Text";
case "MatterStatus":
return FilterText = "Choice";
default:
return FilterText;
}
}
/// <summary>
/// final funtion of caml Query
/// </summary>
/// <param
name="dtFilterQeury"></param>
/// <returns></returns>
public string GetDynamicQuery(DataTable dtFilterQeury)
{
IList<CamlQueryElements> camlIlist = AddElement(dtFilterQeury);
string dyanmicCamlQuery = GenerateQuery(camlIlist);
return dyanmicCamlQuery;
}
// This
function loop List of camlqueryelments which has our filter criteria
// Then
generate query in required format.
// At
end it return string which holds caml query.
public string GenerateQuery(IList<CamlQueryElements> lstOfElement)
{
StringBuilder queryJoin = new StringBuilder();
string query = @"<{0}><FieldRef
Name='{1}' /><Value {2} Type='{3}'>{4}</Value></{5}>";
if (lstOfElement.Count > 0)
{
int itemCount = 0;
foreach (CamlQueryElements element in lstOfElement)
{
itemCount++;
string date = string.Empty;
// Display only Date
if (String.Compare(element.FieldType,
"DateTime", true) == 0)
date = "IncludeTimeValue='false'";
queryJoin.AppendFormat
(string.Format(query,
element.ComparisonOperators, element.FieldName,
date, element.FieldType,
element.FieldValue, element.ComparisonOperators));
if (itemCount >= 2)
{
queryJoin.Insert(0, string.Format("<{0}>",
element.LogicalJoin));
queryJoin.Append(string.Format("</{0}>",
element.LogicalJoin));
}
}
queryJoin.Insert(0, "<Where>");
queryJoin.Append("</Where>");
}
return queryJoin.ToString();
}
}
#endregion
here are reference url:
http://sharepoint.infoyen.com/2012/03/08/create-dynamic-caml-query-in-sharepoint/
No comments:
Post a Comment