public Url GetUrl()
        {
            var validUrlType = new[] { Entities.workflow };

            if (TargetId != null && TargetType != null && validUrlType.Contains(TargetType))
            {
                return(new Url(XrmRecordService.GetWebUrl(TargetType, TargetId), "Open"));
            }
            return(null);
        }
        public override void ExecuteExtention(DeployIntoFieldRequest request, DeployIntoFieldResponse response,
                                              ServiceRequestController controller)
        {
            var records = new List <IRecord>();

            var publishIds = new List <string>();

            var numberToDo = request.Files.Count();
            var numberDone = 0;

            foreach (var file in request.Files.Select(f => f?.Replace("∕", "∕")))
            {
                var fileInfo = new FileInfo(file);

                controller.UpdateProgress(++numberDone, numberToDo, "Importing " + fileInfo.Name);
                var thisResponseItem = new DeployIntoFieldResponseItem()
                {
                    Name = fileInfo.Name
                };
                response.AddResponseItem(thisResponseItem);

                try
                {
                    var containingFolderName       = fileInfo.Directory.Name;
                    var containingFolderParentName = fileInfo.Directory.Parent != null
                        ? fileInfo.Directory.Parent.Name
                        : null;
                    //get target record type
                    string recordType = null;
                    if (Service.GetAllRecordTypes().Any(r => r == containingFolderName))
                    {
                        recordType = Service.GetAllRecordTypes().First(r => r == containingFolderName);
                    }
                    else if (Service.GetAllRecordTypes().Any(r => Service.GetDisplayName(r)?.ToLower() == containingFolderName.ToLower()))
                    {
                        recordType = Service.GetAllRecordTypes().First(r => Service.GetDisplayName(r)?.ToLower() == containingFolderName.ToLower());
                    }
                    else
                    {
                        throw new NullReferenceException($"Could not find matching type by logical or display name for folder name of {containingFolderName}");
                    }

                    thisResponseItem.RecordType = recordType;

                    if (recordType == Entities.adx_webfile)
                    {
                        //this one goes into an attachment
                        var matchingRecord = GetRecordToDeployInto(recordType, fileInfo.Name, containingFolderParentName);
                        if (matchingRecord == null)
                        {
                            throw new NullReferenceException($"There is no {Service.GetDisplayName(recordType)} record name {fileInfo.Name} to load the file attachment into");
                        }
                        thisResponseItem.Link = new Url(Service.GetWebUrl(matchingRecord.Type, matchingRecord.Id), "Open");
                        //get matching attachment by name else create a new one
                        var fileAttachments = Service.RetrieveAllAndClauses(Entities.annotation, new[]
                        {
                            new Condition(Fields.annotation_.filename, ConditionType.Equal, fileInfo.Name),
                            new Condition(Fields.annotation_.objectid, ConditionType.Equal, matchingRecord.Id)
                        }).OrderBy(n => n.GetDateTime(Fields.annotation_.createdon)).ToArray();

                        var contentBytes        = File.ReadAllBytes(file);
                        var contentBase64String = Convert.ToBase64String(contentBytes);
                        if (fileAttachments.Any())
                        {
                            //lets update the last modifed one
                            var attachmentToUpdate = fileAttachments.First();
                            if (attachmentToUpdate.GetStringField(Fields.annotation_.documentbody) != contentBase64String)
                            {
                                attachmentToUpdate.SetField(Fields.annotation_.documentbody, contentBase64String, Service);
                                Service.Update(attachmentToUpdate, new[] { Fields.annotation_.documentbody });
                                thisResponseItem.Updated = true;
                            }
                        }
                        else
                        {
                            //lets create a new attachment
                            var newAttachment = Service.NewRecord(Entities.annotation);
                            newAttachment.SetLookup(Fields.annotation_.objectid, matchingRecord.Id, matchingRecord.Type);
                            newAttachment.SetField(Fields.annotation_.subject, fileInfo.Name, Service);
                            newAttachment.SetField(Fields.annotation_.filename, fileInfo.Name, Service);
                            newAttachment.SetField(Fields.annotation_.documentbody, contentBase64String, Service);
                            Service.Create(newAttachment);
                            thisResponseItem.Created = true;
                        }
                    }
                    else
                    {
                        //get the record with the same name as the file
                        var nameToMatch = fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf("."));

                        var matchingRecord = GetRecordToDeployInto(recordType, nameToMatch, containingFolderParentName);
                        if (matchingRecord == null)
                        {
                            throw new NullReferenceException($"There is no {Service.GetDisplayName(recordType)} record name {fileInfo.Name} to load the file attachment into");
                        }
                        thisResponseItem.Link = new Url(Service.GetWebUrl(matchingRecord.Type, matchingRecord.Id), "Open");
                        var targetField = GetTargetField(fileInfo, recordType);

                        var contentText = File.ReadAllText(file);
                        if (matchingRecord.GetStringField(targetField) != contentText)
                        {
                            matchingRecord.SetField(targetField, contentText, Service);
                            Service.Update(matchingRecord, new[] { targetField });
                            thisResponseItem.Updated = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    thisResponseItem.Exception = ex;
                }
            }
        }