【问题标题】:Access attachments in children of a page - Kentico访问页面子项中的附件 - Kentico
【发布时间】:2020-01-10 02:45:43
【问题描述】:

我有以下结构:

A国
|_问卷1
|_结果 1
B国
|_问卷 3
|_结果 3
C国
|_问卷 5
|_结果 5

国家?属于 CMS.folder 页面类型,调查问卷和结果都属于 CMS.file 页面类型并包含一个附件 (PDF)。 我正在尝试访问文件夹中每个可用出版物中附件的详细信息(名称、Guid、大小)。

我已经尝试了以下

            .Select(m => new
            {
                Country = m.DocumentName,
                questionnaire = m.Children.WithAllData.Where(w => w.DocumentName.Contains("Questionnaire")).Select(s => s.GetValue("PDF")),
                result = m.Children.WithAllData.Where(w => w.DocumentName.Contains("Result")).Select(s => s.GetValue("PDF"))    
            })
            .ToList();

我可以在每个文件夹的调查问卷文件中获取附件的 GUID,但我无法获得结果的值,因为 WithAllData 重复两次似乎阻止了这一点。 我怎样才能访问附件的大小和名称?我尝试包含AttachmentSize or AttachmentName,但我没有成功处理当前节点的子节点。

什么是做我想做的最好的方法?

---------------- 更新 ------------------

按照建议,这是我尝试过的:

.Select(m => new
            {
                Country = m.DocumentName,
                questionnaire = GetDocs(m.Children.Where(w => w.DocumentName.Contains("Questionnaire")).FirstOrDefault()),
                result = GetDocs(m.Children.Where(w => w.DocumentName.Contains("Result")).FirstOrDefault())    
            })
            .ToList();

private PublicationSimpleDto GetDocs(TreeNode tree)
        {
            PublicationSimpleDto publication = null;
            if (tree != null)
            {
                foreach (DocumentAttachment attachment in tree.Attachments)
                {
                    publication = new PublicationSimpleDto()
                    {
                        Title = attachment.AttachmentTitle,
                        Extension = attachment.AttachmentExtension.Replace(".", "").ToUpper(),
                        AttachmentUrl = attachment.AttachmentGUID.ToString(),
                        Size = attachment.AttachmentSize
                    };
                }
            }
            return publication;
        }

但是,它没有捕捉到结果之一,我似乎无法重复.children 几次。这是同样的问题:

questionnaire = m.Children.WithAllData.Where(w => w.DocumentName.Contains("Questionnaire")).Select(s => s.GetValue("PDF")),
                result = m.Children.WithAllData.Where(w => w.DocumentName.Contains("Result")).Select(s => s.GetValue("PDF"))

直接使用附件 GUID 而不是页面的 TreeNode 听起来更容易,但我无法这样做。

【问题讨论】:

  • 该页面的字段仅存储 Attachmend GUID - 因此,您需要再次调用以使用 GUID 获取附件信息,或者以某种方式使用您当前的代码将 GUID 传递给 DocumentHelper.GetAttachment方法将返回附件对象。
  • @jurajo 我已按照建议进行了编辑,但为什么我不能多次访问孩子。

标签: kentico kentico-mvc


【解决方案1】:

请看这个Kentico docs article 解释附件的工作。

您可以尝试以下选项:

  • 通过 DocumentHelper.GetAttachment 检索附件
  • 访问 document.Attachmets 属性(每个页面类型都有这个属性)

然后您将能够查看例如这样的大小:attachment.AttachmentSize

【讨论】:

  • 谢谢@dmitry-bastron,我已经按照建议进行了编辑,但是为什么在应用我的条件时我不能多次访问孩子。
猜你喜欢
  • 2020-09-23
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 2018-10-01
  • 2019-03-11
  • 1970-01-01
  • 2011-05-04
  • 1970-01-01
相关资源
最近更新 更多