I created a partial class to extend the default spmetal class to handle published html fields. As indicated here:
Extending Object Relational Mapping
Snippet from public partial class RelatedLinksItem : Item, ICustomMapping :
/// <summary> /// Read only data is retrieved in this method for each extended SPMetal field /// Used to Read - CRUD operation performed by SPMetal /// </summary> /// <param name="listItem"></param> [CustomMapping(Columns = new string[] { CONTENT_FIELDtesthtml, CONTENT_FIELDLink })] public void MapFrom(object listItem) { SPListItem item = (SPListItem)listItem; // link this.ContentLink = item[CONTENT_FIELDLink] as LinkFieldValue; // html (does NOT work) HtmlField html = item[CONTENT_FIELDtesthtml] as HtmlField; // this returns null // html (does work) HtmlField html2 = (HtmlField)item.Fields.GetFieldByInternalName(CONTENT_FIELDtesthtml); // this returns object this.Contenttesthtml = html2; this.TestHtml = html2.GetFieldValueAsText(item[CONTENT_FIELDtesthtml]); // set property for rendering html }
Fragment from "webpart":
protected override void CreateChildControls() { using (OrganisationalPoliciesDataContext context = new OrganisationalPoliciesDataContext(SPContext.Current.Web.Url)) { var results = from links in context.RelatedLinks select links; foreach (var link in results) {
Two questions:
- Why
HtmlField html = item[CONTENT_FIELDtesthtml] as HtmlField; returns null , but item.Fields.GetFieldByInternalName working correctly? - Is there a way to use the
GetFieldValueAsText method from within the web part, or is it an approach to storing value in a custom property to access later features?
source share