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:
  • C#
      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:
  • C#
      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?
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?