public static void SavedQueries(XrmServiceContext svcContext) { 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 = {SalesOrder.EntityTypeCode} } } } }; RetrieveMultipleRequest retrieveSavedQueriesRequest = new RetrieveMultipleRequest { Query = mySavedQuery }; RetrieveMultipleResponse retrieveSavedQueriesResponse = (RetrieveMultipleResponse)svcContext.Execute(retrieveSavedQueriesRequest); DataCollection<Entity> savedQueries = retrieveSavedQueriesResponse.EntityCollection.Entities; using (StreamWriter sw = new StreamWriter(crmPath + "SavedQueries.txt")) { //Display the Retrieved views String oo = "Open Orders (Support)"; SavedQuery sq = new SavedQuery(); sw.WriteLine("rsq.SavedQueryId;rsq.Name;rsq.QueryType;rsq.IsDefault;rsq.ReturnedTypeCode;rsq.IsQuickFindQuery"); foreach (Entity ent in savedQueries) { SavedQuery rsq = (SavedQuery)ent; sw.WriteLine("{0};{1};{2};{3};{4};{5}", rsq.SavedQueryId, rsq.Name, rsq.QueryType, rsq.IsDefault, rsq.ReturnedTypeCode, rsq.IsQuickFindQuery); if (rsq.Name.Equals(oo)) sq = rsq; } sw.WriteLine("*****************"); sw.WriteLine("FetchXml: " + sq.FetchXml); sw.WriteLine("ColumnSetXml: " + sq.ColumnSetXml); //sw.WriteLine("CreatedBy: " + sq.CreatedBy.Name); sw.WriteLine("Description: " + sq.Description); sw.WriteLine("QueryAPI: " + sq.QueryAPI); sw.WriteLine("ReturnedTypeCode: " + sq.ReturnedTypeCode); ExecuteByIdSavedQueryRequest executeSavedQueryRequest = new ExecuteByIdSavedQueryRequest() { EntityId = sq.Id }; // Execute the request sw.WriteLine(" Executing Saved Query"); ExecuteByIdSavedQueryResponse executeSavedQueryResponse = (ExecuteByIdSavedQueryResponse)svcContext.Execute(executeSavedQueryRequest); // Check results if (String.IsNullOrEmpty(executeSavedQueryResponse.String)) throw new Exception("Saved Query did not return any results"); sw.WriteLine(executeSavedQueryResponse.String); } }
internal static List <string> GetEntityFieldNames(string entityName, XrmServiceContext context, bool cached = true) { if (entityName == "none") { return(new List <string>()); } if (AttributeList.ContainsKey(entityName) && cached) { return(AttributeList[entityName]); } lock (AttributeList) { var entityFilter = new MetadataFilterExpression(LogicalOperator.And); entityFilter.Conditions.Add( new MetadataConditionExpression("LogicalName", MetadataConditionOperator.Equals, entityName)); var entityProperties = new MetadataPropertiesExpression { AllProperties = false }; entityProperties.PropertyNames.AddRange("Attributes"); var attributesFilter = new MetadataFilterExpression(LogicalOperator.And); attributesFilter.Conditions.Add( new MetadataConditionExpression("AttributeOf", MetadataConditionOperator.Equals, null)); var attributeProperties = new MetadataPropertiesExpression { AllProperties = false }; attributeProperties.PropertyNames.AddRange("LogicalName"); var entityQueryExpression = new EntityQueryExpression { Criteria = entityFilter, Properties = entityProperties, AttributeQuery = new AttributeQueryExpression { Criteria = attributesFilter, Properties = attributeProperties } }; var retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest { Query = entityQueryExpression, ClientVersionStamp = null }; var attributeNames = ((RetrieveMetadataChangesResponse)context.Execute(retrieveMetadataChangesRequest)) .EntityMetadata.First().Attributes .Select(attribute => attribute.LogicalName).OrderBy(name => name).ToList(); AttributeList[entityName] = attributeNames; } return(AttributeList[entityName]); }
public void AddSubscribers(string ListId, List <Entities.Subscriber> subscribers) { try { foreach (Entities.Subscriber subscriber in subscribers) { Contact contact = (Contact)context.ContactSet.Where(x => x.EMailAddress1.Equals(subscriber.E_mail)).FirstOrDefault(); Guid listGuid = new Guid(ListId); if (contact == null) { Guid contactGuid = context.Create(new Contact() { EMailAddress1 = subscriber.E_mail, FirstName = subscriber.Name }); var member = new AddMemberListRequest() { EntityId = contactGuid, ListId = listGuid }; context.Execute(member); } else { var member = new AddMemberListRequest() { EntityId = contact.Id, ListId = listGuid }; context.Execute(member); } } context.SaveChanges(); } catch (Exception e) { throw new Exception("Get subscribers CRM (" + e.Message.ToString() + ")"); } }
/// <summary> /// This method first creates a series of Accounts to query over, a user query /// that retrieves the names of all Accounts with a name of 'Coho Winery' and /// a system query that retrieves all Account names. Then it validates the system /// query, executes the system query and displays the results, and finally /// executes the user query and displays the results. /// </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(XrmServiceContext _serviceProxy, bool promptforDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy) { // This statement is required to enable early-bound type support. ///_serviceProxy.EnableProxyTypes(); CreateRequiredRecords(); #region Validate saved query // Create the request ValidateSavedQueryRequest validateRequest = new ValidateSavedQueryRequest() { FetchXml = _savedQuery.FetchXml, QueryType = _savedQuery.QueryType.Value }; // Send the request Console.WriteLine(" Validating Saved Query"); try { // executing the request will throw an exception if the fetch xml is invalid var validateResponse = (ValidateSavedQueryResponse)_serviceProxy.Execute(validateRequest); Console.WriteLine(" Saved Query validated successfully"); } catch (Exception) { Console.WriteLine(" Invalid Saved Query"); throw; } #endregion #region Execute saved query // Create the request ExecuteByIdSavedQueryRequest executeSavedQueryRequest = new ExecuteByIdSavedQueryRequest() { EntityId = _savedQuery.Id }; // Execute the request Console.WriteLine(" Executing Saved Query"); ExecuteByIdSavedQueryResponse executeSavedQueryResponse = (ExecuteByIdSavedQueryResponse)_serviceProxy.Execute(executeSavedQueryRequest); // Check results if (String.IsNullOrEmpty(executeSavedQueryResponse.String)) throw new Exception("Saved Query did not return any results"); PrintResults(executeSavedQueryResponse.String); #endregion #region Execute user query // Create the request ExecuteByIdUserQueryRequest executeUserQuery = new ExecuteByIdUserQueryRequest() { EntityId = _userQuery.ToEntityReference() }; // Send the request Console.WriteLine(" Executing User Query"); ExecuteByIdUserQueryResponse executeUserQueryResponse = (ExecuteByIdUserQueryResponse)_serviceProxy.Execute(executeUserQuery); if (String.IsNullOrEmpty(executeUserQueryResponse.String)) throw new Exception("User Query did not return any results"); // validate results PrintResults(executeUserQueryResponse.String); #endregion 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; } }
private static void getInfoByGUID(XrmServiceContext svcContext) { var request = new ExecuteByIdSavedQueryRequest { EntityId = new Guid("00000000-0000-0000-00AA-000010001002")//Account //EntityId = new Guid("B1605B89-3BFE-E411-B185-6C3BE5A6CB54") //EntityId = new Guid("b053a39a-041a-4356-acef-ddf00182762b")//&id=%7bB1605B89-3BFE-E411-B185-6C3BE5A6CB54%7d&pagetype }; var response = (ExecuteByIdSavedQueryResponse)svcContext.Execute(request); string resultset = response.String; }