Make CRM Faster

it s quite the same logic as here

https://www.linkedin.com/pulse/recently-viewed-items-performance-issuebug-ziv-ben-or/

the idea is too clean the historic of recently viewed item

  static void Main(string[] args)
        {

            ConnectToCRM();
            var fetchXml = $@"
<fetch>
  <entity name='userentityuisettings'>
    <attribute name='userentityuisettingsid' />
    <attribute name='recentlyviewedxml' />
    <attribute name = 'lastviewedformxml' />
    <filter type='and'>
      <condition attribute='recentlyviewedxml' operator='not-null' />
    </filter>
  </entity>
</fetch>";
                            string max;

            Console.WriteLine("How Manny history do you want to keep Minimum is 1: ");
            max = Console.ReadLine();

            if (Convert.ToInt32(max)<=0)
            {
                Console.WriteLine("You gave a value under 1. we set the value to 1 ");
                Console.WriteLine("press Enter To continue");
                Console.ReadLine();
                max = "1";
            }


            var maxItems = Convert.ToInt32(max);
            if (maxItems >= 1)
            {
                EntityCollection userEntityUISettings = _service.RetrieveAll(fetchXml);
                string entityName = userEntityUISettings.EntityName;
                foreach (Entity userSettings in userEntityUISettings.Entities)
                {
                    var recentlyXml = System.Xml.Linq.XElement.Parse(userSettings["recentlyviewedxml"].ToString());
                 
                    var recentlyItems = recentlyXml.Descendants("RecentlyViewedItem").ToList();
                    //number of pinned recently viewed items
                    var pinnedCount = recentlyItems.Where(x => x.Element("PinStatus").Value == "true").Count();
                    //Unpinned recently viewed items

                    var unPinned = recentlyItems
                        .Where(x => x.Element("PinStatus").Value == "false")
                        .Select(x => x)
                        .OrderByDescending(x => DateTime.Parse(x.Element("LastAccessed").Value, CultureInfo.InvariantCulture))
                        .ToList();

                        if (pinnedCount + unPinned.Count >= maxItems)
                        {
                        unPinned.Skip(maxItems - pinnedCount).ToList().ForEach(x => x.Remove());

                        var aa = recentlyXml.ToString();
                        Entity newObject = new Entity("userentityuisettings");

                        if (!userSettings.Contains("lastviewedformxml"))
                        {
                            newObject.Attributes.Add("lastviewedformxml", "");
                        }
                        else
                        {
                            newObject["lastviewedformxml"] = userSettings["lastviewedformxml"];
                        }

                        newObject.Id = userSettings.Id;
                        newObject["userentityuisettingsid"] = userSettings["userentityuisettingsid"];
                     
                       newObject["recentlyviewedxml"] = "";                     
                        _service.Update(newObject);

                        newObject["recentlyviewedxml"] = aa;                                                                     
                        _service.Update(newObject);                                           
                        }       

Commentaires

Posts les plus consultés de ce blog

CRM dynamics V9 wait Async web API to contiue code

How to connect to Crm Dynamics On Premis 2016 with Consol App

promise example