简体   繁体   English

创建用户控件以在Umbraco 4.1中显示DocumentType的列表

[英]Create UserControl to display a list of DocumentType in Umbraco 4.1

I'm new to Umbraco and I like it so far, I understand how it works but I'd like to know how, and what is the best way, to create usercontrols that display some informations from umbraco's DB? 我是Umbraco的新手,到目前为止,我很喜欢它,我了解它的工作原理,但是我想知道如何创建用户控件以显示来自umbraco的数据库的某些信息,以及什么是最佳方法? When it's simple, I do it with XSL template but now I need more possibilities. 简单时,我用XSL模板来做,但是现在我需要更多的可能性。

What I try to do is have a UC that connect to Umbraco's DB, fetch all documents of documentType "NewsItem" and list them in my UC. 我想要做的是拥有一个连接到Umbraco的DB的UC,获取documentType“ NewsItem”的所有文档,并将它们列出在我的UC中。

I found this post : Umbraco: List Child Nodes in User Control but it's not quite it since I don't want to hardcode the nodeId, I want to find my news depending on DocumentType. 我发现了这篇文章: Umbraco:在用户控件中列出子节点,但事实并非如此,因为我不想对nodeId进行硬编码,我想根据DocumentType查找新闻。

I now that there's an API to acces umbraco's data but did not find any exemple. 我现在有一个可以访问umbraco数据的API,但没有找到任何示例。 I also watch lots of videos on umbraco.tv but still do not have a good idea of what's the best way to do it. 我还在umbraco.tv上观看了很多视频,但仍然不清楚什么是最好的方法。 There's also LINQ to Umbraco ( http://our.umbraco.org/wiki/reference/api-cheatsheet/linq-to-umbraco ) but not sure if it's a good way of doing this. 也有LINQ to Umbraco( http://our.umbraco.org/wiki/reference/api-cheatsheet/linq-to-umbraco ),但不确定这是否是一个好方法。

Also, is there a way to test the Usercontrol inside an other WebProject? 另外,有没有办法在另一个WebProject中测试Usercontrol? What I mean is to connect to Umbraco's db in an other project, so that you don't have to go in umbraco's website to test it? 我的意思是在另一个项目中连接到Umbraco的数据库,这样您就不必进入umbraco的网站进行测试了吗?

Thanks a lot! 非常感谢!

There are several areas to your question which I'll try and address one at a time. 您的问题有多个领域,我将尝试一次解决。

  1. Using umbraco.presentation.nodefactory to get Nodes of a specific type. 使用umbraco.presentation.nodefactory获取特定类型的Node。 For this example I'm going to assume all your NewsItems are children of a specific node in this case node id 1024. 对于此示例,我将假设您的所有NewsItems都是特定节点的子级,在这种情况下,节点ID为1024。

     using umbraco.presentation.nodeFactory; namespace cogworks.usercontrols { public partial class ExampleUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { var specificNode = new Node(1024); var childNodes = specificNode.Children; foreach(var node in childNodes) { if(node.NodeTypeAlias == "NewsItem") { //Do something with your NewsItem node! } } } } } 

This is probably not the most efficient way but is OK as an example. 这可能不是最有效的方法,但可以举一个例子。

  1. An example of walking the node tree recursively and adding the found nodes to a list: 递归遍历节点树并将找到的节点添加到列表的示例:

     public static List<Node> SelectChildrenByNameRecursive(Node node, string docType) { var nodes = new List<Node>(); foreach (Node child in node.Children) { FindChildrenByDocType(child, docType, ref nodes); } return nodes; } private static void FindChildrenByDocType(Node node, string docType, ref List<Node> nodes) { if (node.NodeTypeAlias == docType) { nodes.Add(node); } foreach (Node childNode in node.Children) { FindChildrenByDocType(childNode, docType, ref nodes); } } 

Again just example code... 再次只是示例代码...

  1. Testing Umbraco, you'll always need to be running in a an instance of Umbraco as nodefactory is an API on top of the in memory content cache. 测试Umbraco,您始终需要在Umbraco的实例中运行,因为nodefactory是内存内容缓存中的API。

  2. Further reading 进一步阅读

http://blog.hendyracher.co.uk/umbraco-helper-class/ http://blog.hendyracher.co.uk/umbraco-helper-class/

http://our.umbraco.org/wiki/how-tos/useful-helper-extension-methods-(linq-null-safe-access) http://our.umbraco.org/wiki/how-tos/useful-helper-extension-methods-(linq-null-safe-access)

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

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