/// <summary> /// Create a view. /// Retrieve Views /// Deactivate a view /// </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> /// <param name="promptForReactivate">When True, the user will be prompted to reactivate /// a view that was deactivated.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete, bool promptForReactivate) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); // Create the view. //<snippetWorkWithViews1> System.String layoutXml = @"<grid name='resultset' object='3' jump='name' select='1' preview='1' icon='1'> <row name='result' id='opportunityid'> <cell name='name' width='150' /> <cell name='customerid' width='150' /> <cell name='estimatedclosedate' width='150' /> <cell name='estimatedvalue' width='150' /> <cell name='closeprobability' width='150' /> <cell name='opportunityratingcode' width='150' /> <cell name='opportunitycustomeridcontactcontactid.emailaddress1' width='150' disableSorting='1' /> </row> </grid>"; System.String fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='opportunity'> <order attribute='estimatedvalue' descending='false' /> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> </filter> <attribute name='name' /> <attribute name='estimatedvalue' /> <attribute name='estimatedclosedate' /> <attribute name='customerid' /> <attribute name='opportunityratingcode' /> <attribute name='closeprobability' /> <link-entity alias='opportunitycustomeridcontactcontactid' name='contact' from='contactid' to='customerid' link-type='outer' visible='false'> <attribute name='emailaddress1' /> </link-entity> <attribute name='opportunityid' /> </entity> </fetch>"; SavedQuery sq = new SavedQuery { Name = "A New Custom Public View", Description = "A Saved Query created in code", ReturnedTypeCode = "opportunity", FetchXml = fetchXml, LayoutXml = layoutXml, QueryType = 0 }; _customViewId = _serviceProxy.Create(sq); Console.WriteLine("A new view with the name {0} was created.", sq.Name); //</snippetWorkWithViews1> // Retrieve Views //<snippetWorkWithViews2> QueryExpression mySavedQuery = new QueryExpression { ColumnSet = new ColumnSet("savedqueryid", "name", "querytype", "isdefault", "returnedtypecode", "isquickfindquery"), EntityName = SavedQuery.EntityLogicalName, Criteria = new FilterExpression { Conditions = { new ConditionExpression { AttributeName = "querytype", Operator = ConditionOperator.Equal, Values = { 0 } }, new ConditionExpression { AttributeName = "returnedtypecode", Operator = ConditionOperator.Equal, Values = { Opportunity.EntityTypeCode } } } } }; RetrieveMultipleRequest retrieveSavedQueriesRequest = new RetrieveMultipleRequest { Query = mySavedQuery }; RetrieveMultipleResponse retrieveSavedQueriesResponse = (RetrieveMultipleResponse)_serviceProxy.Execute(retrieveSavedQueriesRequest); DataCollection <Entity> savedQueries = retrieveSavedQueriesResponse.EntityCollection.Entities; //Display the Retrieved views foreach (Entity ent in savedQueries) { SavedQuery rsq = (SavedQuery)ent; Console.WriteLine("{0} : {1} : {2} : {3} : {4} : {5},", rsq.SavedQueryId, rsq.Name, rsq.QueryType, rsq.IsDefault, rsq.ReturnedTypeCode, rsq.IsQuickFindQuery); } //</snippetWorkWithViews2> // Deactivate a view //<snippetWorkWithViews3> System.String SavedQueryName = "Closed Opportunities in Current Fiscal Year"; QueryExpression ClosedOpportunitiesViewQuery = new QueryExpression { ColumnSet = new ColumnSet("savedqueryid", "statecode", "statuscode"), EntityName = SavedQuery.EntityLogicalName, Criteria = new FilterExpression { Conditions = { new ConditionExpression { AttributeName = "querytype", Operator = ConditionOperator.Equal, Values = { 0 } }, new ConditionExpression { AttributeName = "returnedtypecode", Operator = ConditionOperator.Equal, Values = { Opportunity.EntityTypeCode } }, new ConditionExpression { AttributeName = "name", Operator = ConditionOperator.Equal, Values = { SavedQueryName } } } } }; RetrieveMultipleRequest retrieveOpportuntiesViewRequest = new RetrieveMultipleRequest { Query = ClosedOpportunitiesViewQuery }; RetrieveMultipleResponse retrieveOpportuntiesViewResponse = (RetrieveMultipleResponse)_serviceProxy.Execute(retrieveOpportuntiesViewRequest); SavedQuery OpportunityView = (SavedQuery)retrieveOpportuntiesViewResponse.EntityCollection.Entities[0]; _viewOriginalState = (SavedQueryState)OpportunityView.StateCode; _viewOriginalStatus = OpportunityView.StatusCode; SetStateRequest ssreq = new SetStateRequest { EntityMoniker = new EntityReference(SavedQuery.EntityLogicalName, (Guid)OpportunityView.SavedQueryId), State = new OptionSetValue((int)SavedQueryState.Inactive), Status = new OptionSetValue(2) }; _serviceProxy.Execute(ssreq); //</snippetWorkWithViews3> _deactivatedViewId = (Guid)OpportunityView.SavedQueryId; DeleteRequiredRecords(promptForDelete); ReactivateDeactivatedView(promptForReactivate); } } // 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> /// Create a view. /// Retrieve Views /// Deactivate a view /// </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> /// <param name="promptForReactivate">When True, the user will be prompted to reactivate /// a view that was deactivated.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete, bool promptForReactivate) { 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(); // Create the view. //<snippetWorkWithViews1> System.String layoutXml = @"<grid name='resultset' object='3' jump='name' select='1' preview='1' icon='1'> <row name='result' id='opportunityid'> <cell name='name' width='150' /> <cell name='customerid' width='150' /> <cell name='estimatedclosedate' width='150' /> <cell name='estimatedvalue' width='150' /> <cell name='closeprobability' width='150' /> <cell name='opportunityratingcode' width='150' /> <cell name='opportunitycustomeridcontactcontactid.emailaddress1' width='150' disableSorting='1' /> </row> </grid>"; System.String fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='opportunity'> <order attribute='estimatedvalue' descending='false' /> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> </filter> <attribute name='name' /> <attribute name='estimatedvalue' /> <attribute name='estimatedclosedate' /> <attribute name='customerid' /> <attribute name='opportunityratingcode' /> <attribute name='closeprobability' /> <link-entity alias='opportunitycustomeridcontactcontactid' name='contact' from='contactid' to='customerid' link-type='outer' visible='false'> <attribute name='emailaddress1' /> </link-entity> <attribute name='opportunityid' /> </entity> </fetch>"; SavedQuery sq = new SavedQuery { Name = "A New Custom Public View", Description = "A Saved Query created in code", ReturnedTypeCode = "opportunity", FetchXml = fetchXml, LayoutXml = layoutXml, QueryType = 0 }; _customViewId = _serviceProxy.Create(sq); Console.WriteLine("A new view with the name {0} was created.", sq.Name); //</snippetWorkWithViews1> // Retrieve Views //<snippetWorkWithViews2> QueryExpression mySavedQuery = new QueryExpression { ColumnSet = new ColumnSet("savedqueryid", "name", "querytype", "isdefault", "returnedtypecode", "isquickfindquery"), EntityName = SavedQuery.EntityLogicalName, Criteria = new FilterExpression { Conditions = { new ConditionExpression { AttributeName = "querytype", Operator = ConditionOperator.Equal, Values = {0} }, new ConditionExpression { AttributeName = "returnedtypecode", Operator = ConditionOperator.Equal, Values = {Opportunity.EntityTypeCode} } } } }; RetrieveMultipleRequest retrieveSavedQueriesRequest = new RetrieveMultipleRequest { Query = mySavedQuery }; RetrieveMultipleResponse retrieveSavedQueriesResponse = (RetrieveMultipleResponse)_serviceProxy.Execute(retrieveSavedQueriesRequest); DataCollection<Entity> savedQueries = retrieveSavedQueriesResponse.EntityCollection.Entities; //Display the Retrieved views foreach (Entity ent in savedQueries) { SavedQuery rsq = (SavedQuery)ent; Console.WriteLine("{0} : {1} : {2} : {3} : {4} : {5},", rsq.SavedQueryId, rsq.Name, rsq.QueryType, rsq.IsDefault, rsq.ReturnedTypeCode, rsq.IsQuickFindQuery); } //</snippetWorkWithViews2> // Deactivate a view //<snippetWorkWithViews3> System.String SavedQueryName = "Closed Opportunities in Current Fiscal Year"; QueryExpression ClosedOpportunitiesViewQuery = new QueryExpression { ColumnSet = new ColumnSet("savedqueryid", "statecode", "statuscode"), EntityName = SavedQuery.EntityLogicalName, Criteria = new FilterExpression { Conditions = { new ConditionExpression { AttributeName = "querytype", Operator = ConditionOperator.Equal, Values = {0} }, new ConditionExpression { AttributeName = "returnedtypecode", Operator = ConditionOperator.Equal, Values = {Opportunity.EntityTypeCode} }, new ConditionExpression { AttributeName = "name", Operator = ConditionOperator.Equal, Values = {SavedQueryName} } } } }; RetrieveMultipleRequest retrieveOpportuntiesViewRequest = new RetrieveMultipleRequest { Query = ClosedOpportunitiesViewQuery }; RetrieveMultipleResponse retrieveOpportuntiesViewResponse = (RetrieveMultipleResponse)_serviceProxy.Execute(retrieveOpportuntiesViewRequest); SavedQuery OpportunityView = (SavedQuery)retrieveOpportuntiesViewResponse.EntityCollection.Entities[0]; _viewOriginalState = (SavedQueryState)OpportunityView.StateCode; _viewOriginalStatus = OpportunityView.StatusCode; SetStateRequest ssreq = new SetStateRequest { EntityMoniker = new EntityReference(SavedQuery.EntityLogicalName, (Guid)OpportunityView.SavedQueryId), State = new OptionSetValue((int)SavedQueryState.Inactive), Status = new OptionSetValue(2) }; _serviceProxy.Execute(ssreq); //</snippetWorkWithViews3> _deactivatedViewId = (Guid)OpportunityView.SavedQueryId; DeleteRequiredRecords(promptForDelete); ReactivateDeactivatedView(promptForReactivate); } } // 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; } }