Accessing SharePoint Using Query Web Service

Shared by: HC11121109129
Categories
Tags
-
Stats
views:
16
posted:
12/11/2011
language:
English
pages:
15
Document Sample
scope of work template
							Hands-On Lab
Accessing SharePoint Using Query Web
Service
Lab version:    1.0.0
Last updated:   12/11/2011
CONTENTS




OVERVIEW ................................................................................................................................................... 3

EXERCISE 1: CONFIGURE THE SEARCH SERVICE APPLICATION ...................................................... 4
       Task 1 – Adding Search Service To The Intranet Web Application’s Services....................................... 4
       Task 2 – Starting Index Crawl Of The SharePoint Content. ................................................................... 5

EXERCISE 2: CREATE A SILVERLIGHT APPLICATION AND A FRAMEWORK FOR DEPLOYMENT . 6
       Task 1 – Adding a new Silverlight Application to the Existing Solution ................................................ 6
       Task 2 – Adding a SharePoint Module to Deploy the Silverlight Xap. .................................................. 7
       Task 3 – Adding a SharePoint Module to a Custom Aspx Page to Display the Silverlight Application in
       a Silverlight Web Part............................................................................................................................ 8
       Exercise 2 Verification ........................................................................................................................... 9

EXERCISE 3: CREATE A KEYWORD QUERY AND EXECUTE AN ASYNCHRONOUS WEB SERVICE
CALL ............................................................................................................................................................. 9
       Task 1 – Adding a Service Reference to Search.asmx ........................................................................... 9
       Task 2 – Creating the ViewModel ....................................................................................................... 10

EXERCISE 4: DISPLAY THE QUERY RESULTS IN THE SILVERLIGHT USER INTERFACE .............. 13
       Task 1 – Modifying the Exiting Silverlight User Interface to Display the Query Results. .................... 13

EXERCISE 5: DEPLOY AND TEST THE SILVERLIGHT APPLICATION ................................................ 14
       Task 1 – Deploying and Test the Application. ..................................................................................... 14

SUMMARY .................................................................................................................................................. 15
Overview
Silverlight applications can access SharePoint data and services using web services. Many SharePoint
2010 web services are available including Search. SharePoint 2010 Web Services increases the reach of
SharePoint data and services.


Objectives
        Create a Silverlight application deployment framework.

        Create an asynchronous web service request from Silverlight.
        Display search results in a Silverlight user interface.



System Requirements
You must have the following items to complete this lab:

        2010 Information Worker Demonstration and Evaluation Virtual Machine
        Microsoft Visual Studio 2010

        Visual Studio 2010 SharePoint Power Tools
        Silverlight 4 Toolkit

        Silverlight WebPart



Setup
You must perform the following steps to prepare your computer for this lab...
    1. Download the 2010 Information Worker Demonstration and Evaluation Virtual Machine from
       http://tinyurl.com/2avoc4b and create the Hyper-V image.
    2. Install the Visual Studio 2010 Silverlight Web Part. The Silverlight Web Part is an add-on to
       Visual Studio 2010.
    3.   Install the Silverlight 4 Toolkit.
    4. Install the Visual Studio 2010 SharePoint Power Tools
Exercises
This Hands-On Lab comprises the following exercises:
    1. Configure the Search Service Application
    2. Create a Silverlight Application and a Framework for Deployment
    3. Create a Keyword Query and Execute an Asynchronous Web Service Call
    4. Display the Query Results in the Silverlight User Interface
    5. Deploy and Test the Silverlight Application

Estimated time to complete this lab: 45 minutes.


Starting Materials
This Hands-On Lab includes the following starting materials.

       Visual Studio solutions. The lab provides the following Visual Studio solutions that you can use
        as starting point for the exercises.

                ◦   <Install location>\Labs\AccessingSPUsingQueryWebService\Source\Begin\
                    SilverlightQueryWebService .sln: This soution is a a Silverlight Web Part with the
                    starter code commented out.

         Note: Inside each exercise folder, you will find an end folder containing a solution with the
         completed lab exercise.



Exercise 1: Configure the Search Service
Application
In this exercise you will add a Silverlight project to an existing solution and create a framework to deploy
the Silverlight application to a SharePoint farm.

Task 1 – Adding Search Service To The Intranet Web Application’s Services
    1. Navigate to Central Administration located at http://demo2010a:2010/default.aspx.
    2. Click the Manage Web Applications link.
    3. Click Intranet in the list of Web Applications. The Intranet entry should be highlighted.
    4. Click on Service Connections located in the Ribbon.
       Figure 1
       Service Connection Button


   5. Choose [custom] as the group of connections.




       Figure 2
       Configure Service Application Associations dialog box


   6. Check all services available except for the FAST Query service. This should be left unchecked for
      this lab.
   7. Click OK.

Task 2 – Starting Index Crawl Of The SharePoint Content.
   1. Navigate to the Search Administration page located at
      http://demo2010a:2010/searchadministration.aspx?appid=edbe9ac4-3596-497b-a210-
      9d1d223b8052
   2. Click Content Sources located in the left hand navigation pane.
       Hover over the Local SharePoint sites link and select Start Full Crawl. The crawl may take a few
       minutes. The crawl will need to complete before you build and deploy the application in this
       lab.




       Figure 3
        Local SharePoint sites link.




Exercise 2: Create a Silverlight
Application and a Framework for
Deployment
In this exercise you will add a Silverlight project to an existing solution and create a framework to deploy
the Silverlight application to a SharePoint farm.

Task 1 – Adding a new Silverlight Application to the Existing Solution
    1. Using Visual Studio, open the starter solution from
       <Install>\Labs\AccessingSPUsingQueryWebServiceSource\Begin\
       SilverlightQueryWebService.sln.
    2. Right-click the SilverlightQueryWebService solution file in the Solution Explorer and select Add
       New Project.
    3. Select Silverlight in the left-hand pane and Silverlight Application in the center pane.
    4. Name the project Silverlight.QueryWebService.




        Figure 4
        Create a new Silverlight application project


    5. Click OK.
Task 2 – Adding a SharePoint Module to Deploy the Silverlight Xap.
    1.   Right-click on SharePoint.Client.QueryWebService project in Solution Explorer and select Add -
         > New Item.
    2. Select the Module project item and name the Module item Silverlight.
    3. Click Add.
    4. Right-click on Sample.txt in the Solution Explorer and select Delete to remove the sample file.
    5. Right-click the Silverlight project item in Solution Explorer and select Properties.
    6. Select Project Output References in the Properties pane.




         Figure 5
         Properties pane


    7. Click the ellipse button.
    8. Click Add in in the Project Output References dialog.




         Figure 6
         Project Output References dialog


    9. Right-click the Silverlight project item in Solution Explorer and select Add -> Existing Item.
    10. Set the Project Name property to Silverlight.QueryWebServices.
    11. Set the Deployment Type property to ElementFile. This will include the output of the
        Silverlight.QueryWebService project in the SharePoint module for deployment.




         Figure 7
         Project Output References dialog


    12. Click OK.
    13. Right-click on the Elements.xml file in the Silverlight module project item and select Open.
    14. Add Url="SiteAssets" to the Module element
    15. Add Type="GhostableInLibrary" to the File element.
    16. Set the Url attribute located in the File element equal to "Silverlight.QueryWebService.xap".

Task 3 – Adding a SharePoint Module to a Custom Aspx Page to Display the Silverlight Application in a
Silverlight Web Part.
    1.   Right-click on SharePoint.Client.QueryWebService project in Solution Explorer and select Add -
         > New Item…
    2. Select the Module project item and name the Module item CustomPages.
    3. Click Add.
    4. Right-click on Sample.txt in the Solution Explorer and select Delete to remove the sample file.
    5. Right-click on the CustomPages project item and select Add -> Existing Item…
    6. Select QueryWSDemo.aspx located at
       <Install>\Labs\AccessingSPUsingQueryWebService\Source\SupportingFiles.
    7. Right-click on the Element.xml file located in the CustomPages module and select Open.
    8. Replace the Xml in Elements.xml with the Xml included in CustomPagesElements.xml file
       located at <Install> \Labs\AccessingSPUsingQueryWebService\Source\SupportingFiles.
Exercise 2 Verification
In order to verify that you have correctly performed all steps of exercise 1, proceed as follows:

Verification 1
In this verification, the custom Aspx page will deploy and contain a Silverlight Web Part. The Silverlight
application has no user interface and will not display any visible ui.
    1. Right-click the solution name in Solution Explorer and select Deploy Solution.
    2. Navigate to http://intranet.contoso.com/queryWSDemo.aspx .
    3. Verify the page loads and contains the Silverlight Query Web Service Web Part.




        Figure 8
        Silverlight Query Web Service Web Part




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 http://intranet.contoso.com/_vti_bin/search.asmx into 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);

        }
        }




Exercise 4: Display the Query Results in
the Silverlight User Interface

In this exercise you will modify the existing Silverlight application to display the results of the query. The
user interface is bound to the SearchResults propery.

Task 1 – Modifying the Exiting Silverlight User Interface to Display the Query Results.
    1. Right-click MainPage.xaml and select Open.
    2. Replace the MainPage.xaml markup with the markup located in
       <Install>\Labs\AccessingSPUsingQueryWebService\Source\SupportingFiles\MainPage.xaml.tx
       t. Notice the user interface components are data bound to the SearchResults property.




        Figure 10
        Silverlight Query Web Service Web Part


    3. Right-click MainPage.xaml and select View Code.
    4. Add using Silverlight.QueryWebService.ViewModels; to the list of using statements in the
       MainPage.xaml.cs file.
    5. Add a private class-level variable named viewModel of type SearchResultsViewModel to the
       class.
        C#
        public partial class MainPage : UserControl
        {
            private SearchResultsViewModel viewModel;



    6. Use snippet 4.1.7 to add the viewModel and DataContext code below InitializeComponent in
        the constructor.
        C#
        public MainPage()
        {
            InitializeComponent();
            viewModel = new SearchResultsViewModel();
            this.DataContext = viewModel;
        }




    7. Use snippet 4.1.8 to add the btnSearch_click event handler that is used to start the search.
        C#
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(txtKeyword.Text))
                viewModel.GetSearchResults(txtKeyword.Text);
        }




Exercise 5: Deploy and Test the
Silverlight Application

In this exercise you will deploy and test the completed Silverlight Web Part.

Task 1 – Deploying and Test the Application.
    1. Right-click on the solution in the Solution Explorer and select Deploy Solution.
    2. Navigate to http://intranet.contoso.com/queryWSDemo.aspx using Internet Explorer.
    3. Enter SharePoint into the Keyword textbox.
    4. Click the Search button.
    5. Verify the results are displayed in the Silverlight application.




        Figure 11
        Silverlight Query Web Service Web Part




Summary
Silverlight applications can connect to SharePoint 2010 using web services. In this exercise you used the
Search.asmx web service to query SharePoint for keywords. The results were displayed using a data
bound list. The project uses the MVVM pattern which is a common pattern in Silverlight development.

						
Related docs
Other docs by HC11121109129
NREGS-AP MIS
Views: 109  |  Downloads: 0
Name : _____ (first AND last
Views: 1  |  Downloads: 0
List of HASP Device Driver Versions Since V4
Views: 60  |  Downloads: 0
Annual project work plan
Views: 19  |  Downloads: 1
TRIAL EXHIBIT LIST
Views: 95  |  Downloads: 0
Design Oriented Model for Symmetric DG MOSFET
Views: 13  |  Downloads: 0
Japanese Drama
Views: 5  |  Downloads: 0
December 14, 2001
Views: 1  |  Downloads: 0
Lecture 4: Superstrings, vibrations & vacu
Views: 3  |  Downloads: 0