Chapter 11. Extending SharePoint with Service-Oriented AJAX

After completing this chapter, you will

  • Understand Windows SharePoint Services as an application platform.

  • Understand the basic SharePoint programming model of sites and services.

  • Be able to implement Web services that are integrated with the SharePoint site context.

  • Implement contract-based Web services using HTTP handlers and WCF technologies.

  • Understand the Web Part programming model and how to deploy AJAX controls in Web Parts.

  • Understand how to integrate and deploy AJAX application code to the SharePoint server.

Introducing the SharePoint Application Platform

The SharePoint application platform is a portal server that runs natively on the ASP.NET framework, providing sites and services for developing and delivering large-scale collaborative Web applications in the enterprise and on the Internet. While SharePoint development can’t be fully explained in a chapter, I’ll provide a brief overview in the following sections and then focus on service development and AJAX integration in the portal environment. As in previous chapters, the examples I provide will use a service-oriented API as the core of the application.

More Information

More Information

For an in-depth look at SharePoint as a development platform, see my other book, with Ted Pattison, Inside Microsoft Windows SharePoint Services 3.0 (Microsoft Press, 2007). This chapter, and in fact this book, is based on the architectural principles introduced in our chapter on AJAX Web Parts.

The term SharePoint applies to both Microsoft Office SharePoint Server, also known as MOSS or SharePoint Server, and Windows SharePoint Services 3.0 (WSS), the core platform. WSS is included in Windows Server 2003 and Windows Server 2008, making it an ideal platform for ASP.NET portal applications. WSS is a site-provisioning platform in which application provisioning components are built by a developer and instantiated and customized by the business user. Provisioning components include site templates, page layouts, list definitions, Web Part applications, and Web service extensions. SharePoint Server builds on the WSS platform by adding enterprise search, a social platform that includes colleagues and user profiles, My Site personal site templates, and publishing and content management components.

Tip

Tip

In the rest of the chapter, I’ll refer to SharePoint as a generic term that applies to WSS and SharePoint Server.

SharePoint has gained tremendous momentum in the market and is deployed in virtually every Fortune 500 company, even when a company has invested in competing portal servers. Both its ease of use and the SharePoint developer community have contributed to its success. Its widespread deployment makes SharePoint an ideal platform for the ASP.NET AJAX developer to learn.

While SharePoint is basically just an ASP.NET application, it does have some nuances that make it challenging to work with. To soften the learning curve, the following steps will get you up and running with SharePoint in no time. If you’re a seasoned SharePoint developer, you may want to skip over the next section.

The SharePoint Virtualized Web Application

SharePoint sites are virtualized in a Web application that is managed by the WSS runtime, which is based on an IIS Web application, file system templates, and Microsoft SQL Server database content. SharePoint sites exist entirely in the database as records of site instances.

One SharePoint Web application is created for each Internet Information Services (IIS) Web site through the SharePoint Central Administration Web site (available by choosing Start, Administrative Tools, SharePoint 3.0 Central Administration), and each Web application can have one or more site collections. Each site collection can contain one or more sites, which correspond to virtual directories and contain the main context for the application. A SharePoint site can be created by any user (with appropriate rights) and contains the social, task, and security contexts that the application executes in. The site can contain pages and lists, and lists can contain list items.

Each site is based on a file system template, which exists in the Program Files directory. The ASPX pages exist in the database based on these templates and can be customized with Web Parts that exist in DLLs. Pages can also be created and deployed to SharePoint sites through site features that add an instance of a file to the site’s virtualized file system.

The default Web application is typically located at C:inetpubwwwrootwssVirtualDirectories80. We’ll use this path in text and in this chapter’s sample code, but this path may vary depending on your installation. This application directory is where the main Web.config file is located that you will edit for your Web application, although all other content in the site exists in the SharePoint virtual file system.

The SharePoint virtual file system is implemented in SQL Server. This virtual file system is used for content in SharePoint sites, including pages and files that are stored in the application.

The Layouts Directory

SharePoint’s _layouts directory is an ideal place to deploy code that extends every SharePoint site. This path is virtualized and exists as a subpath to every SharePoint site’s URL. For example, a SharePoint site that exists at the URL http://mysharepointserver/myorg/myteam would be able to access the layouts virtual directory through the URL http://mysharepointserver/myorg/myteam/_layouts/. This directory is served entirely through the file system and is mapped to the file path Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12TEMPLATELAYOUTS. SharePoint uses files in this location for managing sites and for any resource or Web page that is part of every site. The layouts directory is also an ideal place to serve JavaScript files, CSS files, images, and Web service endpoints such as WCF SVC files.

In the sample code in this chapter, we’ll create the folder SOAjax.Services in the layouts directory to host our WCF endpoints and the folder SOAjax.Script to host the client runtime for our AJAX applications. Code that is run in the _layouts directory should be deployed to the global assembly cache (GAC).

Tip

Tip

A similar directory to layouts is the _vti_bin directory, which is virtualized just as layouts and is used by the SharePoint Web service API. This directory is located at Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI and is available through the Web-relative URL http://sharepointserver/mysharepointsite/_vti_bin/. As with code deployed to the layouts folder, you should create subdirectories for custom code and include your own web.config file for WCF configuration. Although some developers prefer _vti_bin, I’ll use _layouts in this chapter.

Web Service Options for SharePoint

SharePoint includes support for ASMX services and has a relatively complete set of ASMX Web services. Unfortunately, these Web services aren’t AJAX enabled, and in many cases they aren’t very JavaScript friendly either. SharePoint also includes a URL protocol that implements a Web-style API. (For details on this, search on the Web for "SharePoint URL Protocol.")

To create a custom Web service API in SharePoint, there are three recommended options: WCF, ASMX, and HTTP handlers. Each option has its own benefits and drawbacks. ASMX Web services are used throughout the product itself, but this option isn’t recommended for developing new code. WCF is particularly challenging to integrate into the SharePoint environment. It might not be the best approach for commercial applications built on the current release of the SharePoint platform (as of service pack 1), but it is the best long-term approach. WCF is the most flexible option for developing service-oriented APIs for SharePoint. While WCF is not fully supported in the current release, it will be included in future releases.

To support WCF, additional configuration must be deployed to the SharePoint runtime. If you have full control over the SharePoint server, WCF is an option to consider. However, if you are creating and reselling third-party solutions, be cautious about taking a WCF approach within the SharePoint environment. If you are creating REST service endpoints serving XML content, you can still use WCF attributes to define the contract and simply wrap the service in an ASP.NET handler. This is the approach we’ll take in this chapter, and with this approach, you gain the benefits and flexibility of the WCF Web programming model without any configuration changes to the SharePoint runtime.

With an HTTP handler wrapping the WCF endpoint, you can easily move from HTTP handlers to WCF endpoints using the Web programming model without any change in functionality. HTTP handlers and handler factories can be registered through web.config, in the httpHandlers node. Using this technique (which was demonstrated at the end of Chapter 2), an IHttpHandlerFactory is created in an assembly and registered to a handler mapping in web.config. HTTP handlers can be used to wrap WCF Web service classes, giving you a great future-proof solution. I’ll demonstrate this technique throughout this chapter. First, you will see how to create a WCF service, including the data contract. Then you will wrap the service implementation with an HTTP handler using DataContractSerializer. With this technique, it’s trivial as to which technology services the request (HTTP handlers or WCF) because it is processed in the same manner.

To run WCF in the SharePoint application, you must alter the SharePoint runtime through a custom virtual path provider. This technique and instructions for integrating WCF with full access to the SharePoint site context is described in the document "WCF Support in SharePoint" available on the book’s companion content Web site at http://www.microsoft.com/mspress/companion/9780735625914. After WCF support is enabled, WCF must be run in ASP.NET compatibility mode as documented in Chapter 2. Because of this, I favor using an HTTP handler and wrapping the service implementation in the handler, but I’ll also demonstrate the WCF approach. You can run the same code samples through WCF endpoints by deploying an SVC file that utilizes WebServiceHostFactory.

More Information

More Information

In the code download for this chapter, support for WCF can be added to SharePoint by adding a specialized VirtualPathProvider to the SharePoint runtime through a custom HTTP module. The document "WCF Support in SharePoint," included in the book’s companion content, provides instructions for configuring WCF support in SharePoint.

The Web Part Application

The most common way to extend SharePoint is by adding Web Parts. (You can also add pages without Web Parts using SharePoint Features, which is the recommended approach for full-page AJAX applications, but I won’t cover Features in this book.) Web Parts exist as classes that inherit from System.Web.UI.Controls.WebPart and are a pre-AJAX technology deeply rooted in the SharePoint platform. Code for Web Part applications must be deployed to the GAC or the Web application’s bin directory, just as you would deploy code to a typical ASP.NET Web application. Web Part assemblies and namespaces must also be registered in web.config, and they can be added to the Web Part gallery through the Sites Settings link from the root-level SharePoint site.

If you’re new to SharePoint, you’ll want to read the sidebar "Setting up the SharePoint Developer Environment," but seasoned SharePoint developers probably want to skip to the section "Deploying Code Through Web Parts."

Tip

Tip

Code for this book is available online at http://www.microsoft.com/mspress/companion/9780735625914. The code for this chapter is in the file Chapter 11.zip. This chapter’s code requires Windows SharePoint Services 3.0 either on Windows Server 2003 or Windows Server 2008. On Windows Server 2008, you will need to run Visual Studio as an administrator to work with this chapter’s code samples.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset