EasyComp
EasyComp

Reputation: 63

Add Paging Capabilites To DataServiceQuery for Bing Search API

I'm trying to to add to the Bing Search (Web Results Only) proxy so that I can go to different result pages. I know I'm supposed to use the parameter $skip, but because it has a dollar sign in it, i cannot get it to output the URL correctly.

I can only get it to show incorrectly (it needs the dollar sign after &:

https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web()?Query='xbox'&Page=2

... Or give me the following error :

{Error translating Linq expression to URI: Can't add query option '$Page' because it begins with reserved character '$'.}

Here is my code (My addition is at the bottom). Any help would be greatly appreciated. Thanks!

    public BingSearchContainer(Uri serviceRoot) : 
            base(serviceRoot) {
    }

    /// <summary>
    /// </summary>
    /// <param name="Query">Bing search query Sample Values : xbox</param>
    /// <param name="Market">Market. Note: Not all Sources support all markets. Sample Values : en-US</param>
    /// <param name="Adult">Adult setting is used for filtering sexually explicit content Sample Values : Moderate</param>
    /// <param name="Latitude">Latitude Sample Values : 47.603450</param>
    /// <param name="Longitude">Longitude Sample Values : -122.329696</param>
    /// <param name="WebFileType">File extensions to return Sample Values : XLS</param>
    public DataServiceQuery<WebResult> Web(String Query, String Market, String Adult, Double? Latitude, Double? Longitude, String WebFileType, String Page) {
        if ((Query == null)) {
            throw new System.ArgumentNullException("Query", "Query value cannot be null");
        }
        DataServiceQuery<WebResult> query;
        query = base.CreateQuery<WebResult>("Web");
        if ((Query != null)) {
            query = query.AddQueryOption("Query", string.Concat("\'", System.Uri.EscapeDataString(Query), "\'"));
        }
        if ((Market != null)) {
            query = query.AddQueryOption("Market", string.Concat("\'", System.Uri.EscapeDataString(Market), "\'"));
        }
        if ((Adult != null)) {
            query = query.AddQueryOption("Adult", string.Concat("\'", System.Uri.EscapeDataString(Adult), "\'"));
        }
        if (((Latitude != null) 
                    && (Latitude.HasValue == true))) {
            query = query.AddQueryOption("Latitude", Latitude.Value);
        }
        if (((Longitude != null) 
                    && (Longitude.HasValue == true))) {
            query = query.AddQueryOption("Longitude", Longitude.Value);
        }
        if ((WebFileType != null)) {
            query = query.AddQueryOption("WebFileType", string.Concat("\'", System.Uri.EscapeDataString(WebFileType), "\'"));
        }
        if ((Page != null))
        {
            query = query.AddQueryOption("Page", Page);
        }
        return query;
    }
}

Upvotes: 2

Views: 2683

Answers (2)

Teorist
Teorist

Reputation: 86

For some reason, markwilde's answer did not directly work for me but it gave me an idea how to solve a similar problem: with the latest version of the BingSearchContainer/API, the options $top and $skip were not added using the technique above, which surprised me but I did not have time to investigate, so I used the following, more brutal technique.

In my case, I was passing some non-null parameters to bingContainter.Image(...) method, which were added just fine but when I tried using the AddQueryOption method to add $top and $skip, these options were not added. So, what I did was simply modify the bingContainer.Image(...), to pass it the $top and $skip values and then add them inside that method:

public DataServiceQuery<ImageResult> Image(String Query, String Options, String Market, String Adult, Double? Latitude, Double? Longitude, String ImageFilters, int? top, int? skip ) {
        if ((Query == null)) {
            throw new System.ArgumentNullException("Query", "Query value cannot be null");
        }
        DataServiceQuery<ImageResult> query;
        query = base.CreateQuery<ImageResult>("Image");
        if ((Query != null)) {
            query = query.AddQueryOption("Query", string.Concat("\'", System.Uri.EscapeDataString(Query), "\'"));
        }
        if ((Options != null)) {
            query = query.AddQueryOption("Options", string.Concat("\'", System.Uri.EscapeDataString(Options), "\'"));
        }
        if ((Market != null)) {
            query = query.AddQueryOption("Market", string.Concat("\'", System.Uri.EscapeDataString(Market), "\'"));
        }
        if ((Adult != null)) {
            query = query.AddQueryOption("Adult", string.Concat("\'", System.Uri.EscapeDataString(Adult), "\'"));
        }
        if (((Latitude != null) 
                    && (Latitude.HasValue == true))) {
            query = query.AddQueryOption("Latitude", Latitude.Value);
        }
        if (((Longitude != null) 
                    && (Longitude.HasValue == true))) {
            query = query.AddQueryOption("Longitude", Longitude.Value);
        }
        if ((ImageFilters != null)) {
            query = query.AddQueryOption("ImageFilters", string.Concat("\'", System.Uri.EscapeDataString(ImageFilters), "\'"));
        }
        if (((top != null)
                    && (top.HasValue == true)))
        {
            query = query.AddQueryOption("$top", top.Value);
        }
        if (((skip != null)
                    && (skip.HasValue == true)))
        {
            query = query.AddQueryOption("$skip", skip.Value);
        }
        return query;
    }

Since Microsoft seems to change this class quite often, this may or may not work for you in the future.

Upvotes: 0

markwilde
markwilde

Reputation: 1997

The solution is rather simple.

  • Leave the BingSearchContainer intact
  • and add the paging and skip options using the AddQueryOption function

use the following code to call you searchcontainer.

            var imageQuery = bingContainer.Image("XBOX" , null, null, null, null, null, null);
            imageQuery = imageQuery.AddQueryOption("$top", 20);
            imageQuery = imageQuery.AddQueryOption("$skip", 20);

Upvotes: 6

Related Questions