コード例 #1
0
        public override void ExecuteExtention(AddPortalCodeRequest request, AddPortalCodeResponse response,
                                              ServiceRequestController controller)
        {
            var project = VisualStudioService.GetProject(request.ProjectName);

            var exportConfigs = AddPortalCodeConfiguration.GetExportConfigs()
                                .Where(c => request.IncludeType(c.RecordType))
                                .ToArray();
            var toDo = exportConfigs.Count();
            var done = 0;

            //okay so lets iterate the configurations
            //and for each item not null add somehting to the vs solution
            foreach (var config in exportConfigs)
            {
                controller.UpdateProgress(done++, toDo, "Exporting " + config.RecordType + " Code");
                var results = AddPortalCodeConfiguration.GetRecordsForConfig(config.RecordType, Service, request.WebSite.Id);
                results = request.FilterInclusionForType(config.RecordType, results);
                var toDo2 = results.Count();
                var done2 = 0;
                foreach (var result in results)
                {
                    ++done2;
                    if (config.RecordType == Entities.adx_webfile)
                    {
                        controller.UpdateLevel2Progress(done2, toDo2, $"Processing {done2}/{toDo2}");
                    }
                    var recordName           = result.GetStringField(Service.GetPrimaryField(config.RecordType));
                    var thisTypesFolderLabel = Service.GetDisplayName(config.RecordType);
                    var path = new List <string>();
                    if (request.CreateFolderForWebsiteName)
                    {
                        path.Add(request.WebSite.Name);
                    }
                    path.Add(thisTypesFolderLabel);
                    try
                    {
                        if (config.RecordType == Entities.adx_webfile)
                        {
                            var notes = Service.GetLinkedRecords(Entities.annotation, config.RecordType, Fields.annotation_.objectid, result.Id);
                            if (notes.Count() > 1)
                            {
                                throw new Exception("There is more than 1 note attached to the file. You will need to delete the excess notes to export");
                            }
                            else if (notes.Any() || request.ExportWhereFieldEmpty)
                            {
                                string fileContent = notes.Any()
                                    ? notes.First().GetStringField(Fields.annotation_.documentbody)
                                    : null;
                                if (fileContent != null)
                                {
                                    var data = Convert.FromBase64String(fileContent);
                                    fileContent = Encoding.UTF8.GetString(data);
                                }
                                var fileName = recordName;
                                if (IncludeExtention(request, fileName))
                                {
                                    project.AddItem(fileName, fileContent, path.ToArray());
                                }
                            }
                        }
                        else
                        {
                            foreach (var field in config.FieldsToExport)
                            {
                                var content = result.GetStringField(field.FieldName);
                                if (content != null || request.ExportWhereFieldEmpty)
                                {
                                    var fileExtention = field.Extention;
                                    var fileName      = recordName + "." + fileExtention;
                                    //https://superuser.com/questions/187469/how-would-i-go-about-creating-a-filename-with-invalid-characters-such-as
                                    // / -you can use a division symbol ∕
                                    // : -you can use the modifier letter colon ꞉
                                    fileName = fileName?.Replace("/", "∕");
                                    fileName = fileName?.Replace(":", "꞉");
                                    if (IncludeExtention(request, fileName))
                                    {
                                        project.AddItem(fileName, content, path.ToArray());
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        response.AddResponseItem(new AddPortalCodeResponseItem()
                        {
                            RecordType = config.RecordType, RecordName = recordName, Exception = ex
                        });
                    }
                }
                controller.TurnOffLevel2();
            }
            response.Message = "Export Completed";
        }