private string CamlFilterBuilder(SearchQueryFilter queryFilter) { string filter = ""; if (queryFilter.Op == FilterOperator.BeginsWith) { filter = $"<BeginsWith><FieldRef Name=\"{queryFilter.Fieldname}\"/>{CamlFilterValueBuilder(queryFilter.Fieldname, queryFilter.Value.ToString())}</BeginsWith>"; } else if (queryFilter.Op == FilterOperator.Contains) { filter = $"<Contains><FieldRef Name=\"{queryFilter.Fieldname}\"/>{CamlFilterValueBuilder(queryFilter.Fieldname, queryFilter.Value.ToString())}</Contains>"; } else if (queryFilter.Op == FilterOperator.Equals) { filter = $"<Eq><FieldRef Name=\"{queryFilter.Fieldname}\"/>{CamlFilterValueBuilder(queryFilter.Fieldname, queryFilter.Value.ToString())}</Eq>"; } else if (queryFilter.Op == FilterOperator.NotEqual) { filter = $"<Neq><FieldRef Name=\"{queryFilter.Fieldname}\"/>{CamlFilterValueBuilder(queryFilter.Fieldname, queryFilter.Value.ToString())}</Neq>"; } else if (queryFilter.Op == FilterOperator.GreaterThan) { filter = $"<Gt><FieldRef Name=\"{queryFilter.Fieldname}\"/>{CamlFilterValueBuilder(queryFilter.Fieldname, queryFilter.Value.ToString())}</Gt>"; } else if (queryFilter.Op == FilterOperator.LessThan) { filter = $"<Lt><FieldRef Name=\"{queryFilter.Fieldname}\"/>{CamlFilterValueBuilder(queryFilter.Fieldname, queryFilter.Value.ToString())}</Lt>"; } return(filter); }
private SearchQueryFilter MapToFilter(List list, FilterFieldQueryOperator filterOperator, string filterField, string filterFieldValue, FilterChainingOperator?chainingOperatorUsedInCQWP) { if (string.IsNullOrEmpty(filterField)) { return(null); } var foundFields = list.Context.LoadQuery(list.Fields.Where(item => item.InternalName == filterField)); list.Context.ExecuteQueryRetry(); if (foundFields.FirstOrDefault() != null) { // Store field for future reference this.queryFields.Add(foundFields.FirstOrDefault()); // Resolve user ID to name if (foundFields.FirstOrDefault().FieldTypeKind == FieldType.User) { //[11;#i:0#.f|membership|[email protected]] if (filterFieldValue.Contains("|")) { // grab UPN string[] accountParts = filterFieldValue.Replace("[", "").Replace("]", "").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); //string upn = accountParts[accountParts.Length - 1]; if (int.TryParse(accountParts[0], out int userId)) { var user = this.clientContext.Web.GetUserById(userId); this.clientContext.Load(user, p => p.Title); this.clientContext.ExecuteQueryRetry(); filterFieldValue = user.Title; } } } // Build the search query filter object var s = new SearchQueryFilter { Value = filterFieldValue, FilterType = FilterType.Field, Op = MapQueryFilterOperator(filterOperator), Fieldname = foundFields.FirstOrDefault().InternalName, ChainingOperatorUsedInCQWP = chainingOperatorUsedInCQWP, FieldNameMatchText = "" }; return(s); } return(null); }