This topic will show you how to access a service document and modify the feed resources it contains using the Windows.Web.AtomPub namespace, which is the Windows Runtime implementation of the Atom Publication Protocol.
Prerequisites
The following examples use JavaScript and are based on the AtomPub sample. For general help creating a JavaScript Windows Store app, see Create your first Windows Store app using JavaScript. Additionally, JavaScript promises are used in this topic to complete asynchronous operations. For more information on this programming pattern, see Asynchronous programming in JavaScript using promises.
To ensure your Windows Store app is network ready, you must set the capability in the project Package.appxmanifest file. For a definition of each network capability, see How to configure network isolation capabilities.
Instructions
1. Service documents
Before we get into an example, it helps to have a basic understanding of how service documents are used to define the structure of feed content for a web service.
The service document encapsulates at least one workspace element, which represents one or more collections. In other words, web publications like personal blogs and web pages are considered workspaces, and the contained collections represent individual feeds; each containing a number of entries.
The following syntax is a short example of a service document:
<?xml version="1.0" encoding='utf-8'?>
<service xmlns="http://www.w3.org/2007/app"
xmlns:atom="http://www.w3.org/2005/Atom">
<workspace>
<atom:title>Main Site</atom:title>
<collection
href="http://example.org/blog/main" >
<atom:title>My Blog Entries</atom:title>
<categories
href="http://example.com/cats/forMain.cats" />
</collection>
<collection
href="http://example.org/blog/pic" >
<atom:title>Pictures</atom:title>
<accept>image/png</accept>
<accept>image/jpeg</accept>
<accept>image/gif</accept>
</collection>
</workspace>
</service>
To retrieve a service document, pass the associated Uri to RetrieveServiceDocumentAsync. To retrieve, edit, or delete specific feed entries an app will need to parse a retrieved ServiceDocument for the absolute URIs associated with individual entries.
2. Initializing the client with authentication credentials
The following examples utilize Windows.Web.AtomPub classes for feed management operations, and Windows.Web.Syndication classes to represent individual feed elements. Additionally, most web publication services will require some form of authentication, a functionality provided by the Windows.Security namespace.
The following example demonstrates how to set credentials and include them in the initialization of AtomPubClient.
function createClient() { client = new Windows.Web.AtomPub.AtomPubClient(); client.bypassCacheOnRetrieve = true; if ((document.getElementById("userNameField").value !== "") && (document.getElementById("passwordField").value !== "")) { var credential = new Windows.Security.Credentials.PasswordCredential(); credential.userName = document.getElementById("userNameField").value; credential.password = document.getElementById("passwordField").value; client.serverCredential = credential; } else { client.serverCredential = null; } }
3. Creating a new post within a collection
A new post can be added to an existing collection by creating a new SyndicationItem object and populating it with the desired content. When the SyndicationItem is ready, pass the object, a short string describing the entry, and the feed Uri to CreateResourceAsync.
function createPost (postUrl, postTitle, postContent, postSummary, postAuthor) { var uri = new Windows.Foundation.Uri(postUrl); syndicationItem = new Windows.Web.Syndication.SyndicationItem(postTitle, new Windows.Web.Syndication.SyndicationContent(postContent, Windows.Web.Syndication.SyndicationTextType.Text), null); syndicationItem.summary = new Windows.Web.Syndication.SyndicationText(postSummary); syndicationItem.authors[0] = new Windows.Web.Syndication.SyndicationPerson(postAuthor); client.createResourceAsync(uri, syndicationItem).done( outputCallback(uri) ); }
4. Editing a post within a collection
To edit an existing entry in a collection, pass the associated Uri to RetrieveFeedAsync. Prepare a SyndicationItem with new values, and pass the object to UpdateResourceAsync along with the Uri used to retrieve the entry.
function editPost (editUrl, postTitle, postContent, postSummary, postAuthor, refreshCallback) { var uri = new Windows.Foundation.Uri(editUrl); var syndicationItem; syndicationItem = new Windows.Web.Syndication.SyndicationItem(); syndicationItem.title = new Windows.Web.Syndication.SyndicationText(postTitle); syndicationItem.summary = new Windows.Web.Syndication.SyndicationText(postSummary); syndicationItem.content = new Windows.Web.Syndication.SyndicationContent(postContent, Windows.Web.Syndication.SyndicationTextType.Text); syndicationItem.authors[0] = new Windows.Web.Syndication.SyndicationPerson(postAuthor); // Note: Also other item fields can be set such as 'syndicationItem.Categories[0]' client.updateResourceAsync(uri, syndicationItem).done( refreshCallback ); }
5. Deleting a post from a collection
Deleting an entry from a collection is accomplished by passing the associated Uri to DeleteResourceAsync.
function deletePost(editUrl, refreshCallback) { var uri = new Windows.Foundation.Uri(editUrl); client.deleteResourceAsync(uri).done( refreshCallback ); }
Summary and next steps
In this topic we retrieved a service document and introduced new, and modified existing, collection entries within that document. For a quick demonstration of basic feed retrieval, see Quickstart: Accessing a web feed.
Related topics
Build date: 11/28/2012