简体   繁体   中英

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

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.. ie 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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