Access custom fields through the SiteMapNodes

You can access the custom fields using the PageSiteNode object. This can be useful when working with a custom widget that is placed on a page.

Use the following code sample:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Web;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Pages.CustomFields
{
    public partial class PagesCustomFieldsSnippets
    {
        public void GetCustomFieldForPageThroughSiteMapNode()
        {
            //use SiteMapBase.GetCurrentNode() to get the current node
            var provider = SiteMapBase.GetCurrentProvider() as SiteMapBase;
            var node = provider.FindSiteMapNodeFromKey("283D7E4A-61BD-62C5-8058-FF00004169B7") as PageSiteNode;

            //get visits count
            var visitsCount = (decimal)node.GetCustomFieldValue("VisitsCount");

            //get taxonomies
            var tagIds = (IList<Guid>)node.GetCustomFieldValue("Tags");

            //get related image
            var image = ((IEnumerable<IDataItem>)node.GetCustomFieldValue("HelpImage")).FirstOrDefault();
        }
    }
}
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?