Language: JavaScript and HTML | VB/C#/C++ and XAML
This topic has not yet been rated - Rate this topic

Quickstart: Managing feed entries (Windows Store apps using C#/VB/C++ and XAML)

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 standard.

Prerequisites

The following examples use C# and are based on the AtomPub sample. For help creating your first app, see Create your first Windows Store app using C# or Visual Basic.

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.

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.

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. In the case of the syntax above

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.

Namespace declarations and client initialization

First we will want to declare the namespaces that support our feed operations. To create the request and work with the feed content, we declare the Windows.Web.AtomPub and Windows.Web.Syndication. Use of authentication credentials (for secure web services) and the destination Uri requires Windows.Security.Credentials and Windows.Foundation.

Also defined is a set of variables that can be used to pass credentials and specific feed addresses between operations.



using Windows.Foundation;
using Windows.Security.Credentials;
using Windows.Web.AtomPub;
using Windows.Web.Syndication;


// The default values for the web service site
private const string DefaultBaseUri = "http://<web service url>";
private const string DefaultUser = "";
private const string DefaultPassword = "";

// The default Service Document and Edit 'URIs' for the web service
private const string DefaultEditUri = "";
private const string DefaultServiceDocUri = "";
private const string DefaultFeedUri = "";

private AtomPubClient client;
private SyndicationFeed currentFeed;
private int currentItemIndex;


Creating a new post within a collection

To add a new post to an existing collection, first the default edit URI for the collection needs to be parsed from the service document. Create a new SyndicationItem object and populate 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.


private async void NewPostStart_Click(object sender, RoutedEventArgs e)
{
    try
    {
	UpdateClient();
	Uri serviceUri = new Uri(ServiceAddressField.Text.Trim() + DefaultServiceDocUri);

	OutputField.Text += "Fetching Service document: " + serviceUri + "\r\n";

	Uri resourceUri = await FindEditUri(serviceUri);
	// The result here is usually the same as:
	// Uri resourceUri = new Uri(ServiceAddressField.Text.Trim() + DefaultEditUri);

	OutputField.Text += "Uploading Post: " + resourceUri + "\r\n";

	SyndicationItem item = new SyndicationItem();
	item.Title = new SyndicationText(Scenario2TitleField.Text, SyndicationTextType.Text);
	item.Content = new SyndicationContent(Scenario2BodyField.Text, SyndicationTextType.Html);

	SyndicationItem result = await client.CreateResourceAsync(resourceUri, item.Title.Text, item);

	OutputField.Text += "Posted at " + result.ItemUri + "\r\n";

	OutputField.Text += "Complete\r\n";
    }
    catch (Exception ex) // For debugging
    {
	OutputField.Text += ex + "\r\n";
    }
}

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.



private async void UpdatePost_Click(object sender, RoutedEventArgs e)
{
    try
    {
	UpdateClient();

	if (CurrentItem == null)
	{
	    OutputField.Text += "No item currently displayed, please download a feed first.\r\n";
	    return;
	}

	OutputField.Text += "Updating item: " + CurrentItem.EditUri + "\r\n";

	// Update the item
	SyndicationItem updatedItem = new SyndicationItem();
	updatedItem.Title = new SyndicationText(Scenario4TitleField.Text, SyndicationTextType.Text);
	updatedItem.Content = new SyndicationContent(Scenario4BodyField.Text, SyndicationTextType.Html);

	await client.UpdateResourceAsync(CurrentItem.EditUri, updatedItem);

	OutputField.Text += "Complete\r\n";
    }
    catch (Exception ex) // For Debugging
    {
	OutputField.Text += ex + "\r\n";
    }
}

Deleting a post from a collection

Deleting an entry from a collection is accomplished by passing the associated Uri to DeleteResourceAsync.



// Delete the current entry
private async void DeletePost_Click(object sender, RoutedEventArgs e)
{
    try
    {
	UpdateClient();

	if (CurrentItem == null)
	{
	    OutputField.Text += "No item currently displayed, please download a feed first.\r\n";
	    return;
	}

	OutputField.Text += "Deleting item: " + CurrentItem.EditUri + "\r\n";

	await client.DeleteResourceItemAsync(CurrentItem);

	OutputField.Text += "Complete\r\n";

	// Our feed is now out of date.  Re-fetch the feed before deleting something else.
	currentFeed = null;
	TitleField.Text = string.Empty;
	WebView.NavigateToString("<HTML></HTML>");
    }
    catch (Exception ex) // For Debugging
    {
    OutputField.Text += ex + "\r\n";
    }
}

Summary

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

Windows.Web.AtomPub
Windows.Web.Syndication
Quickstart: Accessing a web feed
Accessing syndicated web content

 

 

Build date: 11/29/2012

© 2013 Microsoft. All rights reserved.