Message Board : Custom fields

Shweta Kode asked on September 16, 2015 20:38

I want to show user's details (school name,etc)in the comments written by the user in message board so I want to add custom fields to the message board. I cloned the message board module which generated MessageBoard_Custom.ascx file.

How do I add new fields to Board_Message table ? Do I edit the database ? I didn't see any other way through admin interface.

I went ahead and added new columns in Board_Message table so that I can edit the code in MessageEdit.ascx to add some code but it has an object BoardMessageInfo to add new message. Example : messageInfo.MessageUserName = txtUserName.Text.Trim();

How do I add my code messageInfo.SchoolName since it doesn't recognize SchoolName property ?

Please help.

Correct Answer

Joshua Adams answered on September 17, 2015 19:29

Here is an example of the code needed: Note: this is just a quick example, you may have to modify it or change parts depending on your needs.

Step 1) Register your macro name space

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for BPMacros
/// </summary>
using CMS.Base;
using CMS.MacroEngine;

[Extension(typeof(CustomMacroMethods))]
public class BPMacros : MacroNamespace<BPMacros>
{

}

[MacroNamespaceLoader]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the registration of custom macro namespaces.
    /// </summary>
    private class MacroNamespaceLoaderAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// Called automatically when the application starts.
        /// </summary>
        public override void Init()
        {
            // Registers "CustomNamespace" into the macro engine
            MacroContext.GlobalResolver.SetNamedSourceData("BPMacros", BPMacros.Instance);
        }
    }
}
Step 2)Create Macro Method
using CMS;
using CMS.CustomTables;
using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS.EmailEngine;
using CMS.Helpers;
using CMS.MacroEngine;
using CMS.Membership;
using CMS.OnlineForms;
using CMS.SiteProvider;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

[assembly: RegisterExtension(typeof(CustomMacroMethods), typeof(BPMacros))]
/// <summary>
/// Summary description for CustomMacroMethods
/// </summary>
public class CustomMacroMethods : MacroMethodContainer
{

    /// <summary>
    /// Returns a field for a user depending on the string fieldname passed in
    /// </summary>
    /// <param name="context"></param>
    /// <param name="parameters"></param>
    /// <returns></returns>
    [MacroMethod(typeof(string), "Returns particular field for specific user", 1)]
    [MacroMethodParam(0, "param1", typeof(int), "UserID")]
    [MacroMethodParam(1, "param2", typeof(string), "FieldName")]
    public static object BPUser(EvaluationContext context, params object[] parameters)
    {
        int UserID = ValidationHelper.GetInteger(parameters[0], 0);
        string returnVal = string.Empty;
        UserInfo ui = UserInfoProvider.GetUserInfo(UserID);
        if (ui != null)
        {
            try
            {
                returnVal = ui.GetValue(ValidationHelper.GetString(parameters[1], "")).ToString();
            }
            catch (Exception ex)
            {
                //macro error
            }
        }
        return returnVal;
    }
}

Step 3)Call Macro {% BPMacros.BPUser(53, "FullName")%}

0 votesVote for this answer Unmark Correct answer

Recent Answers


Dawid Jachnik answered on September 16, 2015 22:42 (last edited on September 16, 2015 22:43)

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on September 16, 2015 22:43

Hello Shweta,

If you added a new columns to board.message class (db table Board_Message) you can access to them by

to save data

messageInfo.SetValue("SchoolName", "");

to retrive data:

messageInfo.GetValue("SchoolName");

or

messageInfo.GetStringValue("SchoolName", "");

This should work find for you, but if that's not enough you can extend board.message - more information about extending (customizing) the system class you can read more in documentation https://docs.kentico.com/pages/viewpage.action?pageId=58335892

2 votesVote for this answer Mark as a Correct answer

Shweta Kode answered on September 16, 2015 22:48

Hi Dawid,

That's exactly how I thought it should work ( that works for custom tables that I created) but didn't work in this case using messageInfo.SetValue("SchoolName", "")

I will look at the link you gave me. Thank you!

Shweta

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on September 16, 2015 22:58

Did you add extend the class by admin interface ?

2 votesVote for this answer Mark as a Correct answer

Joshua Adams answered on September 17, 2015 16:51

An alternate solution, since the administration interface may not allow you to create new fields on that module/class could be to make a custom transformation method that pulls in whatever data from the user that you would like. Then that way the data is still only stored on the user, and if they update their information, it will change in the message board as well. You dont have to store that information anywhere else because you already have access to it. All you need is to create a macro method or transformation method that takes the userid(int) as a parameter and then maybe the field name(string) as another parameter, that way you can grab whatever information you need and reuse the same method anywhere you need user information.

This should help you out with creating the custom method: https://docs.kentico.com/display/K8/Registering+custom+macro+methods

0 votesVote for this answer Mark as a Correct answer

Shweta Kode answered on September 17, 2015 18:36

Hi Joshua,

Okay, Yes. I think that will work for me. I will take a look at the link to see how to create custom method. Thank you.

0 votesVote for this answer Mark as a Correct answer

Shweta Kode answered on September 21, 2015 20:07 (last edited on September 21, 2015 23:58)

Hi Joshua,

Thank you.

0 votesVote for this answer Mark as a Correct answer

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