Exercise 2: Creating the Custom SharePoint Syndication Feed
In this exercise, you will create a simple custom syndication feed that will display SharePoint list data as an ATOM feed. The Windows Phone 7 application will retrieve the list data using the custom syndication feed. A syndication feed or a custom application is the only method Windows Phone 7 can use to access data anonymously. A custom syndication feed is simpler to parse than the default RSS feed provided by SharePoint.
Task 1 – Beginning the Exercise
In this task, you will open the lab solution in Visual Studio 2010.
- Make sure that you have downloaded and installed the items listed in System Requirements above prior to beginning this exercise.
- Launch Visual Studio 2010 as administrator and open the lab project by selecting File » Open » Project.
- Browse to the WP7.Acc.Feed.Svr.sln file located at %TrainingKit%\Labs\IntegratingSyndicationFeeds\Source\Before and select it.
- Click Open to open the solution.
In this task, you will build the header information for the ATOM feed. The
- In the WP7.AccBas.Feed.Svr project, in the Layouts\WP7.AccBas.Feed.Svr folder, open the CustomListFeed.aspx.cs file.
Add the following code under the //TODO: 4.3.1 comment to define the GetFeedHeaderXml method:
private XElement GetFeedHeaderXml(string listTitle) { XNamespace atomNS = "https://www.w3.org/2005/Atom"; XNamespace WP7SP = "https://sharepoint.microsoft.com/dpe/WP7SP"; return new XElement(atomNS + "feed", new XAttribute("xmlns", "https://www.w3.org/2005/Atom"), new XAttribute(XNamespace.Xmlns + "maintainenceEvents", "https://sharepoint.microsoft.com/dpe/WP7SP"), new XElement(atomNS + "title", listTitle), new XElement(atomNS + "link", new XAttribute("rel", "self"), new XAttribute("href", this.Page.Request.Url.AbsolutePath)), new XElement(atomNS + "updated", XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Utc)), new XElement(atomNS + "author", new XElement(atomNS + "name", "Student") ), new XElement(atomNS + "id", "urn:uuid:F55E4F8F-E9F1-4051-9E53-1AFB1AA397B3") ); }
The above code defines the required namespaces and creates the header section of an ATOM feed.
Task 3 – Building the Feed Body
In this task, you will build the header information for the Atom feed.
- In the WP7.AccBas.Feed.Svr project, in the Layouts\WP7.AccBas.Feed.Svr folder, open the CustomListFeed.aspx.cs file.
Add the following code under the //TODO: 4.3.2 comment to define the GetListDataAsAtomFeed method:
private XElement GetListDataAsAtomFeed(string listTitle) { XElement feed = GetFeedHeaderXml(listTitle); try { SPWeb ctxWeb = SPContext.Current.Web; SPList list = ctxWeb.Lists[listTitle]; SPView view = list.DefaultView; SPListItemCollection listItems = list.GetItems(view); Uri pageURL = this.Page.Request.Url; string listDisplayUrl = list.RootFolder.Url + "/DispForm.aspx?ID={0}"; XNamespace atomNS = "https://www.w3.org/2005/Atom"; foreach (SPListItem i in listItems) { Guid id = Guid.NewGuid(); XElement element = new XElement(atomNS + "entry", new XElement(atomNS + "id", string.Format("urn:uuid:{0}", id)), new XElement(atomNS + "title", i.Title), new XElement(atomNS + "link", new XAttribute("href", string.Format(listDisplayUrl, i.ID))), new XElement(atomNS + "updated", XmlConvert.ToString(DateTime.Parse(i["Modified"].ToString()), XmlDateTimeSerializationMode.Utc)), new XElement(atomNS + "author", new XElement(atomNS + "name", "Microsoft SharePoint and WP7") )); foreach (string viewField in view.ViewFields) { if (viewField != "LinkTitle") { element.Add(new XElement(viewField.Replace("_x0020_", " "), i.GetFormattedValue(viewField))); } } feed.Add(element); } return feed; } catch (Exception ex) { throw (ex); } }
The above code uses the SharePoint server object model to bind to the list and retrieve the list items. Each list item is added to the feed as a series of XElements and XAttribute objects to form the ATOM feed. Each field in the default view will be available in the feed.
Task 4 – Creating the Feed Using the Page_Load Event
In this task, you add the Page_Load event to create the feed and save it to the Response object.
Add the following code under the //TODO: 4.3.3 comment to define the Page_Load event:
public void Page_Load() { try { string listTitle = Request.QueryString["ListTitle"]; XElement atomFeed = GetListDataAsAtomFeed(listTitle); Response.ContentType = "application/atom+xml"; atomFeed.Save(Response.Output); } catch (Exception ex) { Response.Write(ex.ToString()); } }
- Save CustomListFeed.aspx.cs.
Task 5 – Deploying and Test the Custom List Feed
In this task, you use Visual Studio 2010 to deploy the custom application page to the site’s layouts directory. You will then test the feed to verify the feed returns list items as an ATOM syndication feed.
- Right click on the WP7.AccBas.Feed.Svr solution in the Visual Studio 2010 Solution Explorer pane.
In the Properties window, in the Site URL property, enter the URL to the SharePoint Team Site configured for Forms Based Authentication.
Figure 10
Setting the Solution’s site URL property
Right click on the WP7.AccBas.Feed.Svr solution in the Visual Studio 2010 Solution Explorer pane, and select Deploy.
Figure 11
Deploying the solution to the server
- Open Internet Explorer and click Tools | Internet Options.
Select the Content tab and click the Settings button located in the Feed and Web Slices section.
Figure 12
Configuring feed viewing in Internet Explorer
- Uncheck Turn on feed reading view.
- Click OK.
- Click OK.
- Exit and restart Internet Explorer.
- Using Internet Explorer navigate to the custom feed web page.
- example: https://fbawp7/_layouts/WP7.AccBas.Feed.Svr/customlistfeed.aspx?ListTitle=Maintenance%20Events
Verify the page returns the ATOM feed of the maintenance events list
Figure 13
Viewing the Maintenance Events list Atom feed