简体   繁体   English

Sitefinity从代码获取当前的dynamiccontent项目

[英]Sitefinity get current dynamiccontent item from code

I'm currently writing a site using Sitefinity CMS. 我目前正在使用Sitefinity CMS编写网站。 Can someone please explain how to get the current dynamic content item from server side code on page_load? 有人可以解释一下如何从page_load的服务器端代码中获取当前的动态内容吗?

I have written a user control to display a custom gallery of sliding images. 我编写了一个用户控件来显示自定义的滑动图像库。 There are multiple content types in my dynamic module. 我的动态模块中有多种内容类型。 The user control will sit as part of the masterpage template rather than on every page. 用户控件将作为母版模板的一部分而不是每个页面。 On each page load I would like to fetch the current dynamiccontent item that is associated with the page and examine whether it has a property with the name 'Gallery'. 在每次加载页面时,我想获取与页面关联的当前dynamiccontent项,并检查其是否具有名称为“ Gallery”的属性。 If so I would then extract the images and render them via the usercontrol. 如果是这样,我将提取图像并通过usercontrol渲染它们。

Thanks, 谢谢,

Brian. 布莱恩

I'm assuming your images are related content. 我假设您的图片是相关内容。 This gets every published content item of your type. 这将获取您类型的所有已发布内容项。

var dynamicModuleManager = DynamicModuleManager.GetManager();
var moduleType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.YOURTYPEHERE");

var dcItems = dynamicModuleManager.GetDataItems(moduleType)
            .Where(l => l.Status == ContentLifecycleStatus.Master);

foreach (var dcItem in dcItems)
{
   //pass the dynamic content item to a model constructor or populate here, then
   //   get your image this way:

   var image = dcItem.GetRelatedItems<Image>("Images").SingleOrDefault();
   if (image != null)
   {
      ImageUrl = image.MediaUrl;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM