简体   繁体   中英

Extending linq to sharepoint for Publishing HTML fields

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:

  1. Why does HtmlField html = item[CONTENT_FIELDtesthtml] as HtmlField; return null , but the item.Fields.GetFieldByInternalName works correctly?
  2. 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?
  1. You are casting the field value of item[CONTENT_FIELDtesthtml] to the type HtmlField . But HtmlField represents the type of the field and not the type of the field value. Thus HtmlField html will be assigned with null . 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 just string .
    So you should be safe to convert it to string:

     string html = Convert.ToString(item[CONTENT_FIELDtesthtml]); 
  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM