[英]How to retrieve image data from a dynamic module collection in Sitefinity 10?
我目前正在努力從動態模塊項集合中獲取圖像數據。
我試過搜索各種資源,但似乎仍無法找到解決方案。
我有一個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)僅針對master進行過濾器收集
collection = collection.Where(i=>i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Master);
2)對於每個Live版本,接收它的主人
var masterItem = dynamicModuleManager.Lifecycle.GetMaster(itemLive);
PS它適用於collection.FirstOrDefault().GetRelatedItems<Image>("Image")
因為集合中的第一個元素是Master
PPS 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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.