Get DocumentName in transformation having GUID

Miguel Vieira asked on July 3, 2015 14:00

How do I get the blogpost author name using GUID?

Before i had this code using the nodeID, but i can't find how to get it using guid.

 public static string GetBlogPostAuthorName(object value)
{
    int nodeId = ValidationHelper.GetInteger(value, 0);
    if (nodeId <= 0)
        return string.Empty;


    TreeProvider tree = new TreeProvider(SiteContext.CurrentUser);

    TreeNode node = tree.SelectSingleNode(nodeId, DocumentContext.CurrentDocumentCulture.CultureCode);

    if (node != null)
    {
        return node.DocumentName;
    }
    return string.Empty;
}

In the BlogPost Page types i created a new field called BlogAuthor with the Data Type "Unique Identifier (GUID)" and Form Control is a Dropdown List.

In this part everything works fine, it saves in the Database as it should, but now i need to create a custom function to get the DocumentName in a transformation. Any help?

Recent Answers


Roman Hutnyk answered on July 3, 2015 16:50

Kentico 8 has great feature called data query. You might use it with linq:

TreeNode doc = DocumentHelper.GetDocuments()
.Where(d => d.DocumentGUID = yourValue)
.FirstOrDefault();

Hope this helps.

0 votesVote for this answer Mark as a Correct answer

Virgil Carroll answered on July 3, 2015 16:58

Just to clarify, you want to get the Blog Author's name or the DocumentName? (you said both in your post).

Assuming you meant to get Blog Author and this is the person who created the Page, then you can get this through the NodeOwner or pass the DocumentID to the DocumentContext and get the CurrentDocumentOwner (which will be the UserInfo object that you could get the users name)

0 votesVote for this answer Mark as a Correct answer

Miguel Vieira answered on July 3, 2015 17:03

I need to get the DocumentName, the author is the person who created, the author is a person from the authors section pages

0 votesVote for this answer Mark as a Correct answer

Miguel Vieira answered on July 3, 2015 17:08

I tried this code

 public static string GetBlogPostAuthorName2(object value)
{
    Guid autorguid = ValidationHelper.GetGuid(value, Guid.NewGuid(), System.Globalization.CultureInfo.CurrentCulture);

    TreeProvider tree = new TreeProvider(SiteContext.CurrentUser);

    TreeNode node = tree.SelectSingleNode(autorguid, DocumentContext.CurrentDocumentCulture.CultureCode, SiteContext.CurrentSiteName);

    if (node != null)
    {
        return node.DocumentName;
    }
    return "teste";
}

autorguid has the GUID, but when it gets to Treenode its always null

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on July 3, 2015 19:44

So you're trying to get an Article by author GUID, correct? I can assume that AuthorGUID is a field of CMS.Article page type. Try this:

var docs = DocumentHelper.GetDocuments(SiteContext.CurrentSiteName,"/%","",true,"cms.article",
string.Format("AuthorGUID={0}",guid),"",-1,true,new TreeProvider())

This selects documents along with the coupled data (Article in this case), which allows you to filter by coupled data fields.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.