SpaceJump
SpaceJump

Reputation: 483

Sitecore - Get the field names of an item

I want to get a list of the field names of an item, but can't get it to work.

This is my attempt so far:

if (pathItemInLanguage.Versions.GetVersions().Length == 0)
                        {
                            Item newItemLangVersion = pathItemInLanguage.Versions.AddVersion();

                            //Populate the new language version with values:
                            newItemLangVersion.Editing.BeginEdit();

                            newItemLangVersion.Fields.ReadAll();
                            foreach (Field field in newItemLangVersion.Fields)
                            {
                                string dn = "nix";
                                dn = field.DisplayName;
                                string n = "nix";
                                n = field.Name;
                                string k = "nix";
                                k = field.Key;
                                string xy = "nix";
                                xy = field.ToString();

                                if ((FieldTypeManager.GetField(Sitecore.Context.Item.Fields[field.DisplayName]) is HtmlField) ||
                                    (FieldTypeManager.GetField(Sitecore.Context.Item.Fields[field.DisplayName]) is TextField))
                                {
                                    newItemLangVersion.Fields[field.Name].Value = "new";
                                }
                            }
                            newItemLangVersion.Editing.EndEdit();

None of those strings I read from field give actual field names of the item. The application runs as module from within the Sitecore Desktop.

What am I missing?

Upvotes: 0

Views: 2651

Answers (2)

SpaceJump
SpaceJump

Reputation: 483

Got it to work using this:

if ((FieldTypeManager.GetField(field) is HtmlField) || (FieldTypeManager.GetField(field) is TextField)) {
...
}

Upvotes: 1

Vlad Iobagiu
Vlad Iobagiu

Reputation: 4118

I updated my answer. You need to do in next way: check if the field is a standard template field and check type of the field :

Please do it something like:

   if (pathItemInLanguage.Versions.GetVersions().Length == 0)
     {
        Item newItemLangVersion =  pathItemInLanguage.Versions.AddVersion();
        //Populate the new language version with values:
         //Populate the new language version with values:
        newItemLangVersion.Editing.BeginEdit();

        newItemLangVersion.Fields.ReadAll();

        foreach (Field field in newItemLangVersion.Fields)
        {
            if (!IsStandardTemplateField(field) && (field is HtmlField || field is TextField))
            newItemLangVersion.Fields[field.Name].Value = "new";
        }
        newItemLangVersion.Editing.EndEdit();
      }

and you have a method to check if is a template field because you don't need to modify that kind of fields

  public bool IsStandardTemplateField(
       Field field)
    {
        Sitecore.Data.Templates.Template template = Sitecore.Data.Managers.TemplateManager.GetTemplate(
          Sitecore.Configuration.Settings.DefaultBaseTemplate,
          field.Database);
        Sitecore.Diagnostics.Assert.IsNotNull(template, "template");
        return template.ContainsField(field.ID);
    }

Upvotes: 1

Related Questions