扩展 linq to sharepoint 以发布 HTML 字段

发布于 2024-12-25 21:24:31 字数 2120 浏览 1 评论 0原文

我创建了一个分部类来扩展默认的 spmetal 类来处理发布 html 字段。如此处所述:

扩展对象关系映射

来自 的代码段公共部分类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
}

来自“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)));
            }
        }
    }

两个问题:

  1. 为什么HtmlField html = item[CONTENT_FIELDtesthtml] as HtmlField; 返回 null,但 item.Fields.GetFieldByInternalName 工作正常吗?
  2. 有没有办法从内部使用 GetFieldValueAsText 方法 Web 部件或者是将值存储在自定义中的方法 以后访问的属性可以接受吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2025-01-01 21:24:31
  1. 您正在将 item[CONTENT_FIELDtesthtml] 的字段值转换为 HtmlField 类型。但是 HtmlField 表示字段的类型,而不是字段值的类型。因此,HtmlField html 将被赋值为 null。检查此 MSDN 页面以获取所有发布字段类型的参考和值类型。
    我不确定 HtmlField 的字段值类型是什么。可能只是字符串
    所以你应该可以安全地将其转换为字符串:

    string html = Convert.ToString(item[CONTENT_FIELDtesthtml]);
    
  2. 我认为将值存储在属性中是正确的方法。通过这种方式,您可以实现数据层和表示层的分离。

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文