Friday, November 2, 2012

How to query two or more columns of SharePoint list with multiple Values.

Here I am going to explain a you a way to create a dynamic query which deals with multiple values of two fields(Columns). There are other way of doing it to do it through LINQ but that would be heavy because you have to use for each loop for the purpose.

I will do it for two list of string values collection for two columns. I am considering AND operation between columns and OR between all values for columns. I will divide into three function for clarity. However I and II are same except its filed name, you can modify it as per your need. III function would be combine both these queries with an AND operator.

1. First we will create dynamic query for Column1:

 public StringBuilder GetQueryForColumn1(List<string> Values1)
        {
            StringBuilder stbQuery = new StringBuilder();
            if (Regions.Count > 0)
            {
                bool first = true;
                    foreach (string Value in Values1)
                    {

                        if (first)
                        {
                            stbQuery.Append("<Eq><FieldRef Name='Column1' /><Value Type='Text'>").Append(Value .Trim()).Append("</Value></Eq>");
                            first = false;
                        }
                        else
                        {
                            string formatedQuery = stbQuery.ToString();
                            StringBuilder objNew = new StringBuilder();
                            objNew.Append("<Or>");
                            objNew.Append("<Eq><FieldRef Name='Column1' /><Value Type='Text'>").Append(Value .Trim()).Append("</Value></Eq>");
                            objNew.Append(formatedQuery);
                            objNew.Append("</Or>");
                            stbQuery = objNew;
                        }
                }
            }
            return stbQuery;
        }


2. Now we will create dynamic query for Column2:

 public StringBuilder GetQueryForColumn2(List<string> Values2)
        {
            StringBuilder stbQuery = new StringBuilder();
            if (Regions.Count > 0)
            {
                bool first = true;
                    foreach (string Value in Values2)
                    {

                        if (first)
                        {
                            stbQuery.Append("<Eq><FieldRef Name='Column2' /><Value Type='Text'>").Append(Value .Trim()).Append("</Value></Eq>");
                            first = false;
                        }
                        else
                        {
                            string formatedQuery = stbQuery.ToString();
                            StringBuilder objNew = new StringBuilder();
                            objNew.Append("<Or>");
                            objNew.Append("<Eq><FieldRef Name='Column2' /><Value Type='Text'>").Append(Value .Trim()).Append("</Value></Eq>");
                            objNew.Append(formatedQuery);
                            objNew.Append("</Or>");
                            stbQuery = objNew;
                        }
                }
            }
            return stbQuery;
        }


3. Then we will put this into a common function and concatenate  it : 

 public StringBuilder GetQueryForBothColumn1AndColumn2(List<string> Regions, List<string> OfficerTitles)
        {
            StringBuilder stbAllQuery = new StringBuilder();
            stbAllQuery.Append("<Where><And>");
            stbAllQuery.Append( GetQueryForColumn1(Values1));
            stbAllQuery.Append( GetQueryForColumn2(Values2));
            stbAllQuery.Append("</And></Where>");
            return stbAllQuery;
        }

 
Now this query you can use to query on this on your chunk of selected choices in a single query to list. you can do it for more than two columns as well but that would need a bit effort and change in  GetQueryForBothColumn1AndColumn2 function.




No comments:

Post a Comment