SetField() public method

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.
return void
        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);
        }
        public void SetField_RichText_ThrowsException()
        {
            //Assign
            var expected = "<p>Test with <a href=\"~/link.aspx?_id=BFD7975DF42F41E19DDA9A38E971555F&amp;_z=z\">link</a></p>";

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                    new DbField(FieldName)
                    {
                        Type = "Rich Text"
                    }
                }
            })
            {
                using (new FakeSite())
                {

                    var item =
                        database.GetItem("/sitecore/content/TestItem");
                    var field = item.Fields[FieldName];

                    var mapper = new SitecoreFieldStringMapper();
                    var config = new SitecoreFieldConfiguration();
                    config.PropertyInfo = typeof(StubClass).GetProperty("String");


                    Sitecore.Context.Site = Sitecore.Configuration.Factory.GetSite("website");

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

                    //Act
                    using (new ItemEditing(item, true))
                    {
                        //Rich text not raw throws exception
                        Assert.Throws<NotSupportedException>(() => mapper.SetField(field, expected, config, null));
                    }

                    Sitecore.Context.Site = null;

                    //Assert
                    Assert.AreEqual(string.Empty, field.Value);
                }
            }
        }
        public void SetField_ForceRenderFieldPipeline_ThrowsException()
        {
            //Assign

            var fieldValue = "<p>hello world</p>";
            var expected = "&lt;p&gt;hello world&lt;/p&gt;";

            var item = Database.GetItem("/sitecore/content/Tests/DataMappers/SitecoreFieldStringMapper/GetField");
            var field = item.Fields[FieldName];

            var mapper = new SitecoreFieldStringMapper();
            var config = new SitecoreFieldConfiguration();
            config.Setting = SitecoreFieldSettings.ForceRenderField;
            config.PropertyInfo = new FakePropertyInfo(typeof(string), "String", typeof(StubClass));
            using (new ItemEditing(item, true))
            {
                field.Value = fieldValue;
            }



            //Act
            mapper.SetField(field, fieldValue, config, null);

            //Assert

        }
        public void SetField_ForceRenderFieldPipeline_ThrowsException()
        {
            //Assign

            var fieldValue = "<p>hello world</p>";
            var expected = "&lt;p&gt;hello world&lt;/p&gt;";

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                    new DbField(FieldName)
                    {
                        Value = fieldValue
                    }
                }
            })
            {
                var item = database.GetItem("/sitecore/content/TestItem");
                var field = item.Fields[FieldName];

                var mapper = new SitecoreFieldStringMapper();
                var config = new SitecoreFieldConfiguration();
                config.Setting = SitecoreFieldSettings.ForceRenderField;
                config.PropertyInfo = new FakePropertyInfo(typeof(string), "String", typeof(StubClass));
                using (new ItemEditing(item, true))
                {
                    field.Value = fieldValue;
                }



                //Act
                Assert.Throws<NotSupportedException>(()=> mapper.SetField(field, fieldValue, config, null));

                //Assert
            }

        }