More in this section
Categories
Bloggers
Blogs RSS feed

Displaying Additional User Information in a Sitefinity Forum Post

by Svetoslav Manchev

In this blog post I will show you how you could display additional user information in a Sitefinity forum post. This user information should be available as a custom field in the users’ profile.


Prerequisites:

Created custom field in the
users’ profile (for example: JobPosition)

Code explanation:

1.  We need to create a class for our custom field and inherit from Literal:

public class CustomField : Literal
{
    // we will implement our logic here
}

2.  Define the necessary variables and properties for post owner and custom field value.

           private string ownerId = null;
        private string myField = null;
 
        public string OwnerId
        {
            get{ return ownerId;}
            set{ ownerId = value;}
        }
 
        public string MyField
        {
            get{ return myField;}
            set{ myField = value;}
        }

3.  E
xtend the CreateChildControl method of Literal to get the post owner and the field value by overriding the standard ones:

protected override void CreateChildControls()
  {
      // get the current identity
      var identity = ClaimsManager.GetCurrentIdentity();
 
      // get information about the user from the properties of the ClaimsIdentityProxy object
      var userId = identity.UserId;
 
    // Get user and profile Manager
      UserProfileManager profileManager = UserProfileManager.GetManager();
      UserManager userManager = UserManager.GetManager();
 
    // Get current user
      User user = userManager.GetUser(userId);
 
    // Get the current user’s profile
      SitefinityProfile profile = null;
 
      if (user != null)
      {
          profile = profileManager.GetUserProfile<SitefinityProfile>(user);
      }
 
    // Get the value we are interested to
      var mySearchedField = profile.GetValue(myField).ToString();
 
    // This text will be displayed when we call the property.
      Text = mySearchedField;
 
    // Call the base method
      base.CreateChildControls();
  }

4. A
dd the create .cs file in the root project directory (in case of a custom folder, you need to update the respective references)

5. Build the project

6.  
Create a copy of your post widget

7. 
Register the new control by adding this code at the Register section of the post widget (or create a new post widget based on the built-in one, in order to have both versions):

<%@ Register TagPrefix=" myTagPrefix " Namespace="SitefinityWebApp" Assembly="SitefinityWebApp" %>

8. 
Add the following markup where you need your custom field:

<myTagPrefix:CustomField Text='<%# Eval("Content") %>' OwnerId='<%# Eval("Owner")%>' MyField='JobPosition' runat="server" />


This example works for a different number of fields in the profile, just replace the 'JobPosition' with the desired one.

You could download the complete code example of CustomField.

4 comments

Leave a comment
  1. kashif jawed Nov 02, 2016

    Hi Svetoslav Manchev,

    i have a requirement to show the blog post author information at blog post detail page like your information showing above the social sharing widget.

    I googled, and able to create a new widget template but not able to add the author information on detail page.

    Can you please guide me how i can add the author info at blog post detail page widget.


  2. jk Nov 02, 2017
    I sent your articles links to all my contacts and they all love it including me. physical rehabilitation center
  3. Statham Brown Nov 27, 2017
    So, its good to see that you are providing such informative things to students so that they can learn something from it. You should also guide them about Best Literature Review Writing Service so that students don't face any issue in getting help from professionals.
  4. Uzi Dec 08, 2017
    Thank you very much for sharing this very useful information, I am very happy to have found this information that I have been looking for
    Pengobatan Alami Untuk Nyeri Dada
    Cara Mengatasi Penyakit Kulit
    Cara Menghilangkan Benjolan
    Obat Alami Menyembuhkan Kutu air
    Cara Mengobati Nyeri Ulu Hati
    Cara Mengobati Kerumut
    Cara Mengobati Uci Uci

    Leave a comment