2

I created a custom activity entity that should only be viewable in Mobile Express (ME). I could adapt the queries for all the views in CRM Online to exclude the entities with the type equal to my custom entity, but that's a bit of a tedious job.

Is there another way to set, on a higher level maybe, to exclude this custom entity from all Activity views?

Peter Majeed
  • 5,304
  • 2
  • 32
  • 57
bvgheluwe
  • 853
  • 7
  • 25

1 Answers1

0

If I understand your question correctly, the only way to exclude the custom activity from and include all the other activitys in your Activity views is to alter the views' underlying fetchXml, either manually or by looping through the SavedQuery entity (see below), to make sure the view doesn't reference the activity. There's no flag that you can put in to stop your custom activity from showing up in any particular activity view; you need to alter all views to have this reflected (unless, of course, your custom entity is not an activity at all).

//using System.Xml.Linq;
//your list of activity entities excluding the special custom activity
string activityList = "<condition attribute=\"activitytypecode\" operator=\"in\"><value>4401</value><value>4204</value><value>10058</value></condition>"; 
XElement newFilter = XElement.Parse(activityList);

var sq = from q in xsc.SavedQuerySet
         where q.ReturnedTypeCode == ActivityPointer.EntityLogicalName
         select new
         {
             fetchXml = q.FetchXml
             , queryId = q.SavedQueryId
             , queryName = q.Name
         };

foreach (var q in sq)
{
    //do your xml parsing
    XElement xml = XElement.Parse(q.fetchXml);

    if (!xml.Elements("entity")
            .Elements("filter").Where(x => x.Attributes("type").Single().Value == "and").Any())
    {
        xml.Elements("entity").Single().Add(XElement.Parse("<filter type=\"and\"></filter>"));    
    }

    //some level of validation
    if (!xml.Elements("entity")
            .Elements("filter")
            .Where(x => x.Attributes("type").Single().Value == "and")
            .Single().Elements("condition")
            .Where(x => x.Attributes("attribute")
            .Single().Value == "activitytypecode")
            .Where(x => x.Attributes("operator")
            .Single().Value == "in").Any())
    {
        xml.Elements("entity")
            .Elements("filter")
            .Where(x => x.Attributes("type")
            .Single().Value == "and")
            .Single().Add(newFilter);

        SavedQuery query = new SavedQuery();
        query.SavedQueryId = q.queryId;
        query.FetchXml = xml.ToString();
        service.Update(query);
    }
}

You'll need to publish after this to see your changes stick.

Peter Majeed
  • 5,304
  • 2
  • 32
  • 57