public void AddUniqueFieldConfigJoins(Entity thisEntity, QueryExpression matchQuery, IEnumerable <string> uniqueFields, string prefixFieldInEntity = null) { foreach (var field in uniqueFields) { var theValue = thisEntity.GetFieldValue(prefixFieldInEntity + field); if (theValue == null) { matchQuery.Criteria.AddCondition(new ConditionExpression(field, ConditionOperator.Null)); } else if (theValue is EntityReference) { var name = XrmEntity.GetLookupName(theValue); var type = XrmEntity.GetLookupType(theValue); var linkToReferenced = matchQuery.AddLink(type, field, XrmService.GetPrimaryKeyField(type)); if (name == null) { linkToReferenced.LinkCriteria.AddCondition(XrmService.GetPrimaryNameField(type), ConditionOperator.Null); } else { linkToReferenced.LinkCriteria.AddCondition(XrmService.GetPrimaryNameField(type), ConditionOperator.Equal, name); if (ContainsExportedConfigFields) { AddReferenceConfigJoins(linkToReferenced, thisEntity, field); } } } else { matchQuery.Criteria.AddCondition(new ConditionExpression(field, ConditionOperator.Equal, XrmService.ConvertToQueryValue(field, matchQuery.EntityName, theValue))); } } }
private void MoveAttachmentsToSharepoint() { if (IsMessage(PluginMessage.Create, PluginMessage.Update) && IsStage(PluginStage.PostEvent) && IsMode(PluginMode.Asynchronous)) { if (!GetBoolean(Fields.email_.directioncode) && FieldChanging(Fields.email_.regardingobjectid)) { if (GetLookupType(Fields.email_.regardingobjectid) == Entities.incident) { if (IsMessage(PluginMessage.Create)) { //lets just snooze for 10 seconds if this is a create message //in case attachments are added after the initial create message Thread.Sleep(10000); } var documentExtentions = new[] { "pdf", "doc", "docx" }; var attachmentQuery = XrmService.BuildQuery(Entities.activitymimeattachment, null, new[] { new ConditionExpression(Fields.activitymimeattachment_.activityid, ConditionOperator.Equal, TargetId) }); var typeFilter = attachmentQuery.Criteria.AddFilter(LogicalOperator.Or); typeFilter.Conditions.AddRange(documentExtentions.Select(de => new ConditionExpression(Fields.activitymimeattachment_.filename, ConditionOperator.EndsWith, de))); var attachmentsToMove = XrmService.RetrieveAll(attachmentQuery); if (attachmentsToMove.Any()) { var regardingObjectId = GetLookupGuid(Fields.email_.regardingobjectid); var regardingType = GetLookupType(Fields.email_.regardingobjectid); var regardingName = XrmEntity.GetLookupName(XrmService.LookupField(TargetType, TargetId, Fields.email_.regardingobjectid)); //get a sharepoint folder for the target location record var documentFolder = SharepointService.GetOrCreateDocumentFolderRelativeUrl(regardingType, regardingObjectId.Value, regardingName); foreach (var item in attachmentsToMove) { var fileName = item.GetStringField(Fields.activitymimeattachment_.filename); //if a txt file could potentially have already been moved so lets just leave txt files in crm if (fileName != null && !fileName.EndsWith(".txt")) { var fileContent = item.GetStringField(Fields.activitymimeattachment_.body); //load the attachment to sharepoint var documentUrl = SharepointService.UploadDocument(documentFolder, fileName, fileContent, "[EmailFile]", item.Id.ToString().ToUpper().Replace("{", "").Replace("}", "").Replace("-", "")); ////update the attachment to a text file with the sharepoint url //item.SetField(Fields.activitymimeattachment_.filename, fileName + ".txt"); //item.SetField(Fields.activitymimeattachment_.body, Convert.ToBase64String(("Moved to sharepoint - " + documentUrl).ToByteArray())); //XrmService.Update(item, new[] { Fields.activitymimeattachment_.filename, Fields.activitymimeattachment_.body }); } } } } } } }
private void AddReferenceConfigJoins(LinkEntity linkToReferenced, Entity thisEntity, string field) { var referencedType = XrmEntity.GetLookupType(thisEntity.GetFieldValue(field)); var referencedTypeConfig = XrmRecordService.GetTypeConfigs().GetFor(referencedType); if (referencedTypeConfig != null && referencedTypeConfig.UniqueChildFields != null) { foreach (var uniqueField in referencedTypeConfig.UniqueChildFields) { var theValue = thisEntity.GetFieldValue($"{field}.{uniqueField}"); if (theValue == null) { linkToReferenced.LinkCriteria.AddCondition(new ConditionExpression(uniqueField, ConditionOperator.Null)); } else if (theValue is EntityReference) { var name = XrmEntity.GetLookupName(theValue); var type = XrmEntity.GetLookupType(theValue); var nextLinkToReferenced = linkToReferenced.AddLink(type, uniqueField, XrmService.GetPrimaryKeyField(type)); if (name == null) { nextLinkToReferenced.LinkCriteria.AddCondition(XrmService.GetPrimaryNameField(type), ConditionOperator.Null); } else { nextLinkToReferenced.LinkCriteria.AddCondition(XrmService.GetPrimaryNameField(type), ConditionOperator.Equal, name); AddReferenceConfigJoins(nextLinkToReferenced, thisEntity, $"{field}.{uniqueField}"); } } else { linkToReferenced.LinkCriteria.AddCondition(new ConditionExpression(uniqueField, ConditionOperator.Equal, XrmService.ConvertToQueryValue(uniqueField, referencedType, theValue))); } } } }