we are working on a Sitefinity project and trying to figure out how to fetch the user's email that will be displayed inside a div. The website we are working on is old and has quite a few issues, but this seems to be a straightforward request.
This is the code we implemented:
.Master page (client side): includes link to JS and div that will hold the output.
<head runat="server">
<title></title>
<!-- Include your other head content -->
<script src="~/Scripts/userEmailScript.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<!-- Your HTML content -->
<div class="col-md-12">
<div id="userEmailDiv"></div>
</div>
</form>
JS file
document.addEventListener("DOMContentLoaded", function() {
var userEmail = "<%= UserEmail %>";
var userEmailElement = document.getElementById("userEmailDiv");
if (userEmailElement) {
userEmailElement.innerText = userEmail;
}
});
.cs page (server side)
using System;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;
namespace YourNamespace
{
public partial class YourPage : System.Web.UI.Page
{
protected string UserEmail { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Your server-side C# code here
// For example, fetching user email
UserManager userManager = UserManager.GetManager();
SitefinityProfile profile = userManager.GetUserProfile(); // Assuming the user is logged in
UserEmail = profile.Email;
}
}
}
}
Once we refresh the page, we get this value: <%= UserEmail %> instead of the actual email address. My guess is that the problem is in the server side code. I researched the documentation but could not find the solution or a fix. Any insights? Thank you.
<%= UserEmail %>called in js and not directly inside thedivin question?