+ Start a Discussion
mworld2mworld2 

How to Create RSS Feed using VF

I've been asked to run a query and use VF to build an RSS feed with the results, I can't find any reference on how to do this. I understand VF and APEX and the structure of RSS files but what throws me is how to make the output be XML, not HTML.

Best Answer chosen by Admin (Salesforce Developers) 
mworld2mworld2

public with sharing class RSSPublishedPagesFeedController
{
    public List <PublishedPage> pages{get;set;}
        
    public RSSPublishedPagesFeedController()
    {
        this.pages = new List <PublishedPage>();
        
        List <Page__c> pageList = [SELECT Id, Name, Summary__c FROM Page__c WHERE Status__c = 'Published/Live' ORDER BY LastModifiedDate DESC LIMIT 50];

        for (Page__c singlePage : pageList)
        {
            pages.Add(new PublishedPage(singlePage.Name, singlePage.Summary__c, 'https://c.cs3.visual.force.com/apex/PageView?id=' + singlePage.Id));
        }
    }

    public class PublishedPage
    {
        public String title { get; private set; }
        public String description { get; private set; }
        public String link { get; private set; }
           
        public PublishedPage(String title, String description, String link)
        {
            this.title = title;
            this.description = description;
            this.link = link;
        }
    }
}

 

<apex:page controller="RSSPublishedPagesFeedController" contentType="application/xml"><?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">
        <channel>
            <title>Company X's Published Pages</title>
            <description>These are the most recently published 50 pages on Company X's site</description>
            <link>http://www.AWebsite.com</link>
            <apex:repeat value="{!pages}" var="page">
                <item>
                    <title>{!page.title}</title>
                    <description>{!page.description}</description>
                    <link>{!page.link}</link>
                </item>      
            </apex:repeat>
        </channel>
    </rss>
</apex:page>

All Answers

mworld2mworld2

public with sharing class RSSPublishedPagesFeedController
{
    public List <PublishedPage> pages{get;set;}
        
    public RSSPublishedPagesFeedController()
    {
        this.pages = new List <PublishedPage>();
        
        List <Page__c> pageList = [SELECT Id, Name, Summary__c FROM Page__c WHERE Status__c = 'Published/Live' ORDER BY LastModifiedDate DESC LIMIT 50];

        for (Page__c singlePage : pageList)
        {
            pages.Add(new PublishedPage(singlePage.Name, singlePage.Summary__c, 'https://c.cs3.visual.force.com/apex/PageView?id=' + singlePage.Id));
        }
    }

    public class PublishedPage
    {
        public String title { get; private set; }
        public String description { get; private set; }
        public String link { get; private set; }
           
        public PublishedPage(String title, String description, String link)
        {
            this.title = title;
            this.description = description;
            this.link = link;
        }
    }
}

 

<apex:page controller="RSSPublishedPagesFeedController" contentType="application/xml"><?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">
        <channel>
            <title>Company X's Published Pages</title>
            <description>These are the most recently published 50 pages on Company X's site</description>
            <link>http://www.AWebsite.com</link>
            <apex:repeat value="{!pages}" var="page">
                <item>
                    <title>{!page.title}</title>
                    <description>{!page.description}</description>
                    <link>{!page.link}</link>
                </item>      
            </apex:repeat>
        </channel>
    </rss>
</apex:page>

This was selected as the best answer
NeetikaNeetika

Hello,

I want to display rss feed in visualforce page..I am trying the above code but it is not showing the rss page it is showing the subcribing page in which subscribe now option is displaying..and after subscribing also it is not showing the exact page..

 

My requirement is to add one section in my page that section contains the rss in which only heading of the contents in rss should populate,not the full page..can anyone tell how to resolve this problem?