All blog posts, code samples and downloads licensed under Apache License 2.0.
Close

SSJS to create RSS Feed

Oliver Busse on 02/12/2013 12:00:42 AM CET, filed under SSJS XSP 

Please make sure you have appropriate data in your documents and a view, likely sorted by creation date.

Call this function in your XPage in the afterRenderResponse event. Make sure you set the rendered attribute of the page to false to prevent HTML rendering.

I recommend placing the code within a function to call it from the page event. Cool

var exCon = facesContext.getExternalContext();
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();
 
var v:NotesView
v = database.getView('snDate')
var doc:NotesDocument
 
var dbURL = facesContext.getExternalContext().getRequest().getRequestURL()
dbURL = @LeftBack(dbURL, '/')
 
response.setContentType("application/rss+xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
 
writer.write('<?xml version="1.0" encoding="utf-8"?>');
writer.write('<rss version="2.0">')
writer.write('<channel>')
writer.write('<title>'+database.getTitle()+'</title>')
writer.write('<link>'+dbURL+'</link>')
writer.write('<description>'+database.getTitle()+'</description>')
writer.write('<language>en-us</language>')
writer.write('<copyright>'+database.getTitle()+'</copyright>')
writer.write('<pubDate>'+new java.util.Date().toString()+'</pubDate>')
 
doc = v.getFirstDocument()
while(doc!=null){
writer.write('<item>')
writer.write('<title><![CDATA['+doc.getItemValueString('snSubject')+']]></title>')
try {
writer.write('<description><![CDATA['+doc.getMIMEEntity('snDesc').getContentAsText()+']]></description>')
} catch(e) {
 
}
writer.write('<link>'+dbURL+'/snippet.xsp?documentId='+doc.getUniversalID()+'</link>')
writer.write('<author>'+@Name('[CN]', doc.getItemValueString('snAuthor'))+'</author>')
writer.write('<guid>'+doc.getUniversalID()+'</guid>')
writer.write('<pubDate>'+@Replace(session.evaluate('@Text(@Modified;"D0")', doc), ["[", "]"], ["", ""])+'</pubDate>')
writer.write('</item>')
doc = v.getNextDocument(doc)
}
 
writer.write('</channel>')
writer.write('</rss>')
writer.endDocument();
facesContext.responseComplete();


// this is the XSP markup
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
	<xp:this.afterRenderResponse>
		<![CDATA[#{javascript: paste the code through the editor only!}]]>
	</xp:this.afterRenderResponse>
</xp:view>

Tagged with rss atom xml