Glass.Mapper.Sc.DataMappers.SitecoreFieldStringMapper.SetField C# (CSharp) 메소드

SetField() 공개 메소드

Sets the field.
It is not possible to save data from a rich text field when the data isn't raw. /// + Set the SitecoreFieldAttribute setting property to SitecoreFieldSettings.RichTextRaw for property {0} on type {1}.Formatted(config.PropertyInfo.Name, config.PropertyInfo.ReflectedType.FullName)
public SetField ( Sitecore field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context ) : void
field Sitecore The field.
value object The value.
config Glass.Mapper.Sc.Configuration.SitecoreFieldConfiguration The config.
context SitecoreDataMappingContext The context.
리턴 void
        public override void SetField(Sitecore.Data.Fields.Field field, object value, SitecoreFieldConfiguration config, SitecoreDataMappingContext context)
        {
            if (field == null)
            {
                return;
            }

            if(config.Setting == SitecoreFieldSettings.ForceRenderField)
            {
                throw new NotSupportedException("It is not possible to save data from a field when the data isn't raw."
                     + "In the SitecoreFieldAttribute remove the SitecoreFieldSettings.ForceRenderField valuea for property {0} on type {1}".Formatted(config.PropertyInfo.Name, config.PropertyInfo.ReflectedType.FullName));

            }

            if (field.Type.StartsWith("Rich Text") && config.Setting != SitecoreFieldSettings.RichTextRaw )
            {
                throw new NotSupportedException("It is not possible to save data from a rich text field when the data isn't raw."
                    + "Set the SitecoreFieldAttribute setting property to SitecoreFieldSettings.RichTextRaw for property {0} on type {1}".Formatted(config.PropertyInfo.Name, config.PropertyInfo.ReflectedType.FullName));
            }

            field.Value = value != null ? value.ToString() : null;
        }

Usage Example

        public void SetField_FielNonRichText_ValueWrittenToField()
        {
            //Assign
            var expected = "<p>Test with <a href=\"~/link.aspx?_id=BFD7975DF42F41E19DDA9A38E971555F&amp;_z=z\">link</a></p>";
            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldStringMapper/SetField");
            var field = item.Fields[FieldName];

            var mapper = new SitecoreFieldStringMapper();
            var config = new SitecoreFieldConfiguration();
            config.Setting = SitecoreFieldSettings.RichTextRaw;
            
            Sitecore.Context.Site = Sitecore.Configuration.Factory.GetSite("website");

            using (new ItemEditing(item, true))
            {
                field.Value = string.Empty;
            }

            //Act
            using (new ItemEditing(item, true))
            {
                mapper.SetField(field, expected, config, null);
            }

            Sitecore.Context.Site = null;

            //Assert
            Assert.AreEqual(expected, field.Value);
        }
All Usage Examples Of Glass.Mapper.Sc.DataMappers.SitecoreFieldStringMapper::SetField