How best to dynamically set the Page Title based on a DataSource?

Joe Krill asked on November 21, 2014 21:45

Is there a way to dynamically set the Page Title based on a DataSource? I have a DataSource that returns 0 or 1 row, and I want the page title to be set based on the data returned. So if that data has a DisplayName property, I want that to be set as the page title. Otherwise, I'd like to use something else (like "Not found" or something similar). Is this possible?

I found some posts pointing to various ways to do it from transformations using a server-side script tag, but this doesn't seem like the right way to handle this. Is there a macro method I could use to do something like Eval("") on the first record of a datasource?

Recent Answers


Virgil Carroll answered on November 23, 2014 16:24

IfEmpty(Eval("Field Name", "Not Found", Eval("Field Name"))

Put that in the body of the transformation. Best way to do this.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 24, 2014 14:38

If you're talking about the page title you'd see in the tab of the page or the browser's title, you can use a macro in the pages metadata tab although I don't believe you will get access specifically to that datasource's count values. You do have access to CurrentDocument.DocumentName and fields within the DataSource if you use CurrentDocument.GetValue("ColumnName"). Then you can check for null or empty string and replace it with Not Found.

Regarding the server-side script tag, that would be an OK way to handle it since you'd only have 0 or 1 record. If you had more than one record, I'd recommend not doing it.

0 votesVote for this answer Mark as a Correct answer

Joe Krill answered on November 24, 2014 16:05 (last edited on December 10, 2019 02:30)

Sorry, yes, I was referring to the page title as it appears in the browser (i.e. the <title> tag -- although what I really want to modify is only the DocumentPageTitle, because I want the system to still honor my "Page title format" setting I have set for the site in Settings -> Content.)

What I was hoping for was maybe a macro I can use in the "Page title" field of the Metadata tab for the document. For example, something along the lines of:

{% CurrentDocument["MyDataSource"].Items.Count == 1 ? CurrentDocument["MyDataSource"].Items[0].DisplayName : "Not found" |(identity)GlobalAdministrator%}

But that only works because the profile loaded into MembershipContext.CurrentUserProfile is hard-coded to look for a userid, username, or userguid supplied in the query string. It's very specific to showing user profiles.

So I certainly could write some custom code that works similarly to MembershipContext.CurrentUserProfile, but I was really hoping for a more generic solution that could be employed for any DataSource, because I anticipate this being something I'll need to do more in the future, and I didn't want to have to rely on writing custom code for each and every single situation like this.

Using the transformation to handle this just doesn't seem to be the correct solution. That seems totally out of the scope of what a transformation does or should do, and could lead to confusing behavior if someone working with it in the future doesn't just happen to know that it will change the page title (and they likely won't). If there's no out-of-the-box solution for this type of thing, I think I'll probably just create a custom web part that can change the page the title based on a DataSource -- that's much more explicit, and could still be used without writing custom code each time.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on December 8, 2014 09:45

Hi,

You can make your data source to add the value into the query string parameter of the URL. Then, in the page metadata tab you can use query string macro to read this value and it will be displayed as the page title. It is similar to the profile you mentioned. the user profile page has a wild card URL which is basically a query string parameter that is read and used to fill out the page title.

Best regards,
Juraj Ondrus

0 votesVote for this answer Mark as a Correct answer

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