我目前正在努力从动态模块项目集中获取图像数据。
我尝试搜索各种资源,但似乎仍然找不到解决方案。
我有一个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集合上的图像数据,而仅当使用单个项目时有什么想法吗?
谢谢你们!
最佳答案
根据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版本,它都是Master
var masterItem = dynamicModuleManager.Lifecycle.GetMaster(itemLive);
附言它适用于
collection.FirstOrDefault().GetRelatedItems<Image>("Image")
,因为集合中的第一个元素是MasterP.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
}
}
关于c# - 如何从Sitefinity 10中的动态模块集合中检索图像数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43852850/