Monday, July 21, 2008

CRM Custom RSS Feed in less than 30 minutes

by Mitchell Kett 07.11.08


One of the best ways to improve a client's business is to keep users better informed and up-to-date on the information provided by CRM.  A workflow could be created (and maintained) to send out an email to the appropriate parties when a specific event happens (create, update, delete of an entity), but what if we could go one step further and provide the same up-to-date information without emails (and maintaining who gets what) or without the need for a user to look in CRM?  What about using an RSS feed?

 

Thanks to a very useful tutorial provided by Jeff at uberasp.net, creating an RSS feed for CRM can be done in a matter of minutes.  For a very quick crash course in XML and the syntax for RSS, see  http://www.w3schools.com/rss/rss_syntax.asp .

 

Say I'd like to create an RSS Feed for a specific entity in CRM.  Whenever a new record is created for this entity, I want to see it in my RSS Feed.  For this example, I created a custom entity in CRM called "new_rssfeed".  The only attribute I added to new_rssfeed was an ntext field called "new_description" which will contain text describing the new record.  After publishing my new entity type, I opened up Visual Studio 2005 and started a new ASP.Net Web Site.  I renamed the Default.aspx file generated by VS to "RSS_Feed.aspx" and changed the code to the following:

 

//RSS_Feed.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="RSS_Feed.aspx.cs" Inherits="_Default" EnableViewState="false" %>

<%@ OutputCache Duration="300" VaryByParam="none" %>

 

Yup, that is all you should see in your .aspx file.  No need for any html tags or DOCTYPE declarations.  What will happen is that when a user navigates to the RSS_Feed.aspx file, the Page_Load event will generate a stream of XML code which the web browser will interpret as an RSS feed.  So there is no need for any HTML.

 

Within the code-behind file, RSS_Feed.aspx.cs, I added the following code to generate the XML for the feed within the Page_Load event.  You can use this code as a template for your own feed.

 

//RSS_Feed.aspx.cs

protected void Page_Load(object sender, EventArgs e)

    {

Response.Clear();

Response.ContentType = "text/xml";

XmlTextWriter objX = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

objX.WriteStartDocument();

objX.WriteStartElement("rss");

objX.WriteAttributeString("version","2.0");

objX.WriteStartElement("channel");

objX.WriteElementString("title", "Practice CRM RSS Feed");

objX.WriteElementString("link","http://localhost:5555/RSS/RSS_Feed.aspx");

objX.WriteElementString("description","Live, up-to-date information coming from CRM!");

objX.WriteElementString("copyright","(c) 2008. All rights reserved.");

objX.WriteElementString("ttl","5");

SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["crmConnectionString"].ToString());

objConnection.Open();

string sql = "SELECT TOP 10 new_name, new_description, new_rssfeedid, createdon FROM new_rssfeed ORDER BY createdon DESC";

SqlCommand objCommand = new SqlCommand(sql, objConnection);

SqlDataReader objReader = objCommand.ExecuteReader();

while (objReader.Read())

{

objX.WriteStartElement("item");

objX.WriteElementString("title",objReader.GetString(0));

objX.WriteElementString("description",objReader.GetString(1));

objX.WriteElementString("link", "http://localhost:5555/MicrosoftCRM/userdefined/edit.aspx?id=" + objReader["new_rssfeedid"].ToString() + "&etc=10008");

objX.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("R"));

objX.WriteEndElement();

}

objReader.Close();

objConnection.Close();

 

objX.WriteEndElement();

objX.WriteEndElement();

objX.WriteEndDocument();

objX.Flush();

objX.Close();

Response.End();

    }

 

Notice the bolded text within the code.  These are snippets that will differ in your code.  For my RSS feed, I gave it the title of "Practice CRM RSS Feed".  The link element is for the URL used to get to the aspx file.  For my connection to CRM, I simply created a web.config file with a connection string to my CRM DB.  Throw in your own custom SQL Query to grab the necessary info to populate the "title", "description", "link", and "pubDate" for the feed <item> element.  The above code, in a nut shell, will grab the 10 most recently added New_rssfeed elements and format them for the feed.  I built and published the web site project and the last thing to do was configure IIS to make the feed accessible.

 

In IIS, all that I needed to do was create a new virtual directory with the alias "RSS" under the Microsoft CRM web site and point it to the folder with the compiled web code.  It automatically saw the web.config file, so no other adjustments had to be made.  Do an IIS reset and navigate to the aspx page.   You should see a basic page with the feed title and a description of how to subscribe to the feed.  You will also see the feed articles listed below and search options to the right (I used IE7 -- other browsers may render differently or re-direct to an RSS Reader like Google Reader).

 

Example of RSS Feed rendering in IE7 (click to view larger image)

 

Just think of what you could use this for!  You could integrate workflows and plugins with an RSS feed in order to provide up-to-date info on what's happening in CRM to other users (or anyone within the local network).  Inform sales people of new opportunities and leads, give executives updates by the minute as opportunities close and new ones come in.  Create one generic feed and register a plugin for multiple actions which could generate a variety of updates to the feed.  You could even create multiple feeds/aspx files and give users the option of how much/ little they'd like to get updated on.  We could even throw in a couple parameters  like entity type and GUID and we've got an RSS feed for one specific record in CRM. 

 

I merely scratched the surface of RSS (you can add images and other content as well), so be creative and think of how you might be able to use this to keep users (and developers) better informed of what's going on in CRM.

No comments: