public static ListItemCollection GetMasterDocuments(List <KeyValuePair <string, string> > list) { // When using For Each Need to create initial builder with value CAML.QueryBuilder.CAMLQueryGenericFilter filterAll = new CAML.QueryBuilder.CAMLQueryGenericFilter("ACTIVITY_STATUS", CAML.QueryBuilder.FieldType.Boolean, "1", CAML.QueryBuilder.QueryType.Equal); CAML.QueryBuilder.CAMLQueryBuilder builder = new CAML.QueryBuilder.CAMLQueryBuilder(filterAll); foreach (KeyValuePair <string, string> kvp in list) { if (!string.IsNullOrEmpty(kvp.Value)) { CAML.QueryBuilder.FieldType fieldType = MasterDocument.GetFieldTypeByFieldName(kvp.Key); CAML.QueryBuilder.CAMLQueryFilter filter; switch (fieldType) { case CAML.QueryBuilder.FieldType.Lookup: int intValue = int.MinValue; bool isInt = int.TryParse(kvp.Value, out intValue); if (isInt) { filter = new CAML.QueryBuilder.CAMLQueryLookupFilter(kvp.Key, intValue, CAML.QueryBuilder.QueryType.Equal); builder.ANDFilter(filter); } else { filter = new CAML.QueryBuilder.CAMLQueryLookupFilter(kvp.Key, kvp.Value, CAML.QueryBuilder.QueryType.Equal); builder.ANDFilter(filter); } break; default: filter = new CAML.QueryBuilder.CAMLQueryGenericFilter(kvp.Key, fieldType, kvp.Value, CAML.QueryBuilder.QueryType.Equal); builder.ANDFilter(filter); break; } } } // foreach builder.DocumentFilter(CAML.QueryBuilder.FSObjType.Document, true); builder.AddViewFields(MasterDocument.GetAllFieldNames()); builder.BuildQuery(); builder.OrderBy("Created", false); builder.BuildViewFields(); CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = builder.ToString(); ClientContext ctx = ConnectToSharePoint(); List spList = ctx.Web.Lists.GetByTitle("MasterDocuments"); ctx.Load(spList); ctx.ExecuteQuery(); if (spList != null && spList.ItemCount > 0) { ListItemCollection listItems = spList.GetItems(camlQuery); ctx.Load(listItems); ctx.ExecuteQuery(); // ctx.Dispose(); return(listItems); } else { return(null); } }