Using a URL query parameter to add a Component Presentation to an ASP.NET Web page
An example of how to add a single Component Presentation to an ASP.NET Web page dynamically, using a query parameter string.
Source Page
This Page Template combines with a Page to produce a publishable ASP.NET Web page that contains a link to the target Web page, with a query parameter string specifying the Component to add, and the Component Template to render it with.
<%@Page language="C#"%>
<%@ Import Namespace="Tridion.ContentDelivery.Web.WAI"%>
<%@ Import Namespace="Tridion.ContentDelivery.Web.Linking"%>
<%
string pageURI = "tcm:1-26-64";
string componentURI = "tcm:1-22";
string templateURI = "1-19-32";
string text = "This is a Dynamic component link!";
DynamicComponentLink dcpLink = new DynamicComponentLink();
Link link = dcpLink.GetLink(pageURI, componentURI, templateURI, "attributes", text, true);
%>
<html>
<head>
<title>Dynamic Component Link Source Page</title>
</head>
<body>
Dynamic Component Link to Component Presentation
(componentId=<%=componentURI%>, componentTemplateId=<%=templateURI%>) on page <%=pageURI%>:
<%
Response.Write (link.ToString());
%>
</body>
</html>
In this Page Template, the dcpLink object validates the link to the Dynamic Component Presentation by making sure that both the Page that can assemble Dynamic Component Presentations and the Dynamic Component Presentation itself are available in the Content Data Store. When DynamicComponentLink resolves a Link, the following parameters are added to the query parameter string:
Component- The ID of the Component
ComponentTemplate- The ID of the Component Template
The resulting published Web page contains the following content:
<a href="/thepage.aspx?Component=18&ComponentTemplate=16">
This is a Dynamic component link!
</a>
Target Page
This Page Template combines with a Page to produce a publishable Web page that takes two query string parameters, one specifying the Component, the other specifying the Component Template. Both are combined into a Component Presentation and added to the Web page at request time.
<%@Page language="C#"%>
<%@ Import Namespace="Tridion.ContentDelivery.DynamicContent"%>
<%
ComponentPresentationAssembler componentPresentationAssembler = null;
componentPresentationAssembler = new ComponentPresentationAssembler("[%=Page.ID%]",Page);
int publicationId = 1;
string component = "tcm:" + publicationId + "-" + Request.QueryString["ComponentId"];
string template = "tcm:" + publicationId + "-" + Request.QueryString["ComponentTemplateId"] + "-32";
%>
<html>
<head>
<title>Dynamic Component Link Destination Page</title>
</head>
<body>
Assembled Component:
<%Response.Write(componentPresentationAssembler.GetContent(component, template));%>
</body>
</html>
<%
componentPresentationAssembler.Dispose();
%>
The resulting published Web page contains the following content (the content under "Assembled Component" depends on the Component and Component Template specified):
<html>
<head>
<title>Dynamic Component Link Destination Page</title>
</head>
<body>
Assembled Component:
<b>This is the content of the component presentation!</b>
</body>
</html>