QueueItem RSS – An Example Of CRM RSS

By Carlton Colter - Last updated: Wednesday, September 30, 2009 - Save & Share - Leave a Comment

The Notification Accelerator on Codeplex does not offer a queueitem RSS feed. I’m not sure why, but here is a simple solution to display an RSS feed. It defaults to the current user’s queues, but if the q=blah is specified in address then all queueitems in any queue with blah in it will be displayed. Please feel free to post your comments. I did not spend a lot of time on this and I think sql would be quicker. If you find a method that is quick and easy that displays the queue name and queue items, please post it.

crmsdk is my http://crm/MSCrmServices/2007/CrmService.asmx web reference

using System;
using System.Net;
using System.Text;
using System.Web.UI;
using System.Xml;
using CRMQIRSS.crmsdk;

namespace CRMQIRSS
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var QueueName = String.Empty;

            try
            {
                QueueName = Request["q"];
            }
            catch
            {
                // Ignore Errors
            }

            // Clear Any Response
            Response.Clear();
            Response.ContentType = "text/xml";

            var xtwFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            xtwFeed.WriteStartDocument();
            // Setup Feed

            xtwFeed.WriteStartElement("rss");
            xtwFeed.WriteAttributeString("version", "2.0");

            // Setup Channel
            xtwFeed.WriteStartElement("channel");
            xtwFeed.WriteElementString("title", QueueName);
            xtwFeed.WriteElementString("link", "http://www.mscrmblogger.com");
            xtwFeed.WriteElementString("ttl", "5");
            xtwFeed.WriteElementString("description", "The most technical and solution rich CRM blog.");
            xtwFeed.WriteElementString("copyright", "Copyright 2009 mscrmblogger.com. All rights reserved.");

            // Get the Dataset
            BusinessEntityCollection becQueueItems;
            if (String.IsNullOrEmpty(QueueName))
            {
                becQueueItems = GetQueueItemsByCurrentUser();
            }
            else
            {
                becQueueItems = GetQueueItemsByName(QueueName);
            }

            foreach (var be in becQueueItems.BusinessEntities)
            {
                var qi = (queueitem) be;

                var description = "Created: " + Convert.ToDateTime(qi.createdon.Value).ToString("f");

                xtwFeed.WriteStartElement("item");

                xtwFeed.WriteElementString("title", qi.title);

                xtwFeed.WriteElementString("description", description);

                String objecttypecode;
                switch (qi.objecttypecode.Value)
                {
                    case "incident":
                        objecttypecode = "112";
                        break;
                    case "task":
                        objecttypecode = "4212";
                        break;
                    case "phonecall":
                        objecttypecode = "4202";
                        break;
                    case "letter":
                        objecttypecode = "4207";
                        break;
                    case "serviceappointment":
                        objecttypecode = "4214";
                        break;
                    case "fax":
                        objecttypecode = "4204";
                        break;
                    case "email":
                        objecttypecode = "4202";
                        break;
                    case "campaignactivity":
                        objecttypecode = "4402";
                        break;
                    case "campaignresponse":
                        objecttypecode = "4401";
                        break;
                    default:
                        objecttypecode = "0";
                        break;
                }

                xtwFeed.WriteElementString("link",
                                           "http://crm/CRMReports/viewer/drillopen.aspx?ID=" + qi.objectid.Value +
                                           "&OTC=" + objecttypecode);

                xtwFeed.WriteElementString("pubDate", Convert.ToDateTime(qi.enteredon.Value).ToString("f"));

                xtwFeed.WriteElementString("author", qi.createdby.name);

                //TODO: Lookup the queue...  this would be quicker (and easier) with SQL...
                //xtwFeed.WriteElementString("category", "queue/" + qi.queueid);

                xtwFeed.WriteEndElement();
            }

            // Close all tags
            xtwFeed.WriteEndElement();
            xtwFeed.WriteEndElement();
            xtwFeed.WriteEndDocument();
            xtwFeed.Flush();
            xtwFeed.Close();
            Response.End();
        }

        private CrmService GetCrmServiceConnection(String Organization)
        {
            var token = new CrmAuthenticationToken();
            token.AuthenticationType = 0; //AD
            token.OrganizationName = Organization;

            var crmService = new CrmService();
            crmService.CrmAuthenticationTokenValue = token;
            crmService.Credentials = CredentialCache.DefaultCredentials;

            return crmService;
        }

        private BusinessEntityCollection FetchDataSet(string fetchXml)
        {
            var service = GetCrmServiceConnection("OrganizationXYZ");

            var request = new FetchXmlToQueryExpressionRequest();
            request.FetchXml = fetchXml;

            var response = (FetchXmlToQueryExpressionResponse) service.Execute(request);

            var items = service.RetrieveMultiple(response.Query);

            return items;
        }

        private BusinessEntityCollection GetQueueItemsByName(String queueName)
        {
            var fetchXML = "<fetch mapping=\"logical\" count=\"50\" distinct=\"true\">" +
                           "  <entity name=\"queueitem\">" +
                           "    <all-attributes />" +
                           "    <link-entity name=\"queue\" from=\"queueid\" to=\"queueid\">" +
                           "      <attribute name=\"name\" />" +
                           "      <filter>" +
                           "        <condition attribute=\"name\" operator=\"like\" value=\"%" +
                           queueName +
                           "%\" />" +
                           "      </filter>" +
                           "    </link-entity>" +
                           "  </entity>" +
                           "</fetch>";

            return FetchDataSet(fetchXML);
        }

        private BusinessEntityCollection GetQueueItemsByCurrentUser()
        {
            var fetchXML = "<fetch mapping=\"logical\" count=\"50\" distinct=\"true\">" +
                           "  <entity name=\"queueitem\">" +
                           "    <all-attributes />" +
                           "    <link-entity name=\"queue\" from=\"queueid\" to=\"queueid\">" +
                           "      <attribute name=\"name\" />" +
                           "      <filter>" +
                           "        <condition attribute=\"primaryuserid\" operator=\"eq-userid\" />" +
                           "      </filter>" +
                           "    </link-entity>" +
                           "  </entity>" +
                           "</fetch>";

            return FetchDataSet(fetchXML);
        }
    }
}

Total time spent on this: 45 minutes

Posted in Addons • Tags: , , , , , , , Top Of Page

Write a comment