public static bool WithCredentials = false; // This should be set to true if using Integrated auth with Visual Studio debug server in chrome such that Organization.svc calls are cross domain #endregion Fields #region Methods /// <summary> /// Associate one or more related entites to a target entity. /// </summary> /// <param name="entityName">The Logical Name of the target entity.</param> /// <param name="entityId">The GUID that uniquely defines the target entity.</param> /// <param name="relationship">The Relationship to use for the association.</param> /// <param name="relatedEntities">A list of related EntityReference records to associate to the target.</param> public static void Associate(string entityName, Guid entityId, Relationship relationship, List<EntityReference> relatedEntities) { XmlDocument resultXml = GetResponse(GetAssociateRequest(entityName,entityId,relationship,relatedEntities), "Execute", null); // Tidy up Script.Literal("delete {0}", resultXml); resultXml = null; }
/// <summary> /// Checks for an existing N:N relationship between two records by executing a fetch against the relationship /// association table. /// </summary> /// <param name="relationship">The Relationship to evaluate.</param> /// <param name="Entity1">EntityReference for the one of the entities to test.</param> /// <param name="Entity2">EntityReference for the second entity to test.</param> /// <returns>Boolean true if Entity1 and Entity2 have an existing relationship.</returns> public static bool DoesNNAssociationExist(Relationship relationship, EntityReference Entity1, EntityReference Entity2) { string fetchXml = "<fetch mapping='logical'>" + " <entity name='" + relationship.SchemaName + "'>" + " <all-attributes />" + " <filter>" + " <condition attribute='" + Entity1.LogicalName + "id' operator='eq' value ='" + Entity1.Id.Value + "' />" + " <condition attribute='" + Entity2.LogicalName + "id' operator='eq' value='" + Entity2.Id.Value + "' />" + " </filter>" + " </entity>" + "</fetch>"; EntityCollection result = OrganizationServiceProxy.RetrieveMultiple(fetchXml); if (result.Entities.Count > 0) return true; return false; }
public static void BeginAssociate(string entityName, Guid entityId, Relationship relationship, List<EntityReference> relatedEntities, Action<object> callBack) { GetResponse(GetAssociateRequest(entityName, entityId, relationship, relatedEntities), "Execute", callBack); }
private static string GetDisassociateRequest(string entityName, Guid entityId, Relationship relationship, List<EntityReference> relatedEntities) { //convert the list of related entites into a snippet of xml that can be inserted into the soap body. string entityReferences = ""; foreach (EntityReference item in relatedEntities) { entityReferences += item.ToSoap("a"); } string xmlSoapBody = "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"; xmlSoapBody += " <request i:type=\"a:DisassociateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">"; xmlSoapBody += " <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">"; xmlSoapBody += " <a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <b:key>Target</b:key>"; xmlSoapBody += " <b:value i:type=\"a:EntityReference\">"; xmlSoapBody += " <a:Id>" + entityId.Value + "</a:Id>"; xmlSoapBody += " <a:LogicalName>" + entityName + "</a:LogicalName>"; xmlSoapBody += " <a:Name i:nil=\"true\" />"; xmlSoapBody += " </b:value>"; xmlSoapBody += " </a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <b:key>Relationship</b:key>"; xmlSoapBody += " <b:value i:type=\"a:Relationship\">"; xmlSoapBody += " <a:PrimaryEntityRole i:nil=\"true\" />"; xmlSoapBody += " <a:SchemaName>" + relationship.SchemaName + "</a:SchemaName>"; xmlSoapBody += " </b:value>"; xmlSoapBody += " </a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <b:key>RelatedEntities</b:key>"; xmlSoapBody += " <b:value i:type=\"a:EntityReferenceCollection\">" + entityReferences + "</b:value>"; xmlSoapBody += " </a:KeyValuePairOfstringanyType>"; xmlSoapBody += " </a:Parameters>"; xmlSoapBody += " <a:RequestId i:nil=\"true\" />"; xmlSoapBody += " <a:RequestName>Disassociate</a:RequestName>"; xmlSoapBody += " </request>"; xmlSoapBody += " </Execute>"; return xmlSoapBody; }
/// <summary> /// Issues an <c>UpdateCompoundRequest</c> to the target <c>CrmService</c>. /// </summary> /// <param name="entity">The <c>Entity</c> to be created.</param> /// <param name="children">The <c>Array</c> of <c>Entity</c> objects that contains the children.</param> protected void UpdateCompoundEntity(Entity entity, Entity[] children) { if (entity == null) { throw new AdapterException(string.Format(CultureInfo.CurrentCulture, Resources.ArgumentNullExceptionMessage)) { ExceptionId = AdapterException.SystemExceptionGuid }; } this.PrepEntityForUpdate(entity); Relationship linesRelationship = new Relationship((entity.LogicalName == "salesorder" ? "order" : entity.LogicalName) + "_details"); CreateNewLines(children); entity.RelatedEntities.Add(linesRelationship, new EntityCollection(children)); UpdateRequest request = new UpdateRequest() { Target = entity }; this.CallCrmExecuteWebMethod(request); }
private static string GetDisassociateRequest(string entityName, Guid entityId, Relationship relationship, List <EntityReference> relatedEntities) { //convert the list of related entites into a snippet of xml that can be inserted into the soap body. string entityReferences = ""; foreach (EntityReference item in relatedEntities) { entityReferences += item.ToSoap("a"); } string xmlSoapBody = "<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"; xmlSoapBody += " <request i:type=\"a:DisassociateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">"; xmlSoapBody += " <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">"; xmlSoapBody += " <a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <b:key>Target</b:key>"; xmlSoapBody += " <b:value i:type=\"a:EntityReference\">"; xmlSoapBody += " <a:Id>" + entityId.Value + "</a:Id>"; xmlSoapBody += " <a:LogicalName>" + entityName + "</a:LogicalName>"; xmlSoapBody += " <a:Name i:nil=\"true\" />"; xmlSoapBody += " </b:value>"; xmlSoapBody += " </a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <b:key>Relationship</b:key>"; xmlSoapBody += " <b:value i:type=\"a:Relationship\">"; xmlSoapBody += " <a:PrimaryEntityRole i:nil=\"true\" />"; xmlSoapBody += " <a:SchemaName>" + relationship.SchemaName + "</a:SchemaName>"; xmlSoapBody += " </b:value>"; xmlSoapBody += " </a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <a:KeyValuePairOfstringanyType>"; xmlSoapBody += " <b:key>RelatedEntities</b:key>"; xmlSoapBody += " <b:value i:type=\"a:EntityReferenceCollection\">" + entityReferences + "</b:value>"; xmlSoapBody += " </a:KeyValuePairOfstringanyType>"; xmlSoapBody += " </a:Parameters>"; xmlSoapBody += " <a:RequestId i:nil=\"true\" />"; xmlSoapBody += " <a:RequestName>Disassociate</a:RequestName>"; xmlSoapBody += " </request>"; xmlSoapBody += " </Execute>"; return(xmlSoapBody); }
public static void BeginDisassociate(string entityName, Guid entityId, Relationship relationship, List <EntityReference> relatedEntities, Action <object> callBack) { GetResponse(GetDisassociateRequest(entityName, entityId, relationship, relatedEntities), "Execute", callBack); }
/// <summary> /// Set the attribute values from SDK Webservice response on a business entity /// </summary> /// <param name="entityNode"></param> public void DeSerialise(XmlNode entityNode) { this.LogicalName = XmlHelper.SelectSingleNodeValue(entityNode, "LogicalName"); this.Id = XmlHelper.SelectSingleNodeValue(entityNode, "Id"); XmlNode attributes = XmlHelper.SelectSingleNode(entityNode,"Attributes"); int attributeCount = attributes.ChildNodes.Count; for (int i = 0; i < attributeCount; i++) { XmlNode node = attributes.ChildNodes[i]; try { // Add typed attribute value string attributeName = XmlHelper.SelectSingleNodeValue(node, "key"); object attributeValue = Attribute.DeSerialise(XmlHelper.SelectSingleNode(node, "value"),null); this._attributes[attributeName] = attributeValue; SetDictionaryValue(attributeName, attributeValue); } catch (Exception e) { throw new Exception("Invalid Attribute Value :" + XmlHelper.GetNodeTextValue(node) + ":" + e.Message); } } // Get Formatted values XmlNode formattedValues = XmlHelper.SelectSingleNode(entityNode, "FormattedValues"); if (formattedValues != null) { for (int i = 0; i < formattedValues.ChildNodes.Count;i++ ) { XmlNode node = formattedValues.ChildNodes[i]; string key = XmlHelper.SelectSingleNodeValue(node, "key"); string value = XmlHelper.SelectSingleNodeValue(node, "value"); SetDictionaryValue(key+"name", value); FormattedValues[key+"name"] = value; object att = this._attributes[key]; if (att != null) { Script.Literal("{0}.name={1}", att, value); } } } // Get related entities XmlNode relatedEntities = XmlHelper.SelectSingleNode(entityNode, "RelatedEntities"); if (relatedEntities != null) { Dictionary<string,EntityCollection> relatedEntitiesColection = new Dictionary<string,EntityCollection>(); for (int i = 0; i < relatedEntities.ChildNodes.Count; i++) { XmlNode node = relatedEntities.ChildNodes[i]; XmlNode key = XmlHelper.SelectSingleNode(node, "key"); string schemaName = XmlHelper.SelectSingleNodeValue(key, "SchemaName"); Relationship relationship = new Relationship(schemaName); XmlNode value = XmlHelper.SelectSingleNode(node, "value"); EntityCollection entities = EntityCollection.DeSerialise(value); relatedEntitiesColection[relationship.SchemaName] = entities; } this.RelatedEntities = relatedEntitiesColection; } }