How to get a PostTeaser URL or Guid in another document in Api?

Danuta Welz asked on April 1, 2019 14:14

I have a PostTeaser picture in every blog. I would like to get a PostTeaser URL or Guid in another document to show this teaser image. Macro CMSContext.Current.Documents["/Blogs/Blog/December/Article"].PostTeaser in System/Macros/Console works, It gives Guid. I try to use it in Api in Custom Web Part Layount: MacroResolver.Resolve("{% CMSContext.Current.Documents[\"/Blogs/Blog/December/Article\"].PostTeaser %}"); but it doesn't work. Is it possible to get guid or url in this way?

Recent Answers


Roman Hutnyk answered on April 1, 2019 14:51

This syntax should work: Documents.WithAllData["/Blogs/TotalTech-Blog/December-2018/Second-blog-post"].GetValue("BlogPostTeaser")

However I'm not sure it is a good approach to insert macro in a custom layout.

0 votesVote for this answer Mark as a Correct answer

Danuta Welz answered on April 1, 2019 15:24

Unfortunately, it also works in the console, but not in a custom layout. How do I get a Blog Post Teaser in a custom layout knowing the Blog alias patch? Maybe not a macro

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on April 1, 2019 15:30

You can write c# code within layout, just wrap it with <script runat="server">...</script> and you are good to go.

The only question I have is why would you need to do it within layout? Won't it be easier to use some viewer?

0 votesVote for this answer Mark as a Correct answer

Danuta Welz answered on April 1, 2019 16:17

I use a custom layout to prepare a custom web part. I need this for a reason.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 1, 2019 20:00 (last edited on April 1, 2019 20:45)

It could be easily macro permission thing. Take a look at this topic

Or you can do it directly in the code, not need for macro. Just use attachment API. The code below is not fully tested it just to give you the direction:)

(https://docs.kentico.com/api11/content-management/attachments)

<script runat="server">
protected void Page_PreRender(object sender, EventArgs e)
{
    CMS.DocumentEngine.TreeProvider tree = new CMS.DocumentEngine.TreeProvider(CMS.Membership.MembershipContext.AuthenticatedUser);

    // Gets a page
    CMS.DocumentEngine.TreeNode page = tree.SelectNodes()
        .Path("/News/%")
        .OnCurrentSite()
        .Culture("en-us")
        .FirstObject;


  string attachmentGUID = "";
    if (page != null)
    {
        foreach (CMS.DocumentEngine.DocumentAttachment attachment in page.AllAttachments)
        {
            if (someCondition) {
                attachmentGUID =  attachment.AttachmentGUID.ToString();
                break;
            }
        }
    }  

  test.Text = attachmentGUID ;
}
</script>
<asp:Literal runat="server" ID="test" Text="test" />
1 votesVote for this answer Mark as a Correct answer

Danuta Welz answered on April 3, 2019 21:21

At the end I used

var pages = CMS.DocumentEngine.DocumentHelper.GetDocuments("CMS.BlogPost") .OnCurrentSite() .Path("/Blogs/Blog/%") .WhereEquals("NodeAliasPath", pageAlias) .Published();

if (pages != null) { foreach (var blogpage in pages) { Guid guid = blogpage.GetGuidValue("BlogPostTeaser", Guid.Empty); img = CMS.DocumentEngine.AttachmentURLProvider.GetAttachmentUrl(guid, ""); }

}

Thanks

0 votesVote for this answer Mark as a Correct answer

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