1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Display username as news author

Display username as news author

4 posts, 0 answered
  1. Szymon
    Szymon avatar
    22 posts
    Registered:
    04 Jan 2008
    14 Jul 2008
    Link to this post
    Hi,
    I would like to display the name of the user that created the news as its author. Currently the template uses the Author metafield. How I can change it to use the Creator name instead? Or how to change the system to initialize the Author with logged in user name when creating new news item?

    Regards,
    Szymon
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    16 Jul 2008
    Link to this post
    Hi Szymon,

    You can set the Author metafield dynamically with the Sitefinity's APIs.

    This could be done with a few lines to global.asax file, located in your project's bin directory:

    void Application_Start(object sender, EventArgs e)  
       //Event handler catching the Content Manager executing
       Telerik.Cms.Engine.ContentManager.Executing += new EventHandler<Telerik.ExecutingEventArgs>(ContentManager_Executing); 
     
    void ContentManager_Executing(object sender, Telerik.ExecutingEventArgs e) 
        //Check whether the user is creating content 
        if (e.CommandName == "CreateContent"
        { 
            //take the IContent Item from the command arguments 
            Telerik.Cms.Engine.IContent cnt = e.CommandArguments as Telerik.Cms.Engine.IContent; 
            //chech if there is such content and if it belongs to the news provider 
            if (cnt != null && cnt.ProviderName == "News"
            { 
                //get the current user's First and Last names 
                ProfileBase profile = ProfileBase.Create(HttpContext.Current.User.Identity.Name); 
                string FirstName = profile.GetPropertyValue("FirstName").ToString(); 
                string LastName = profile.GetPropertyValue("LastName").ToString(); 
                //Save these to the Author Metafield. 
                cnt.SetMetaData("Author", FirstName+" "+ LastName); 
            } 
        } 

    I have included some comments.

    In addition, you may also check whether the user has first or last name, or if there's already something written in the Author's field.

    I hope this helps.

    All the best,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Szymon
    Szymon avatar
    22 posts
    Registered:
    04 Jan 2008
    16 Jul 2008
    Link to this post
    Hi Georgi,

    Thanks for your response - this solves my problem!

    I also noticed that there is also possible to set the default value for metafields in web.config. For example this is for the Publication_date

    <add key="News.Publication_Date" valueType="DateTime" visible="True" searchable="True" sortable="True" defaultValue="#Now" />

    Difference is that these valus are applied already when user enters the create news page, where with your solution it is applied on save. Would be great if you could add a shorthand for the user name that could be used here as well. Or maybe some simple expression minilanguage to combine several fields like you did with the FirstName and LastName from user profile.

    Thanks!
    -Szymon

  4. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    17 Jul 2008
    Link to this post
    Hi Szymon,
    /* tfs 14543 */
    We are adding this suggestion to our Feature Requests list right away. The suggestions are always welcome, and we will definitely discuss yours.

    Thank you!

    Regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Register for webinar
4 posts, 0 answered