Sample: Create and retrieve Outlook filters

 

Applies To: Dynamics CRM 2013

This sample code is for Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online. Download the Microsoft Dynamics CRM SDK package. It can be found in the following location in the download package:

SampleCode\CS\Client\Outlook\RetrieveDataFilters.cs

SampleCode\VB\Client\Outlook\RetrieveDataFilters.vb

Requirements

For more information about the requirements for running the sample code provided in this SDK, see Use the sample and helper code.

Demonstrates

This sample shows how to retrieve filters for CRM for Outlook.

Example



// Create and Retrieve Offline Filter
// In your Outlook client, this will appear in the System Filters tab
// under File | CRM | Synchronize | Outlook Filters.
Console.Write("Creating offline filter");
String contactName = String.Format("offlineFilteredContact {0}",
    DateTime.Now.ToLongTimeString());
String fetchXml = String.Format("<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\"><entity name=\"contact\"><attribute name=\"contactid\" /><filter type=\"and\">" +
    "<condition attribute=\"ownerid\" operator=\"eq-userid\" /><condition attribute=\"description\" operator=\"eq\" value=\"{0}\" />" +
    "<condition attribute=\"statecode\" operator=\"eq\" value=\"0\" /></filter></entity></fetch>", contactName);
SavedQuery filter = new SavedQuery();
filter.FetchXml = fetchXml;
filter.IsQuickFindQuery = false;
filter.QueryType = SavedQueryQueryType.OfflineFilters;
filter.ReturnedTypeCode = Contact.EntityLogicalName;
filter.Name = "ReadOnlyFilter_" + contactName;
filter.Description = "Sample offline filter for Contact entity";
_offlineFilter = _serviceProxy.Create(filter);

Console.WriteLine(" and retrieving offline filter");
SavedQuery result = (SavedQuery)_serviceProxy.Retrieve(
    SavedQuery.EntityLogicalName,
    _offlineFilter,
    new ColumnSet("name", "description"));
Console.WriteLine("Name: {0}", result.Name);
Console.WriteLine("Description: {0}", result.Description);
Console.WriteLine();

// Create and Retrieve Offline Template
// In your Outlook client, this will appear in the User Filters tab
// under File | CRM | Synchronize | Outlook Filters.
Console.Write("Creating offline template");
String accountName = String.Format("offlineFilteredAccount {0}",
    DateTime.Now.ToLongTimeString());
fetchXml = String.Format("<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\"><entity name=\"account\"><attribute name=\"accountid\" /><filter type=\"and\">" +
    "<condition attribute=\"ownerid\" operator=\"eq-userid\" /><condition attribute=\"name\" operator=\"eq\" value=\"{0}\" />" +
    "<condition attribute=\"statecode\" operator=\"eq\" value=\"0\" /></filter></entity></fetch>", accountName);
SavedQuery template = new SavedQuery();
template.FetchXml = fetchXml;
template.IsQuickFindQuery = false;
template.QueryType = SavedQueryQueryType.OfflineTemplate;
template.ReturnedTypeCode = Account.EntityLogicalName;
template.Name = "ReadOnlyFilter_" + accountName;
template.Description = "Sample offline template for Account entity";
_offlineTemplate = _serviceProxy.Create(template);

Console.WriteLine(" and retrieving offline template");
result = (SavedQuery)_serviceProxy.Retrieve(
    SavedQuery.EntityLogicalName,
    _offlineTemplate,
    new ColumnSet("name", "description"));
Console.WriteLine("Name: {0}", result.Name);
Console.WriteLine("Description: {0}", result.Description);
Console.WriteLine();


' Create and Retrieve Offline Filter
' In your Outlook client, this will appear in the System Filters tab
' under File | CRM | Synchronize | Outlook Filters.
Console.Write("Creating offline filter")
Dim contactName As String = String.Format("offlineFilteredContact {0}", Date.Now.ToLongTimeString())
Dim fetchXml As String = String.Format("<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"">" &amp; _
    "<entity name=""contact""><attribute name=""contactid"" /><filter type=""and"">" &amp; _
    "<condition attribute=""ownerid"" operator=""eq-userid"" /><condition attribute=""description"" " &amp; _
    "operator=""eq"" value=""{0}"" />" &amp; "<condition attribute=""statecode"" operator=""eq"" value=""0"" />" &amp; _
    "</filter></entity></fetch>", contactName)
Dim filter As New SavedQuery()
filter.FetchXml = fetchXml
filter.IsQuickFindQuery = False
filter.QueryType = SavedQueryQueryType.OfflineFilters
filter.ReturnedTypeCode = Contact.EntityLogicalName
filter.Name = "ReadOnlyFilter_" &amp; contactName
filter.Description = "Sample offline filter for Contact entity"
_offlineFilter = _serviceProxy.Create(filter)

Console.WriteLine(" and retrieving offline filter")
Dim result As SavedQuery = CType(_serviceProxy.Retrieve(SavedQuery.EntityLogicalName, _offlineFilter, _
                                                        New ColumnSet("name", "description")), SavedQuery)
Console.WriteLine("Name: {0}", result.Name)
Console.WriteLine("Description: {0}", result.Description)
Console.WriteLine()

' Create and Retrieve Offline Template
' In your Outlook client, this will appear in the User Filters tab
' under File | CRM | Synchronize | Outlook Filters.
Console.Write("Creating offline template")
Dim accountName As String = String.Format("offlineFilteredAccount {0}", Date.Now.ToLongTimeString())
fetchXml = String.Format("<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"">" &amp; _
                         "<entity name=""account""><attribute name=""accountid"" /><filter type=""and"">" &amp; _
                         "<condition attribute=""ownerid"" operator=""eq-userid"" /><condition attribute=""name"" " &amp; _
                         "operator=""eq"" value=""{0}"" />" &amp; _
                         "<condition attribute=""statecode"" operator=""eq"" value=""0"" /></filter></entity></fetch>", _
                         accountName)
Dim template As New SavedQuery()
template.FetchXml = fetchXml
template.IsQuickFindQuery = False
template.QueryType = SavedQueryQueryType.OfflineTemplate
template.ReturnedTypeCode = Account.EntityLogicalName
template.Name = "ReadOnlyFilter_" &amp; accountName
template.Description = "Sample offline template for Account entity"
_offlineTemplate = _serviceProxy.Create(template)

Console.WriteLine(" and retrieving offline template")
result = CType(_serviceProxy.Retrieve(SavedQuery.EntityLogicalName, _offlineTemplate, _
                                      New ColumnSet("name", "description")), SavedQuery)
Console.WriteLine("Name: {0}", result.Name)
Console.WriteLine("Description: {0}", result.Description)
Console.WriteLine()

See Also

IOrganizationService
Extend Microsoft Dynamics CRM 2013 for Outlook
Sample: Use Outlook methods
Offline and Outlook filters and templates
SavedQuery (view) entity messages and methods