Access custom fields

Using the custom fields API with C# code for pages is similar when using it with other content items. You can call the same methods on the PageNode object - GetValue and SetValue and GetString and SetString. These are located in namespace Telerik.Sitefinity.Model. There is also a simplified API for accessing For developers: Related data API and media content items located in the Telerik.Sitefinity.RelatedData namespace.

  • To get a custom field, use the following code sample:
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.RelatedData;
    namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Pages.CustomFields
    {
    public partial class PagesCustomFieldsSnippets
    {
    public Telerik.Sitefinity.Libraries.Model.Image GetCustomFieldsNativeAPI(string pageName, string fieldName)
    {
    var fluentPages = App.WorkWith().Pages();
    var homePage = fluentPages.Where(x => x.Title.Equals(pageName)).Get().FirstOrDefault();
    if (homePage != null)
    {
    //getting related data
    var relatedImage = homePage.GetRelatedItems<Telerik.Sitefinity.Libraries.Model.Image>(fieldName).FirstOrDefault();
    return relatedImage;
    }
    return null;
    }
    }
    }
  • To set a custom field, use the following sample:
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Telerik.Sitefinity.Model;
    using Telerik.Sitefinity.RelatedData;
    namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Pages.CustomFields
    {
    public partial class PagesCustomFieldsSnippets
    {
    public void SetCustomFieldsNativeAPI(string pageName, Guid tagId)
    {
    var fluentPages = App.WorkWith().Pages();
    var page = fluentPages.Where(x => x.Title.Equals(pageName)).Get().FirstOrDefault();
    if (page != null)
    {
    //Setting custom field with name Hint of type LString
    page.SetString("Hint", "Welcome to the home page");
    //Setting basic types
    page.SetValue("VisitsCount", 20);
    //First option to set taxonomies.
    //Important note -> this does not update the taxonomy statistics
    var taxaList = page.GetValue("Tags") as IList<Guid>; // maintain a reference to the list
    taxaList.Clear();
    taxaList.Add(tagId);
    //Second option
    page.Organizer.Clear("Tags");
    page.Organizer.AddTaxa("Tags", new Guid[] { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() });
    //Setting related data
    var fluentImages = App.WorkWith().Images();
    var image = fluentImages.Where(x => x.Title.Equals("WelcomePage")).Get().FirstOrDefault();
    if (image != null)
    {
    page.CreateRelation(image, "HelpImage");
    }
    fluentPages.SaveChanges();
    }
    }
    }
    }

Multilingual considerations

If you want to set the culture to a language other than the one of the current context (CurrentUICulture), you must use the GetString and SetString methods and pass the culture as an additional parameter.

Performance considerations

Custom fields of type Classification are loaded on demand – they are retrieved from the database only when requested, except if they are not already cached by Open Access. You should consider this when trying to access the taxonomy property of pages. The same is valid for RelatedData and RelatedMedia fields.

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?