【问题标题】:How to retrieve image data from a dynamic module collection in Sitefinity 10?如何从 Sitefinity 10 中的动态模块集合中检索图像数据?
【发布时间】:2017-05-08 16:34:51
【问题描述】:

我目前正在努力从我的动态模块项目集合中获取图像数据。

我已尝试搜索各种资源,但似乎仍然找不到解决方案。

我有一个 IQueryable 类型,它包含一组动态模块项。然后,我使用 LINQ 选择转换此集合以过滤数据并返回自定义类型。请参阅以下内容:

IQueryable<DynamicContent> collection = (Query to Sitefinity for my custom dynamic module items);

return collection.Select(b => new CustomType()
{
   Title = b.GetValue<string>("Title"),
   Body = b.GetValue<string>("Body"),
   ExternalLink = b.GetValue<string>("ExternalLink"),
   Image = b.GetRelatedItems<Image>("Image")
});

当我尝试上述所有其他属性时,除了返回空 Image 对象的 Image 属性之外,所有其他属性都会被填充。但是当我使用单个项目时:

 collection.FirstOrDefault().GetRelatedItems<Image>("Image") 

上面将返回一个 Image 对象。

不知道为什么我不能在 IQueryable 集合上查询图像数据,但只能在使用单个项目时查询,有什么想法吗?

谢谢大家!

【问题讨论】:

    标签: c# linq telerik sitefinity


    【解决方案1】:

    基于 Sitefinity 文档 (http://docs.sitefinity.com/for-developers-related-data-api):

    与相关数据API一起使用时,需要与master一起使用 相关数据项和您所在的项的版本 创建关系。

    问题是当你查询集合collection = (Query to Sitefinity for my custom dynamic module items);时,你没有按Master版本过滤。

    在您的情况下,有两种解决方案:

    1) 过滤器集合仅用于 master

    collection = collection.Where(i=>i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master);
    

    2) 对于每个 Live 版本,都会收到它的 Master

    var masterItem = dynamicModuleManager.Lifecycle.GetMaster(itemLive);
    

    附:它适用于 collection.FirstOrDefault().GetRelatedItems&lt;Image&gt;("Image"),因为集合中的第一个元素是 Master

    附言GetRelatedItems 会减慢您的查询速度,这是使用 ContentLinks API 的最佳方式,它快很多倍。示例:

    var contentLinksManager = ContentLinksManager.GetManager();
    var librariesManager= LibrariesManager.GetManager();
    var masterId = data.OriginalContentId; //IF data is Live status or data.Id if is Master status
    var imageFileLink = contentLinksManager.GetContentLinks().FirstOrDefault(cl=>cl.ParentItemId == masterId && cl.ComponentPropertyName == "Image");
    if (imageFileLink != null)
    {
        var image= librariesManager.GetImage(imageFileLink.ChildItemId);
        if (image!= null)
        {
           // Work with image object
        }
    }
    

    【讨论】:

    • 非常感谢您的深入回复,等我回去工作我会尝试以上方法。一个快速的问题,是否有任何性能测试显示内容链接 api 比相关项目更快我认为引入相关项目来替换内容链接 api?
    • 你可以在这里找到带有测试号的文章:americaneagle.com/blog/web-development-blog/staff/2015/04/30/… 它已经很老了,但仍然适用于 Sitefinity 10
    • 再次感谢您分享上面的链接,我确实偶然发现了您刚才提到但没有意识到的文章!
    • 如果我的解决方案对您有用,请您接受我的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多