Share via


Exercise 3: Create a Keyword Query and Execute an Asynchronous Web Service Call

In this exercise, you will create a Keyword Query using Search.asmx web service and execute it asynchronously.

Task 1 – Adding a Service Reference to Search.asmx

  1. Right-click the References folder in the Silverlight.QueryWebService project.
  2. Select Add Service Reference to display the Add Service Reference dialog.
  3. Enter https://intranet.contoso.com/_vti_bin/search.asmxinto the address textbox
  4. Click Go to locate the service.
  5. Enter QueryWebService in the Namespace textbox.
  6. Click Ok

Task 2 – Creating the ViewModel

  1. Right-click on the Silverlight.QueryWebService project and select Add.
  2. Select New Folder and name the folder ViewModels
  3. Right-click ViewModel folder,
  4. Select Add | Class.
  5. Name the class SearchResult.
  6. Click Add.
  7. Open the SearchResult.cs file.
  8. Use snippet 4.1.1 to replace the SearchResult class. .
  9. Right-click INotifyPropertyChanged and select Resolve.
  10. Right-click ViewModel folder
  11. Select Add | Class.
  12. Name the class SearchResultsViewModel.cs
  13. Click Add.
  14. Add the additional using statements to the SearchResultsViewModel with snippet 4.1.2

    Figure 9

    Additional using statements

  15. Modify the SearchResultsViewModel’s class declaration to inherit from INotifyPropertyChanged.

    C#

    public class SearchResultsViewModel : INotifyPropertyChanged

  16. Use snippet 4.1.3 to add the INotifyPropertyChanged interface’s event.

    C#

    public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string property) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(property)); } }

  17. Use snippet 4.1.4 to add the SearchResults property to SearchResultsViewModel class.

    C#

    public ObservableCollection<SearchResult> SearchResults { get; private set; } public SearchResultsViewModel() { SearchResults = new ObservableCollection<SearchResult>(); }

  18. Add the GetSearchResults method using snippet 4.1.5. The GetSearchResults method sets up the query using the service proxy to the asmx web service. The web service uses CAML to query SharePoint. The method sets up the QueryCompleted method and calls the QueryAsync method of the proxy class.

    C#

    public void GetSearchResults(string Keyword) { SearchResults.Clear(); QueryServiceSoapClient proxy = new QueryServiceSoapClient(); string hostName = App.Current.Host.Source.Host; string endPointUrl = proxy.Endpoint.Address.ToString().Replace("localhost", hostName); proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(endPointUrl); proxy.QueryCompleted += new EventHandler<QueryCompletedEventArgs>(proxy_QueryCompleted); var query = String.Format( @"<QueryPacket> <Query> <Context> <QueryText type='STRING' language='en-us' >""{0}""</QueryText> <LanguagePreference>en-us</LanguagePreference> </Context> <Properties> <Property name='path'/> <Property name='title'/> </Properties> <EnableStemming>true</EnableStemming> </Query> </QueryPacket>", Keyword); proxy.QueryAsync(query); }

  19. Add the proxy_QueryCompleted event handler using snippet 4.1.6. This method retrieves and parses the Xml response returned from query. Each individual results are added to the SearchResults collection.

    C#

    void proxy_QueryCompleted(object sender, QueryCompletedEventArgs e) { string v = e.Result; XElement QueryResponse = XElement.Parse(e.Result); XNamespace nsDocument = "urn:Microsoft.Search.Response.Document"; string nsDocumentDocument = "urn:Microsoft.Search.Response.Document.Document"; var query = from document in QueryResponse.Descendants(nsDocument + "Document") select new SearchResult { Title = document.Descendants( XName.Get("Property", nsDocumentDocument)) .Where(x => x.Element( XName.Get("Name", nsDocumentDocument)).Value == "title") .First().Element(XName.Get("Value", nsDocumentDocument)).Value, Path = document.Descendants( XName.Get("Property", nsDocumentDocument)) .Where(x => x.Element( XName.Get("Name", nsDocumentDocument)).Value == "path") .First().Element(XName.Get("Value", nsDocumentDocument)).Value }; foreach (var result in query) { SearchResults.Add(result); } }