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.
After adding some data to the SharePoint list, open Visual Studio and follow these
steps:
-
Create a new solution. Provide a name for the solution
(such as SharePointFromCloud).
-
Right-click the solution and select Add | New
Project.
-
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.
-
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.
-
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; }
}
}
-
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 { }
}
-
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).
-
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.
-
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.
-
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.
-
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.
-
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.
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.