Home > Extensions > API-SDK > QueueItem RSS – An Example Of CRM RSS

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

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2011 MSCRM Blogger

This site does not necessarily represent the views of Microsoft. The material posted on this blog are provided AS-IS, and you are to use the content at your own risk. Some of the code and information on this site may use unsupported methods. If you experience any problems using the information provided through this website, please post a comment and we will do our best to assist you, however, we can in no way guarantee support or resolution.

Microsoft Dynamics®, Silverlight®, Visual Studio®, and the corresponding logos are a registered trademarks owned by Microsoft Corporation. MSCRM Blogger has made every effort to supply trademark information about company names, products, and services mentioned on this blog. All third party trademarks are the property of their respective owners. Any rights not expressly granted herein are reserved.