Presentation from SharePoint Saturday New Zealand that used an alerting solution to show how to build 1) a custom WCF web service that returns JSON, 2) how the SP.UI JavaScript namespace is used with the status bar, notifications and modal dialogues and 3) how to call web services from jQuery.
3. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
Session Breakdown
Introduction (Brief)
There a couple of things I “prepared earlier” … a brief explanation
WCF
Building a custom WCF service for SharePoint 2010
The focus will not be what the service does but how to wire it up
jQuery
Instead of using the ASP.NET Ajax / Script Manager lets talk to the WCF
service with jQuery
SharePoint 2010 UI Utilities – Status Bar, Modal Dialog and Notifications
The UI glue to show the information to users
4. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
Introduction
The Alert architecture
A SharePoint List – the alert storage
A custom WCF Service – the basic business objects / logic and
communication mechanism
jQuery – asynchronous data retrieval (on the client)
SP.UI.Status – the presentation layer
6. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
WCF
Can build SOAP, REST or Data Services
Supports dynamic configuration using factories. SharePoint 2010
provides 3 factories:
Service Type Service Factory Description
SOAP service MultipleBaseAddressBasicHttpBindingS Basic HTTP binding must be used,
erviceHostFactory which creates endpoints for a service
based on the basic HTTP binding.
REST Service MultipleBaseAddressWebServiceHostF The service factory creates endpoints
actory with Web bindings.
ADO.NET Data Service MultipleBaseAddressDataServiceHostF A data service host factory can be used.
actory
7. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
Custom WCF Service in SharePoint 2010
Can’t be deployed by a sandbox solution
WCF isn’t 100% supported in SharePoint project templates
Create a separate WCF Service Library and copy the files into the SharePoint
project
Add references to System.ServiceModel and System.Runtime.Serialization
The service declaration (.svc) needs to be added to list of files processed for
replaceable tokens
http://msdn.microsoft.com/en-us/library/ee231545.aspx
Create Alert class that represents our alerts for serialization/data
transfer
Alert { Title, Description, Link, Colour }
8. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
Custom WCF Service in SharePoint 2010
Make the service ASP.NET compatible
using System.ServiceModel.Activation
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Required)]
Make the service REST capable
using System.ServiceModel.Web
[WebGet(UriTemplate = "/Alert", BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json)]
Using MultipleBaseAddressWebServiceHostFactory because its a REST
service (sort of)
11. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
jQuery
We could use ASP.NET AJAX but … we won’t
We will use jQuery to call our WCF web service
jQuery has built in support for ajax and JSON
jQuery.ajax()
jQuery.getJSON()
13. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
The OOTB UI Improvements in 2010
SP.UI.Notify
addNotification(), Notify(), removeNotification()
SP.UI.Status
addStatus(), appendStatus(), removeAllStatus(), removeStatus(),
setStatusPriColor(), Status(), updateStatus()
SP.UI.ModalDialog.showModalDialog(options)
options is a JavaScript literal of the properties to pass this method
e.g. var options = { url: “http://sharepoint.com” }
15. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
Resources
WCF Services in SharePoint Foundation 2010
http://msdn.microsoft.com/en-us/library/ff521586.aspx
Creating a Custom WCF Service in SharePoint Foundation
http://msdn.microsoft.com/en-us/library/ff521581.aspx
jQuery ajax and JSON
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/jQuery.getJSON/
16. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
Resources
SP.UI.Notify
http://msdn.microsoft.com/en-us/library/ff408137.aspx
SP.UI.Status
http://msdn.microsoft.com/en-us/library/ff412058.aspx
SP.UI.ModalDialog
http://msdn.microsoft.com/en-us/library/ff408909.aspx
http://msdn.microsoft.com/en-us/library/ff411351.aspx
Enabling Intellisense in Visual Studio
http://msdn.microsoft.com/en-us/library/ff798328.aspx
17. SharePoint Saturday New Zealand – WCF, jQuery and the SP Status Bar
Nick Hadlee
SharePoint Consultant at Intergen
Blog: nickhadlee.wordpress.com
Twitter: @nickhadlee