我目前正在努力从动态模块项集合中获取图像数据。
我尝试过搜索各种资源,但似乎无法找到解决方案。
我有一个IQueryable类型,它包含一组动态模块项。然后我使用LINQ select转换此集合以过滤数据并返回自定义类型。请参阅以下内容:
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系列上查询图片数据,但仅在使用单个项目时才有任何想法?
谢谢大家!
答案 0 :(得分:3)
基于Sitefinity文档(http://docs.sitefinity.com/for-developers-related-data-api):
使用相关数据API时,您需要使用主数据库 您所关注的相关数据项和项目的版本 建立关系。
问题是当您查询集合collection = (Query to Sitefinity for my custom dynamic module items);
时,您没有按主版本进行过滤。
在您的情况下,有两种解决方案:
1)仅针对主
的过滤器集合collection = collection.Where(i=>i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master);
2)每个Live版本都会收到它的主人
var masterItem = dynamicModuleManager.Lifecycle.GetMaster(itemLive);
P.S。它适用于collection.FirstOrDefault().GetRelatedItems<Image>("Image")
,因为集合中的第一个元素是Master
P.P.S。 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
}
}