Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: How to Programmatically Dynamic CAML Query in SharePoint 2013?
In this post, we will show you how to programmatically dynamic CAML Query in SharePoint 2013. The following code showing you dynamic CAML query generator in SharePoint.When we have to filter more than 2 column, it is very difficult to create CAML query. For example, our application have more than 2 drop down (Bind to SharePoint list column) as search criteria then it is difficult to generate CAML query. To create dynamic CAML query please check this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
class Program { public static string CreateCAMLQuery(List < string > parameters, string orAndCondition, bool isIncludeWhereClause) { StringBuilder sb = new StringBuilder(); if (parameters.Count == 0) { AppendEQ(sb, "all"); } int j = 0; for (int i = 0; i < parameters.Count; i++) { if (!string.IsNullOrEmpty(parameters[i].Split(';')[3])) { AppendEQ(sb, parameters[i]); if (i > 0 && j > 0) { sb.Insert(0, "<" + orAndCondition + ">"); sb.Append("</" + orAndCondition + ">"); } j++; } } if (isIncludeWhereClause) { sb.Insert(0, "<Where>"); sb.Append("</Where>"); } return sb.ToString(); } public static void AppendEQ(StringBuilder sb, string value) { string[] field = value.Split(';'); sb.AppendFormat("<{0}>", field[2].ToString()); sb.AppendFormat("<FieldRef Name='{0}'/>", field[0].ToString()); sb.AppendFormat("<Value Type='{0}'>{1}</Value>", field[1].ToString(), field[3].ToString()); sb.AppendFormat("</{0}>", field[2].ToString()); } static void Main(string[] args) { List < string > objColumns = new List < string > (); // here, format is like: "Column name(internal name of column);Column type; Conditional operator; Search string (input by user) objColumns.Add("Title;Text;Eq;" + "abc"); objColumns.Add("FileLeafRef;Text;Eq;" + "PQR"); objColumns.Add("DropDown;Text;Eq;" + "abc"); objColumns.Add("FileLeafRef1;Text;Eq;" + "PQR"); string query = CreateCAMLQuery(objColumns, "And", true); Console.WriteLine(query); Console.Read(); } } |
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options.
Print article | This entry was posted by Peter on October 4, 2016 at 5:30 am, and is filed under Other Related Post. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |