+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Create Custom Field for Pages Programmatically

Create Custom Field for Pages Programmatically

2 posts, 1 answered
  1. Jeff
    Jeff avatar
    3 posts
    Registered:
    19 Jun 2011
    11 Jun 2014
    Link to this post

    Is there a way to create a custom field for pages programmatically? 

    Answered
  2. Jeff
    Jeff avatar
    3 posts
    Registered:
    19 Jun 2011
    12 Jun 2014 in reply to Jeff
    Link to this post

    Found a previous post that explained how to do this.

     //example 1 
                var metaManager = MetadataManager.GetManager();
                var field = metaManager.CreateMetafield("Lstring1");
                field.MetaAttributes.Add(new MetaFieldAttribute()
                {
                    Name = "UserFriendlyDataType",
                    Value = "ShortText"
                });
                field.ClrType = typeof(Lstring).FullName;
                var metaType = metaManager.GetMetaType(typeof(PageNode));
                metaType.Fields.Add(field);
                metaManager.SaveChanges();

                ////example 2
                App.WorkWith()
                    .DynamicData()
                    .Type(typeof(PageNode))
                    .Field()
                    .TryCreateNew("Lstring2", typeof(Lstring))
                    .Do((metaField) => 
                    {
                        metaField.MetaAttributes.Add(new MetaFieldAttribute()
                        {
                            Name = "UserFriendlyDataType",
                            Value = "ShortText"
                        });
                    })
                    .SaveChanges();

2 posts, 1 answered