2

When reading an item's fields like this:

item.Fields.ReadAll();

It reads all the fields. Is there a way to exclude the default Sitecore fields and get only the fields that a user has created.

For example, I have created a template with 2 fields - Title & Description.

for(Field field in item.Fields)
{

}

The above loop will run through all the fields of the item, but I want to get only the non-default fields.

This is for updating field values programmatically and the field IDs are not always known.

And there is a chance of updating the values of the Sitecore fields.

Is that possible.

1
  • What problem does this cause by reading all fields? Mar 28 '20 at 19:23
4

I'll say this

  1. Users don't create fields, developers create fields
  2. Issuing a Field.ReadAll is absolutely terrible for performance. Make sure you only do this in a script, never ever in runtime code.

That said, what you probably want is this.

        var i = Sitecore.Context.Item;
        var ownFieldCollection = i.Template.OwnFields;

        foreach (var field in ownFieldCollection)
        {
            string value = i.Fields[field.Key].Value;
            // or do whatever here
        }

This will get you a the fields defined directly on the template of the item you are processing.

Alternatively if you really do want fields, inherited fields, and whatnot - this is how to get to them.

        var i = Sitecore.Context.Item;
        var templateFieldsCollection = i.Template.Fields;

        foreach (var field in templateFieldsCollection)
        {
            if (!field.InnerItem.Paths.FullPath.StartsWith("/sitecore/templates/system", StringComparison.OrdinalIgnoreCase))
            {
                string value = i.Fields[field.Key].Value;
                // or do whatever here
            }
        }

Either way, the cache defying .ReadAll fields call is avoided.

No solution should need code like this however.

2

I don't think you can skip reading all the fields that are part of the Standard Template, but you could filter them out.

Example: The following will get all fields on the Standard Template and remove from the list of fields on the Sample Item (and its base templates).

# Create a list of field names on the Standard Template. This will help us filter out extraneous fields.
$standardTemplate = Get-Item -Path "master:" -ID ([Sitecore.TemplateIDs]::StandardTemplate.ToString())
$standardTemplateTemplateItem = [Sitecore.Data.Items.TemplateItem]$standardTemplate
[string[]]$standardFields = $standardTemplateTemplateItem.OwnFields + $standardTemplateTemplateItem.Fields | Select-Object -ExpandProperty key -Unique

$sampleItemTemplate = Get-Item -Path "master:" -ID "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}"
$sampleItemTemplateTemplateItem = [Sitecore.Data.Items.TemplateItem]$sampleItemTemplate
[string[]]$sampleItemFields = $sampleItemTemplateTemplateItem.OwnFields + $sampleItemTemplateTemplateItem.Fields | Select-Object -ExpandProperty key -Unique

[Linq.Enumerable]::Except($sampleItemFields, $standardFields)

title
text
3
  • I'm doing it in C#. Yes, filtering them works for me. Read all the fields of Standard Template and filter them. Is that correct. Could you pls provide a littl emore explanation. Thank you.
    – Qwerty
    Mar 28 '20 at 19:36
  • Are you reading the fields of /sitecore/templates/System/Templates/Standard template or /sitecore/templates/System/Templates/Template
    – Qwerty
    Mar 28 '20 at 19:46
  • The ID used is for Standard Template and Sample Item Mar 28 '20 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.