I was trying to display user information inside a Scriban template (SXA variant).
I managed to write a context function that gives me the full name of the user (or any profile property I put in there) - but as I don't want to create a function for every property I might need. I'd rather be able to return the User (or UserProfile) object and choose the properties that I want to display inside my Scriban template. But that didn't work.
To create the extension, I used this information: https://ggullentops.blogspot.com/2019/11/custom-sxa-scriban-extensions.html
The code now is this:
public class GetSubscribers : IGenerateScribanContextProcessor
{
private delegate IEnumerable<string> Subscribers(Item currentItem);
public void Process(GenerateScribanContextPipelineArgs args)
{
var subscribers = new Subscribers(GetSubscriberList);
args.GlobalScriptObject.Import("sc_subscribers", subscribers);
}
public IEnumerable<string> GetSubscriberList(Item currentItem)
{
var users = currentItem.Fields["Subscribers"].Value.Split('|');
const string domainName = "extranet";
var userList = new List<User>();
foreach (var userName in users)
{
if (!string.IsNullOrEmpty(userName) && User.Exists(domainName + @"\" + userName))
{
userList.Add(User.FromName(domainName + @"\" + userName, false));
}
}
return userList.Select(u => u.Profile.FullName);
}
}
If I return objects instead of the strings, I get errors...
Is there a way to achieve what I'm trying here?