public void ApplyRulesToUser(EntityReferenceCollection ec, Guid userId) { var request = new InstantiateFiltersRequest { UserId = userId, TemplateCollection = ec }; service.Execute(request); }
public void ApplyRuleToActiveUsers(EntityReferenceCollection ec) { var qba = new QueryByAttribute("systemuser"); qba.AddAttributeValue("isdisabled", false); foreach (var user in service.RetrieveMultiple(qba).Entities) { var request = new InstantiateFiltersRequest { UserId = user.Id, TemplateCollection = ec }; service.Execute(request); } }
[STAThread] // Required to support the interactive login experience static void Main(string[] args) { CrmServiceClient service = null; try { service = SampleHelpers.Connect("Connect"); if (service.IsReady) { // Create any entity records that the demonstration code requires SetUpSample(service); #region Demonstrate // 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 = service.Create(filter); Console.WriteLine(" and retrieving offline filter"); var result = (SavedQuery)service.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); var 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 = service.Create(template); Console.WriteLine(" and retrieving offline template"); result = (SavedQuery)service.Retrieve( SavedQuery.EntityLogicalName, _offlineTemplate, new ColumnSet("name", "description")); Console.WriteLine("Name: {0}", result.Name); Console.WriteLine("Description: {0}", result.Description); Console.WriteLine(); // Call InstantiateFiltersRequest Console.WriteLine("Retrieving user's ID and creating the template collection"); var whoAmI = new WhoAmIRequest(); Guid id = ((WhoAmIResponse)service.Execute(whoAmI)).UserId; EntityReferenceCollection templates = new EntityReferenceCollection(); templates.Add(new EntityReference( SavedQuery.EntityLogicalName, _offlineTemplate)); Console.WriteLine("Activating the selected offline templates for this user"); var request = new InstantiateFiltersRequest { UserId = id, TemplateCollection = templates }; var response = (InstantiateFiltersResponse)service.Execute(request); Console.WriteLine(); // Call ResetUserFiltersRequest Console.WriteLine("Resetting the user's offline templates to the defaults"); var resetRequest = new ResetUserFiltersRequest { QueryType = SavedQueryQueryType.OfflineFilters }; var resetResponse = (ResetUserFiltersResponse)service.Execute(resetRequest); Console.WriteLine(); #endregion Demonstrate #region Clean up CleanUpSample(service); #endregion Clean up } else { const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Common Data Service"; if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR)) { Console.WriteLine("Check the connection string values in cds/App.config."); throw new Exception(service.LastCrmError); } else { throw service.LastCrmException; } } } catch (Exception ex) { SampleHelpers.HandleException(ex); } finally { if (service != null) { service.Dispose(); } Console.WriteLine("Press <Enter> to exit."); Console.ReadLine(); } }
/// <summary> /// This method first connects to the Outlook service. Afterwards, /// client information is retrieved and the client state is changed. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); //<snippetRetrieveDataFilters1> // 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(); //</snippetRetrieveDataFilters1> //<snippetRetrieveDataFilters2> // Call InstantiateFiltersRequest Console.WriteLine("Retrieving user's ID and creating the template collection"); WhoAmIRequest whoAmI = new WhoAmIRequest(); Guid id = ((WhoAmIResponse)_serviceProxy.Execute(whoAmI)).UserId; EntityReferenceCollection templates = new EntityReferenceCollection(); templates.Add(new EntityReference( SavedQuery.EntityLogicalName, _offlineTemplate)); Console.WriteLine("Activating the selected offline templates for this user"); InstantiateFiltersRequest request = new InstantiateFiltersRequest { UserId = id, TemplateCollection = templates }; InstantiateFiltersResponse response = (InstantiateFiltersResponse)_serviceProxy.Execute(request); Console.WriteLine(); //</snippetRetrieveDataFilters2> //<snippetRetrieveDataFilters3> // Call ResetUserFiltersRequest Console.WriteLine("Resetting the user's offline templates to the defaults"); ResetUserFiltersRequest resetRequest = new ResetUserFiltersRequest { QueryType = SavedQueryQueryType.OfflineFilters }; ResetUserFiltersResponse resetResponse = (ResetUserFiltersResponse)_serviceProxy.Execute(resetRequest); Console.WriteLine(); //</snippetRetrieveDataFilters3> DeleteRequiredRecords(promptforDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } }
/// <summary> /// This method first connects to the Outlook service. Afterwards, /// client information is retrieved and the client state is changed. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); //<snippetRetrieveDataFilters1> // 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(); //</snippetRetrieveDataFilters1> //<snippetRetrieveDataFilters2> // Call InstantiateFiltersRequest Console.WriteLine("Retrieving user's ID and creating the template collection"); WhoAmIRequest whoAmI = new WhoAmIRequest(); Guid id = ((WhoAmIResponse)_serviceProxy.Execute(whoAmI)).UserId; EntityReferenceCollection templates = new EntityReferenceCollection(); templates.Add(new EntityReference( SavedQuery.EntityLogicalName, _offlineTemplate)); Console.WriteLine("Activating the selected offline templates for this user"); InstantiateFiltersRequest request = new InstantiateFiltersRequest { UserId = id, TemplateCollection = templates }; InstantiateFiltersResponse response = (InstantiateFiltersResponse)_serviceProxy.Execute(request); Console.WriteLine(); //</snippetRetrieveDataFilters2> //<snippetRetrieveDataFilters3> // Call ResetUserFiltersRequest Console.WriteLine("Resetting the user's offline templates to the defaults"); ResetUserFiltersRequest resetRequest = new ResetUserFiltersRequest { QueryType = SavedQueryQueryType.OfflineFilters }; ResetUserFiltersResponse resetResponse = (ResetUserFiltersResponse)_serviceProxy.Execute(resetRequest); Console.WriteLine(); //</snippetRetrieveDataFilters3> DeleteRequiredRecords(promptforDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { // You can handle an exception here or pass it back to the calling method. throw; } }