Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:52:50+00:00 2026-05-28T01:52:50+00:00

I’ve created a partial class to extend the default spmetal class to handle publishing

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:52:51+00:00Added an answer on May 28, 2026 at 1:52 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.