umbraco API: trying to get the document type data for a given site node

和自甴很熟 提交于 2020-01-06 19:42:06

问题


Just trying to get some perspective about how to retrieve data from umbraco via the API method. I believe we are using umbraco 4.9.x.

Basically there is a data type called DiaryEventItems, and I use the following code to access this:

// Get the ID of the data type
DocumentType DocTypeDiaryEvents = DocumentType.GetByAlias("DiaryEventItems");

// Loop through those items using a foreach at present
foreach (Document DiaryEvent in Document.GetDocumentsOfDocumentType(DocTypeDiaryEvents.Id))
{
    // Do whatever I need to
}

So this works well.. I get back the collection/rows of "DiaryEventItems", however I get ALL DiaryEventItems from the umbraco instance of course.. i.e. for all sites. So obviously there are methods to get the site root node ID and perhaps work down the tree to get the actual document type I need, however is there some way of doing this which is similar to the above code?

Any help appreciated thanks!


回答1:


you can try following function for only Published node:

// this is variable to retrieve Node list
private static List<Node> listNode = new List<Node>();

public static List<Node> GetDescendantOrSelfNodeList(Node node, string nodeTypeAlias)
{
    if (node.NodeTypeAlias == nodeTypeAlias)
        listNode.Add(node);

    foreach (Node childNode in node.Children)
    {
        GetDescendantOrSelfNodeList(childNode, nodeTypeAlias);
    }

    return listNode;
}

now you can call that function in your code as below:

// 1234 would be root node id
Node rootNode = new Node(1234)

// we are passing root node so that it can search through nodes with alias as DiaryEventItems
List<Node> diaryEventItems = GetDescendantOrSelfNodeList(rootNode, "DiaryEventItems");

I hope this would help, if you looking for unpublished node with Document and its different and will be taking little bit time for me but if you want unpublished node only then i'll do that bit later.



来源:https://stackoverflow.com/questions/12620331/umbraco-api-trying-to-get-the-document-type-data-for-a-given-site-node

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!