Skip to content

CRM 2011 Views and Local Data Groups – Part IV: Filter Templates

December 12, 2011

This article completes the series on Local Data Group management related to the CRM 2011 Views and Filters Toolkit. Here we’ll see how to deploy and instantiate filter templates for Outlook users. You can find more information about the toolkit by selecting the Views Toolkit Category of this blog. For a detailed discussion about Filters and Templates see this article.

Note

The examples shown below refer to Outlook Offline capabilities; they can be extended to the online case with minimal modifications. This note applies to this article as well.

Introduction

The default filter template for offline accounts is a query called My Accounts. This implies that by default, when a user is using the CRM Outlook client offline, they will see only accounts that they own.

Since My Accounts is a default filter template, it will exist as a template and as a filter deployed for each user. We can verify this by running the following two queries:

The first query shows the filter template:

SELECT SavedQueryId, Name, QueryType, IsDefault FROM SavedQuery
WHERE ReturnedTypeCode = 1 AND QueryType = 8192
SavedQueryId Name QueryType IsDefault
31089FD8-596A-47BE-9C9C-3FF82C7A8F8C My Accounts 8192 1

The second query shows the actual filter deployed to the CRM Administrator. Note the ParentQueryId pointing back to the filter template above:

SELECT OwnerIdName, Name, QueryType, ParentQueryId FROM UserQuery
WHERE ReturnedTypeCode = 1 AND QueryType = 16
OwnerIdName Name QueryType ParentQueryId
CRM Administrator My Accounts 16 31089FD8-596A-47BE-9C9C-3FF82C7A8F8C

Changing the default behaviour by modifying Filter Templates

We want to change the default behaviour of CRM by creating a default filter template that allows offline users to see all accounts, not only the ones they own.

To do so, we’ll take an already existing view (Active Accounts) and redeploy it as a filter template. This is done below in multiple steps for clarity, but it could be consolidated into just one workflow process.

Step 1: Deploying a Filter Template

First let’s deploy an offline filter template called Outlook Accounts by creating a workflow called Deploy Offline Filter Template, as shown in the figure below. Note that all workflows in this example are based on Business Unit, but they could be based on any entity.

Deploy Offline Filter Template workflow

Deploy Offline Filter Template workflow

The workflow is composed of two steps:

Retrieve Active Accounts System View

This step uses the RetrieveView custom activity to retrieve the Active Accounts system view definition. I’ve specified the following parameters:

Retrieve Active Accounts System View Parameters

Retrieve Active Accounts System View Parameters

Deploy Offline Filter Template

This step uses the DeploySystemView custom activity with the following parameters:

Deploy Offline Filter Template parameters

Deploy Offline Filter Template parameters

I’ve set only four parameters, which are:

  • When Existing: Error — This means that if the destination view already exists, the activity will fail. See the documentation in Codeplex for other possible values of this parameter.
  • System View Name: Offline Accounts — This overrides the name of the view when deployed. If left blank, this would be the same name of the view in the View Definition parameter, which in this case is Active Accounts.
  • System View Query Type: OfflineTemplate — This is the type of the view when deployed. Since I want to deploy a filter template for offline synchronization, this must be either OfflineTemplate or 8192. For a list of all view types see here.
  • View Definition: View Definition(Retrieve “Active Accounts” System View) — This is the actual definition of the view, which is set to the output parameter of the RetrieveView activity.

Verify

Now select the record relative to the Business Unit or organisation you want to affect, and run the Deploy Offline Filter Template Workflow.

If the workflow completed successfully, you will have a new template called Offline Accounts, and you can verify this by running the following query:

SELECT SavedQueryId, Name, QueryType, IsDefault FROM SavedQuery
WHERE ReturnedTypeCode = 1 AND QueryType = 8192
SavedQueryId Name QueryType IsDefault
9953EFEE-8FD5-E011-84D2-0800277B84F8 Offline Accounts 8192 0
31089FD8-596A-47BE-9C9C-3FF82C7A8F8C My Accounts 8192 1

Now we have a new Filter Template that we can manually deploy to users (using the InstantiateFilter activity, see below for an example). However, since this template is not the default template for Accounts, when new users are created it will not be deployed by default.

Step 2: Set a Filter Template as Default

Let’s now see how we can change which Filter Template gets deployed automatically when new users are created. We want to change the default and have the newly created Offline Accounts template set as default, so that an Offline Accounts filter is deployed to new users instead than a My Accounts filter.

To do so, I’ve created a workflow called Set Default Offline Account Filter Template, as shown in the figure below:

Set Default Offline Account Filter Template workflow

Set Default Offline Account Filter Template workflow

The workflow is composed of just one step:

Set Offline Filter Template as Default

This step uses the SetFilterTemplateDefault custom activity. I’ve specified the following parameters:

Set Offline Filter Template as Default Parameters

Set Offline Filter Template as Default Parameters

I’ve set two parameters, which are:

  • Filter Template – This is the template we want to set as default.
  • True: Set Default, False: Reset Default – This is quite self-explanatory I hope.

Verify

Now select the record relative to the Business Unit or organisation you want to affect, and run the Set Default Offline Account Filter Template Workflow.

If the workflow completed successfully, the Offline Accounts template will be now the default template for offline accounts, and you can verify this by running the same query we executed before:

SELECT SavedQueryId, Name, QueryType, IsDefault FROM SavedQuery
WHERE ReturnedTypeCode = 1 AND QueryType = 8192
SavedQueryId Name QueryType IsDefault
9953EFEE-8FD5-E011-84D2-0800277B84F8 Offline Accounts 8192 1
31089FD8-596A-47BE-9C9C-3FF82C7A8F8C My Accounts 8192 0

Now the Offline Accounts Filter Template will be deployed by default to new users. However, what about existing users? Bear with me…

Step 3: Instantiate Filter Template

The workflow Instantiate Filter Template shown in the figure below can be used to manually instantiate a filter to a user from a template:

Instantiate Filter Template workflow

Instantiate Filter Template workflow

The workflow is composed of just one step:

Instantiate to CRM Administrator

This step uses the InstantiateFilter custom activity. I’ve specified the following parameters:

Instantiate to CRM Administrator Parameters

Instantiate to CRM Administrator Parameters

I’ve set two parameters, which are:

  • Filter Template – This is the template we want to set as default.
  • Target User – The user we want to instantiate the filter to.

Verify

Now select the record relative to the Business Unit or organisation you want to affect, and run the Instantiate Filter Template Workflow.

If the workflow completed successfully, the Offline Accounts filter will now be deployed to CRM Administrator, and you can verify this by running the following:

SELECT OwnerIdName, Name, QueryType, ParentQueryId FROM UserQuery
WHERE ReturnedTypeCode = 1 AND QueryType = 16
OwnerIdName Name QueryType ParentQueryId
CRM Administrator Offline Accounts 16 9953EFEE-8FD5-E011-84D2-0800277B84F8
CRM Administrator My Accounts 16 31089FD8-596A-47BE-9C9C-3FF82C7A8F8C

Step 4: Reset Default Filters

One more thing: the process shown above is not the only way to instantiate filters. The CRM 2011 Views and Filters Toolkit contains another useful custom activity: ResetUserFilters will reset all filters for a particular user to their defaults. An example on how to do this can be found here: Reset User Filters with the Views and Filters Toolkit.

Can you guess what happens to our example scenario if you run the activity on the CRM Administrator? Right, the old default filter goes away:

SELECT OwnerIdName, Name, QueryType, ParentQueryId FROM UserQuery
WHERE ReturnedTypeCode = 1 AND QueryType = 16
OwnerIdName Name QueryType ParentQueryId
CRM Administrator Offline Accounts 16 9953EFEE-8FD5-E011-84D2-0800277B84F8

Conclusions

We’ve come a long way tinkering with Local Data Groups, Filters and Templates. The process certainly helped me to understand in detail how this functionality works in CRM 2011. Hopefully you found a source of useful information somewhere buried in all these posts.

Alberto “Filter-no-more” Gemin

Advertisements
9 Comments leave one →
  1. Mark permalink
    January 17, 2012 20:44

    This sure seems much more complex than the 4.0 tool. Why is that?

  2. Alicia permalink
    January 17, 2012 21:25

    We used your old Admin Console for CRM 4.0, which was very helpful. Wish you had the same one for 2011! We have 49 specific Offline Filters that we set for our users with the Offline client. With the Admin Console, it was really easy to clone these views and quickly push them out to users. It looks like I now have to update 49 workflow steps for EACH user that we deploy the Offline Client for using the Instantiate Filter workflow activity. Am I correct, or do you have another suggestion?

  3. Alicia permalink
    January 18, 2012 16:43

    I *think* I figured out what I need to do. I originally thought I had to run the workflow on the BU entity. After changing to the User entity, I can just make the user field dynamic in the workflow properties.

    I “revoke” my last comment! Thanks!

  4. January 31, 2012 15:51

    Hi Alicia, you’ve got it. You can also combine this tool with the distribute workflow tool, so that you can run the activity automatically for all the users in a BU, for example.

  5. January 31, 2012 15:57

    Different approach. The console is completely manual (i.e. it is good to do one-off things quickly). I wanted something that an administrator can package in a repeatable step/solution.

    This way you can create a workflow and run it for each new employee for example. Also the complexity of the logic was pretty much at its limit for a project like the original Console, which was really a quick fix to an annoying problem.

    With the introduction of Filter Templates the logic would have become too complex to be maintainable, and I would have had to re-write a more solid solution. I decided to go this way rather, and I have to tell you that at least it helped me to learn a lot of new stuff ;)

  6. Richard Page permalink
    March 9, 2012 22:06

    How would I de-activate the Default (if no other way then delete) the User Filter for the Outlook Filter?

    Thank you!

    Richard Page

  7. April 19, 2012 15:03

    Hi

    I have followed the example here:-

    https://xrm2011.wordpress.com/2011/12/12/crm-2011-views-and-local-data-groups-part-iv-filter-templates/

    But when I try to run the workflow I am getting this error:-

    Unhandled Exception: Microsoft.Xrm.Sdk.InvalidPluginExecutionException: System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type Microsoft.Xrm.Sdk.Entity. Data at the root level is invalid. Line 1, position 1. —> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlReader.MoveToContent() at System.Xml.XmlReader.IsStartElement(String localname, String ns) at System.Runtime.Serialization.XmlObjectSerializer.IsRootElement(XmlReaderDelegator reader, DataContract contract, XmlDictionaryString name, XmlDictionaryString ns) at System.Runtime.Serialization.DataContractSerializer.InternalIsStartObject(XmlReaderDelegator reader) at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) — End of inner exception stack trace — at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlReader reader) at Gemina.CRM2011.WF.LdgManager.AgCodeActivityBase.DeserializeEntity(String str) at Gemina.CRM2011.WF.LdgManager.AgDeploySystemView.CreateEntity() at Gemina.CRM2011.WF.LdgManager.AgDeploySystemView.ExecuteBody(CodeActivityContext executionContext) at Gemina.CRM2011.WF.LdgManager.AgCodeActivityBase.Execute(CodeActivityContext executionContext) at Gemina.CRM2011.WF.LdgManager.AgCodeActivityBase.Execute(CodeActivityContext executionContext) at Gemina.CRM2011.WF.LdgManager.AgDeploySystemView.Execute(CodeActivityContext executionContext) at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager) at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

  8. Theresa permalink
    June 14, 2012 20:08

    Great articles! What about creating different workflows, using the SetFilterTemplateDefault, that sets offline filters for different security roles? Is there an easier way? Thanks.

Trackbacks

  1. CRM 2011 Views and Local Data Groups – Part IV: Filter Templates … | blog, iphone, app, creative, games, programming, project, various

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: