public string GetFetchXmlForQuery(string entityLogicalName, string queryName, string searchTerm, SearchTermOptions searchOptions)
        {
            FetchQuerySettings config;

            if (queryName == "QuickFind")
            {
                config = EntityLookup[entityLogicalName].QuickFindQuery;
            }
            else
            {
                config = EntityLookup[entityLogicalName].Views[queryName];
            }
            jQueryObject fetchElement = config.FetchXml.Clone().Find("fetch");

            fetchElement.Attribute("distinct", "true");
            fetchElement.Attribute("no-lock", "true");

            jQueryObject orderByElement = fetchElement.Find("order");

            orderByElement.Remove();

            // Add the search string and adjust any lookup columns
            jQueryObject conditions = fetchElement.Find("filter[isquickfindfields='1']");

            conditions.First().Children().Each(delegate(int index, Element element)
            {
                // Is this a lookup column?
                string logicalName = element.GetAttribute("attribute").ToString();
                if (LookupAttributes.ContainsKey(logicalName))
                {
                    element.SetAttribute("attribute", logicalName + "name");
                }
            });

            //See what field types can we use for query and remove those attributes we cannot query using this search term.
            if (Number.IsNaN(Int32.Parse(searchTerm)))
            {
                fetchElement.Find("condition[value='#QueryInt#']").Remove();
            }
            if (Number.IsNaN(Decimal.Parse(searchTerm)))
            {
                fetchElement.Find("condition[value='#QueryCurrency#']").Remove();
            }
            if (Number.IsNaN(Date.Parse(searchTerm).GetDate()))
            {
                fetchElement.Find("condition[value='#QueryDateTime#']").Remove();
            }
            if (Number.IsNaN(Double.Parse(searchTerm)))
            {
                fetchElement.Find("condition[value='#QueryFloat#']").Remove();
            }
            // Add the sort order placeholder
            string fetchXml = fetchElement.Parent().GetHtml();//.Replace("</entity>", "{3}</entity>");

            //Prepare search term based on options
            string textSearchTerm = searchTerm;

            if (searchOptions != null && (searchOptions & SearchTermOptions.PrefixWildcard) == SearchTermOptions.PrefixWildcard)
            {
                //Trimming, in case there are already wildcards with user input
                while (textSearchTerm.StartsWith("*") || textSearchTerm.StartsWith("%"))
                {
                    textSearchTerm = textSearchTerm.Substring(1, textSearchTerm.Length);
                }
                textSearchTerm = "%" + textSearchTerm;
            }
            if (searchOptions != null && (searchOptions & SearchTermOptions.SuffixWildcard) == SearchTermOptions.SuffixWildcard)
            {
                //Trimming, in case there are already wildcards
                while (textSearchTerm.EndsWith("*") || textSearchTerm.EndsWith("%"))
                {
                    textSearchTerm = textSearchTerm.Substring(0, textSearchTerm.Length - 1);
                }
                textSearchTerm = textSearchTerm + "%";
            }

            // Add the Query term
            fetchXml = fetchXml.Replace("#Query#", XmlHelper.Encode(textSearchTerm))
                       .Replace("#QueryInt#", Int32.Parse(searchTerm).ToString())
                       .Replace("#QueryCurrency#", Double.Parse(searchTerm).ToString())
                       .Replace("#QueryDateTime#", XmlHelper.Encode(Date.Parse(searchTerm).Format("MM/dd/yyyy")))
                       .Replace("#QueryFloat#", Double.Parse(searchTerm).ToString());

            return(fetchXml);
        }
Example #2
0
        public string GetFetchXmlForQuery(string entityLogicalName, string queryName, string searchTerm, SearchTermOptions searchOptions)
        {
            FetchQuerySettings config;
            if (queryName == "QuickFind")
            {
                config = EntityLookup[entityLogicalName].QuickFindQuery;
            }
            else
            {
                config = EntityLookup[entityLogicalName].Views[queryName];
            }
            jQueryObject fetchElement = config.FetchXml.Clone().Find("fetch");

            fetchElement.Attribute("distinct", "true");
            fetchElement.Attribute("no-lock", "true");

            jQueryObject orderByElement = fetchElement.Find("order");
            orderByElement.Remove();

            // Add the search string and adjust any lookup columns
            jQueryObject conditions = fetchElement.Find("filter[isquickfindfields='1']");

            conditions.First().Children().Each(delegate(int index, Element element)
            {
                // Is this a lookup column?
                string logicalName = element.GetAttribute("attribute").ToString();
                if (LookupAttributes.ContainsKey(logicalName))
                {
                    element.SetAttribute("attribute", logicalName + "name");
                }
            });

            //See what field types can we use for query and remove those attributes we cannot query using this search term.
            if (Number.IsNaN(Int32.Parse(searchTerm)))
            {
                fetchElement.Find("condition[value='#QueryInt#']").Remove();
            }
            if (Number.IsNaN(Decimal.Parse(searchTerm)))
            {
                fetchElement.Find("condition[value='#QueryCurrency#']").Remove();
            }
            if (Number.IsNaN(Date.Parse(searchTerm).GetDate()))
            {
                fetchElement.Find("condition[value='#QueryDateTime#']").Remove();
            }
            if (Number.IsNaN(Double.Parse(searchTerm)))
            {
                fetchElement.Find("condition[value='#QueryFloat#']").Remove();
            }
            // Add the sort order placeholder
            string fetchXml = fetchElement.Parent().GetHtml();//.Replace("</entity>", "{3}</entity>");

            //Prepare search term based on options
            string textSearchTerm = searchTerm;
            if (searchOptions != null && (searchOptions & SearchTermOptions.PrefixWildcard) == SearchTermOptions.PrefixWildcard)
            {
                //Trimming, in case there are already wildcards with user input
                while (textSearchTerm.StartsWith("*") || textSearchTerm.StartsWith("%"))
                {
                    textSearchTerm = textSearchTerm.Substring(1, textSearchTerm.Length);
                }
                textSearchTerm = "%" + textSearchTerm;
            }
            if (searchOptions != null && (searchOptions & SearchTermOptions.SuffixWildcard) == SearchTermOptions.SuffixWildcard)
            {
                //Trimming, in case there are already wildcards
                while (textSearchTerm.EndsWith("*") || textSearchTerm.EndsWith("%"))
                {
                    textSearchTerm = textSearchTerm.Substring(0, textSearchTerm.Length - 1);
                }
                textSearchTerm = textSearchTerm + "%";
            }

            // Add the Query term
            fetchXml = fetchXml.Replace("#Query#", XmlHelper.Encode(textSearchTerm))
                .Replace("#QueryInt#", Int32.Parse(searchTerm).ToString())
                .Replace("#QueryCurrency#", Double.Parse(searchTerm).ToString())
                .Replace("#QueryDateTime#", XmlHelper.Encode(Date.Parse(searchTerm).Format("MM/dd/yyyy")))
                .Replace("#QueryFloat#", Double.Parse(searchTerm).ToString());

            return fetchXml;
        }