IT tutorials
 
Windows
 

Using the Windows Azure Service Bus with SharePoint (part 3) - Create an On-Premises Service

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/21/2012 9:32:19 AM

Create an On-Premises Service

You will use the Sales list you created earlier in this chapter in this exercise; to get started, return to the list and add some data. When you’re done, the list should look something like the one shown in Figure 5. Eventually, you’ll use the services you create to communicate with this list, exposing the list data in both a Windows Forms application and a Windows Phone 7 application.

Adding some data to the Sales list in SharePoint.

Figure 5. Adding some data to the Sales list in SharePoint.

After adding some data to the SharePoint list, open Visual Studio and follow these steps:

  1. Create a new solution. Provide a name for the solution (such as SharePointFromCloud).

  2. Right-click the solution and select Add | New Project.

  3. From the Windows templates, select Class Library. You’re going to add a class library that can be used by your services. Provide a name for the class library (such as Resources), and click OK.

  4. Right-click the class library project and select Add Reference. Add references to the System.ServiceModel.dll and the System.Runtime.Serialization.dll to the project.

  5. Right-click the class library and select Add, and then select Class. Replace the boilerplate code in the new class with the following bolded code. This class is the core data object used for serialization:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Serialization;
    
    namespace Resources
    {
        [DataContract]
        public class Sales
        {
            [DataMember]
            public string Company { get; set; }
            [DataMember]
            public string Contact { get; set; }
            [DataMember]
            public string FY09Sales { get; set; }
            [DataMember]
            public string FY10Sales { get; set; }
            [DataMember]
            public string FY11Sales { get; set; }
        }
    }
  6. Right-click the class library and select Add | New. Select Interface, and then add the code shown in bold to the new interface. This serves as the service interface contract:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    
    namespace Resources
    {
        [ServiceContract]
        public interface IEmployeeInfoSvc
        {
            [OperationContract]
            List<Sales> GetSales();
        }
    
        public interface ISalesDataChannel : ISPSalesInfoSvc, IClientChannel { }
    }
  7. With the Resources class library complete, you’ll now add the core service class code that listens and, when called, retrieves data from the SharePoint Sales list. To do this, right-click the solution and add a Windows Console application. Provide a name for the application library (for example, SalesDataService).

  8. Right-click the project and select Add Reference. Click the Projects tab, and select Resources. You can also right-click the newly added project and select Project Dependencies and then Resources.

  9. After creating the new application, right-click the project and add a new class by selecting Add | Class. Provide a name for the class (such as SPListenerService), and click Add.

  10. Add the bold code shown here to the new class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Resources;
    using System.Xml.Serialization;
    using System.IO;
    using Microsoft.SharePoint;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    
    namespace SPListenerService
    {
        public class SPListenerSvc : ISPSalesInfoSvc
        {
            List<Sales> ISPSalesInfoSvc.GetSales()
            {
                List<Sales> mySales = new List<Sales>();
    
                Console.WriteLine("GetSalesData method called...");
    
                using (SPSite mySite = new SPSite("http://blueyonderdemo"))
                {
                    using (SPWeb myWeb = mySite.OpenWeb())
                    {
                        SPList myList = myWeb.Lists["Sales"];
                        SPQuery camlQuery = new SPQuery();
                        camlQuery.ViewXml = "<View/>";
                        SPListItemCollection collListItems = myList.GetItems(camlQuery);
                        foreach (SPListItem listItem in collListItems)
                        {
                            Sales tempSales = new Sales();
                            tempSales.Company = listItem["Title"].ToString();
                            tempSales.Contact = listItem["Contact"].ToString();
                            tempSales.FY09Sales = listItem["FY09"].ToString();
                            tempSales.FY10Sales = listItem["FY10"].ToString();
                            tempSales.FY11Sales = listItem["FY11"].ToString();
                            mySales.Add(tempSales);
                        }
                    }
                }
                return mySales;
            }
        }
    }

    You saw the SharePoint server object model code earlier, but this code is slightly different. Instead of adding a record, this code creates a query and then returns the result of that query as a list collection. You do this by first wrapping your core server code with the using statements to set the correct context for your SharePoint site. You then assign the Sales list to the myList object, and query the list with a collaborative application markup language (CAML) query. (CAML is the standard way to query SharePoint data.) Then you get the items in the list by using the GetItems method, and finally, iterate through the results and assign them to an in-memory Sales object that ends up in the mySales list collection. WCF inherently understands how to manage the list collection, which it passes back to the calling application as XML.

  11. Now open the Program.cs file and modify the code to match the following bold code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;
    using System.ServiceModel;
    
    namespace SPListenerService
    {
        class Program
        {
            private static ServiceHost mySvcHost;
    
            static void Main(string[] args)
            {
                mySvcHost = new ServiceHost(typeof(SPListenerSvc));
    
                mySvcHost.Open();
    
                Console.WriteLine("Sales Data info service is running.");
                Console.WriteLine("Hit [Enter] to exit application...");
                Console.ReadLine();
    
                mySvcHost.Close();
            }
        }
    }

    The code here is fairly simple; it creates a new instance of the service and exits the application when the user presses Enter.

  12. The last thing you’ll need to do is add an application configuration file for your service. Right-click the project, select the General tab, and then choose Application Configuration. Name the file app.config and amend the XML code in the configuration file, as in the following code snippet:

    <?xml version="1.0"?>
    <configuration>
      <system.serviceModel>
        <services>
          <service behaviorConfiguration="serviceBehavior"
           name="SPListenerService.SPListenerSvc">
            <endpoint address=
              "sb://servicebusexample.servicebus.windows.net/SPListenerService/"
              behaviorConfiguration="svcBusAuth" binding="netTcpRelayBinding"
              name="RelayEndpoint" contract="Resources.ISPSalesInfo" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="serviceBehavior">
              <serviceMetadata/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="svcBusAuth">
              <transportClientEndpointBehavior credentialType="SharedSecret">
                <clientCredentials>
                  <sharedSecret issuerName="owner" issuerSecret="<secret here>"/>
                </clientCredentials>
              </transportClientEndpointBehavior>
            </behavior>
          </endpointBehaviors>
        </behaviors>
      </system.serviceModel>
    <startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

Now that you’ve created the service application, press F6 to build and test the service. Note that at this point, you’ll be able to test only whether the service application builds and runs; you will not be able to test a call to the service. When you debug your console application, it should look something like Figure 6.

The listener service working and waiting to be called.

Figure 6. The listener service working and waiting to be called.

If your service builds and runs, you’re now ready to move on to the second part of the exercise, creating and deploying a WCF service to Windows Azure that will call the on-premises service you just built to execute the code against SharePoint.

 
Others
 
- Using the Windows Azure Service Bus with SharePoint (part 2) - Create the Listener Service
- Using the Windows Azure Service Bus with SharePoint (part 1) - Create a Service Bus Namespace
- Windows 8 : Working with User Accounts (part 3) - Switching Users
- Windows 8 : Working with User Accounts (part 2) - Changing User Account Settings
- Windows 8 : Working with User Accounts (part 1) - Add a User
- Mac OS X Hints
- Securing Your Windows 8 Computer : Maintaining Your Privacy
- Securing Your Windows 8 Computer : Switching to a Local Account
- Buyers' Guide : Mac Dreams (Part 3) - iPod classic, iPod Touch, iPod nano, iPod shuffle
- Buyers' Guide : Mac Dreams (Part 2) - MacBook Pro Retina Display, Mac mini, iPhone 5, New iPad
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us