I've created a partial class to extend the default spmetal class to handle publishing html fields. As outlined here:
Extending the 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
}
Snippet 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)
{
// render link
Controls.Add(new LiteralControl(string.Format("<p>Link: {0}</p>", link.ContentLink)));
// render html
Controls.Add(new LiteralControl(string.Format("<p>HTML: {0}</p>", link.TestHtml)));
}
}
}
Two questions:
- Why does
HtmlField html = item[CONTENT_FIELDtesthtml] as HtmlField;
returnnull
, but theitem.Fields.GetFieldByInternalName
works correctly? - Is there a way to use the
GetFieldValueAsText
method from within the webpart or is the approach of storing the value in a custom property for accessing later acceptable?
You are casting the field value of
item[CONTENT_FIELDtesthtml]
to the typeHtmlField
. ButHtmlField
represents the type of the field and not the type of the field value. ThusHtmlField html
will be assigned withnull
. Check this MSDN page for a reference of all publishing field types and value types.I am not sure what the field value type of a
HtmlField
is. Probably juststring
.So you should be safe to convert it to string:
I think storing the value in a property is the way to go. This way you achieve a separation of data layer and presentation layer.