Reorder forums

To reorder forums, you must perform the following:

  1. Change the ordinal of the specified forum.
    Query the specified forum and modify the value of its Ordinal property. For more information, see For developers: Modify forums.
  2. Update the ordinals of the other forums.
    Update the values of the Ordinal properties of the other sibling forums according to the new ordinal of the specified group.

Here is a code example:

C#
using System;
using System.Linq;
using Telerik.Sitefinity.Forums;

namespace SitefinityWebApp
{
    public class ReorderForums_MoveForum
    {
        public static void MoveForum(Guid forumId, int newOrdinal)
        {
            ForumsManager forumsManager = ForumsManager.GetManager();

            var forumToMove = forumsManager.GetForum(forumId);
            var forums = forumsManager.GetForums().Where(f => f.Group != null && f.Group.Id == forumToMove.Group.Id);

            //Validate new ordinal
            if (newOrdinal < 0 || newOrdinal == forumToMove.Ordinal)
            {
                return;
            }
            else if (newOrdinal > forums.Count())
            {
                newOrdinal = forums.Count();
            }

            //Preserve the old ordinal value
            var oldOrdinal = forumToMove.Ordinal;

            //Assign the new ordinal value
            forumToMove.Ordinal = newOrdinal;

            //Reorder the other forums.
            foreach (var forum in forums)
            {
                if (forum.Equals(forumToMove))
                {
                    continue;
                }

                //Moving up
                if (newOrdinal < oldOrdinal)
                {
                    //Update only the items between the new and the old position
                    if (forum.Ordinal >= newOrdinal && forum.Ordinal < oldOrdinal)
                    {
                        forum.Ordinal++;
                    }
                }
                //Moving down
                else
                {
                    //Update only the items between the new and the old position
                    if (forum.Ordinal <= newOrdinal && forum.Ordinal > oldOrdinal)
                    {
                        forum.Ordinal--;
                    }
                }
            }

            forumsManager.SaveChanges();
        }
    }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?