Condividi tramite


SharePoint 2010 ECM Code Sample: Publishing Cache Services and Silverlight

Use this SharePoint 2010 code sample to learn about the publishing cache services and Silverlight controls.

Applies to: SharePoint Server 2010

In this article
PublishingCacheRESTService Project
The Role of the SilverlightNavigationControl in SharePoint Publishing Cache Services
The Role of SilverlightContentQuery in SharePoint Publishing Cache Services
Building and Running the Publishing Cache Services and Silverlight Sample

The Publishing Cache Services and Silverlight sample in the PublishingCacheServicesAndSilverlightControls.zip file in the path C:\Program Files\Microsoft SDKs\SharePoint 2010\Samples folder includes several interrelated sample projects. Each one is a separate project in the Microsoft Visual Studio 2010 solution named PublishingCacheServiceAndSilverlightControls. The projects are described in Table 1.

Download the SharePoint 2010 sample code described in this topic from Code Gallery.

Table 1. Publishing cache service and Silverlight controls projects

Project

Description

PublishingCacheRESTService

A Windows Communication Foundation (WCF) service that is implemented as a Microsoft SharePoint Server 2010 project that includes several REST methods for querying for data from the server’s cross-list query cache. The cross-list query cache is the same cached data store used by the Content By Query Web Part (CQWP), the server’s Global Navigation Provider, the server’s Current Navigation Provider, and the server’s Combined Navigation Provider (which are all used by the default navigation features).

All of these data stores are cached in memory on SharePoint Server 2010 front-end servers. In situations where users are mostly or always reading, they efficiently get navigation data and site content.

SilverlightNavigationControl

A Silverlight application that renders navigation data returned by the PublishingCacheRESTService into a navigation control.

SilverlightContentQuery

A Silverlight application that displays a table that shows a list of results based on a SharePoint Server 2010 cross-list query returned by the PublishingCacheRESTService in table format.

PublishingCacheRESTService Project

This project is implemented as a single WCF service that includes several operations.

GetGlobalNav, GetCurrentNav, and GetCombinedNav Methods

The GetGlobalNav, GetCurrentNav, and GetCombinedNav methods are implemented by a single helper method, called getNav. getNav takes as input the appropriate PortalSiteMapProvider object delivered by default in SharePoint Server 2010.

The getNav method iterates through all of the PortalSiteMapNode objects from the current URL to all of its descendents in the provider, then serializes them into an XML fragment. The XML fragment includes the Title, Description, and URL for each PortalSiteMapNode object.

GetDefaultContentQuery and doCustomContentQuery Methods

The GetDefaultContentQuery method and the doCustomContentQuery method are implemented by the same underlying method: doCustomContentQuery. The doCustomContentQuery method takes as inputs all of the parameters used to invoke a cached cross-list query by way of the CrossListQuery class, and returns an XML fragment representing the results of the cached query.

The getDefaultContentQuery method takes no parameters, and simply performs and returns a cross list query with a hard-coded set of parameters. It is mainly intended to be used as a template for creating other hard-coded query services.

Deploying and Using the WCF Service by Way of a SharePoint Farm-Scoped Feature

This project deploys the WCF service by way of a farm-scoped SharePoint Server 2010 feature, which is a capability made possible by SharePoint Server 2010 integration with Visual Studio 2010.

The service is deployed into the ISAPI folder in SharePoint Server 2010, which is accessed by way of the _vti_bin virtual folder URL. Therefore, the format of the URL to the service is http://<server>/<webUrl>/_vti_bin/PublishingCache.svc.

After the service is deployed, access its methods with the URLs and parameters as shown in Table 2.

Table 2. WCF service URLs and parameters

Service

Endpoint-Relative URL

HTTP Method

Header

getGlobalNav

/GlobalNav

GET

Not applicable.

getCurrentNav

/CurrentNav

GET

Not applicable.

getCombinedNav

/CombinedNav

GET

Not applicable.

getDefaultContentQuery

/ContentQuery

GET

Not applicable.

The doCustomContentQuery method takes the parameters listed in Table 3.

Table 3. doCustomContentQuery parameter values

Parameter

Value

Endpoint-Relative URL

/ContentQuery

HTTP Method

POST

Headers

All headers are optional.

  1. ViewFields: A semi-colon–separated list of fields to display. (Default: Title;FileRef)

  2. RowLimit: The number of rows to return. (Default: 50)

  3. Webs: The scope of the query. (Default: Recursive)

  4. Lists: The List template value of the lists to include in the query. (Default: 850)

  5. WebUrl: The site-collection-relative URL for the query. (Default: "/")

Request Body

The Collaborative Application Markup Language (CAML) for the query to execute. That is, the value used as the Query value in the CrossListQueryInfo object.

Body Content Type

Text/plain.

The Role of the SilverlightNavigationControl in SharePoint Publishing Cache Services

This Silverlight application fetches data from the NavigationProvider object by using the PublishingCacheRESTService, and renders it into a two-level navigation display. The top level shows the current location and its direct descendants, displaying the Title for each site (and the Description of each site is shown as a tooltip). When the user mouses over any of the nodes in the top level, the bottom level is populated with the nodes for the children of the moused-over node.

Clicking any node in the top level or the bottom level causes the browser to navigate to the URL of the selected node.

Deploying and Using the SilverlightNavigationControl

Build the project and deploy the resulting .xap file to a location on the server running SharePoint Server 2010. You can use the application in two standard ways: by using the Silverlight Web Part, or by manually including an <object> tag in the HTML markup of a SharePoint Server 2010 page. Most commonly, the SilverlightNavigationControl includes the application in the master page for a SharePoint Server 2010 site so that it appears in a consistent location for all pages that use that master page.

Table 4. cacheServiceUri parameter values

Parameter Name

Required

Value

cacheServiceUri

Yes

The URL to the PublishingCacheRESTService that returns the requested data from the NavigationProvider object

For the navigation provider to display site hierarchy relative to the current page, the URL should be specified as a relative URL (for example, _vti_bin/PublishingCache.svc).

For the navigation provider to display absolute hierarchy (for the navigation provider to always display the hierarchy relative to a specific loction), the URL should be specified as an absolute URL (for example, https://server/_vti_bin/PublishingCache.svc) or a server-relative URL (for example, /_vti_bin/PublishingCache.svc).

The Role of SilverlightContentQuery in SharePoint Publishing Cache Services

This application displays cross-list query data and translates query data results into user-specific data with Microsoft Translator HTTP services.

Deploying and Using the SilverlightContentQuery

Based on its initiation parameters as shown in Table 5, this application uses the doCustomContentQuery method of the PublishingCacheRESTService to execute a cached cross-list query, and then displays the results in a table format. The first column in each result row is a clickable hyperlink—the URL for each row is a column value for each item, as specified in the initiation parameters.

By using the Microsoft Translator services, the application translates the displayed data to any of the languages available in the Translate into drop-down list. The list is dynamically populated based on the list of languages supported by Microsoft Translator.

You can use the application two standard ways: by using the Silverlight Web Part, or by manually including an <object> tag in the HTML markup of a SharePoint Server 2010 page.

Table 5. SilverlightContentQuery initiation parameters

Parameter Name

Required

Usage

Default Values (Non-Required Parameters Only)

TranslatorAppId

Yes

An AppId for the Microsoft Translator service. To obtain an AppId, see the MSDN article Getting Started with Microsoft Translator [https://msdn.microsoft.com/en-us/library/dd575235.aspx].

Not applicable.

cacheServiceUri

Yes

The URL to the doCustomContentQuery method of the PublishingCacheRESTService (for example, https://server/PublishingCache.svc/ContentQuery)

Specify the URL as relative, server-relative, or absolute.

Not applicable.

Query

Yes

The Collaborative Application Markup Language (CAML) for the query to execute. See the description of the Request Body parameter of doCustomContentQuery, in Table 3.

Not applicable

ViewFields

Yes

A semicolon–separated list of fields to display. See the parameter descriptions of the doCustomContentQuery method, in Table 3.

Not applicable.

UrlField

No

The field containing the URL that should be navigated to when the user clicks the row in the displayed results table.

URL

RowLimit

No

The number of rows to return. See the description of the doCustomContentQuery parameters, in Table 3.

Not applicable. The service provides a default value, if value is not specified.

Webs

No

The scope of the query.

Not applicable. The service provides a default value, if value is not specified.

Lists

No

The List Template value of the lists to include in the query.

Not applicable. The service provides a default value, if value is not specified.

WebUrl

No

The site-collection––relative URL of the Web that is used for the query.

Not applicable. The service provides a default value, if value is not specified.

ShowHeaderRow

No

If set to true, then show the column names of ViewFields as a header row in the results table; otherwise, false.

true

Note

For descriptions of many parameters, see their descriptions in the doCustomContentQuery method section.

Building and Running the Publishing Cache Services and Silverlight Sample

You can test this project on your development or test site by taking the following steps.

Building the Sample

The information in this section can be used with any project that uses a WCF REST service. Running the sample requires that you first make the WCF REST service host deployable to SharePoint Server 2010 by using the SharePoint Server 2010 integration with WCF and Visual Studio 2010.

To set up the WCF REST service to be host-deployable in SharePoint Server

  1. Create your WCF Service in a project of type Empty SharePoint Project. This project enables your service to be automatically packaged and deployed to SharePoint Server 2010 as a solution.

    In your project, add a reference to the Microsoft.SharePoint.Client.ServerRunTime.dll file.

  2. Manually add the file reference by browsing to the file. In Visual Studio 2010, open the Add Reference dialog box, click the Browse tab, and then browse to the C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint.Client.ServerRuntime\ folder.

  3. Edit the .svc file. Change the Factory attribute for your service to the new SharePoint factor for hosting WCF services: Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c".

  4. In Visual Studio 2010, open the *.sln file. In the Properties pane of Visual Studio 2010, change the Site URL value to be the absolute address of your development test site. For example, http://MyDevServer/. Ensure that you include the closing forward slash.

    Instead of creating a separate assembly reference, change the Service attribute to include a reference to the project assembly. In SharePoint Server 2010, the reference will fill in when the solution is deployed. Specifically, include the $SharePoint.Project.AssemblyFullName$ reference.

    Remove the Assembly element, if the .svc file already contains an Assembly element.

  5. Add the ISAPI-mapped folder to your project and move files into it. In Visual Studio 2010, right-click the project, and then click Add. Click SharePoint Mapped Folder, and then select the ISAPI folder.

    Move the .svc file and the .cs code-behind file to the ISAPI-mapped folder in the project.

  6. Update the .csproj file. Click the .csproj file and, when prompted, select a text editor such as Notepad to open it.

    Add <TokenReplacementFileExtensions>svc</TokenReplacementFileExtensions> to the PropertyGroup element.

Running the Sample

After the solution is built and deployed, the WCF REST service queries data from the cross-list query cache of the server running SharePoint Server 2010. It then renders the results in a Silverlight navigation control, and can display the results in a table based on the results of a cross-list query returned by the PublishingCacheRESTService.

See Also

Concepts

Enterprise Content Management (ECM) Code Samples for SharePoint Server 2010

Object Caching in SharePoint Server 2010 (ECM)

How to: Customize Navigation in SharePoint Server 2010 (ECM)

Customizing Navigation Controls and Providers in SharePoint Server 2010 (ECM)

Other Resources

Enterprise Content Management Resource Center

SharePoint Developer Center

SharePoint Developer Team Blog