Extend linq to sharepoint to publish HTML fields

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) { // 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 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?
+4
source share
1 answer
  • You enter the value of the item[CONTENT_FIELDtesthtml] field in the HtmlField type. But HtmlField represents a field type, not a field value type. Thus, the HtmlField html will be set to null . Check the MSDN page for links to all types and types of publication fields.
    I'm not sure what the value type of the HtmlField field HtmlField . Probably just a string .
    Therefore, you must be safe to convert it to a string:

     string html = Convert.ToString(item[CONTENT_FIELDtesthtml]); 
  • I think that keeping the value in the property is the way to go. This way you will achieve a separation of the data layer and the presentation layer.

0
source

Source: https://habr.com/ru/post/1389983/


All Articles