简体   繁体   English

将linq扩展到sharepoint以发布HTML字段

[英]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. 我创建了一个部分类来扩展默认的spmetal类来处理发布html字段。 As outlined here: 如下所述:

Extending the Object-Relational Mapping 扩展对象关系映射

Snippet from public partial class RelatedLinksItem : Item, ICustomMapping : public partial class RelatedLinksItem : Item, ICustomMapping片段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": 来自“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; 为什么HtmlField html = item[CONTENT_FIELDtesthtml] as HtmlField; return null , but the item.Fields.GetFieldByInternalName works correctly? 返回null ,但item.Fields.GetFieldByInternalName正常工作?
  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? 有没有办法在webpart中使用GetFieldValueAsText方法,或者是将值存储在自定义属性中以便以后访问的方法?
  1. You are casting the field value of item[CONTENT_FIELDtesthtml] to the type HtmlField . 您正在将item[CONTENT_FIELDtesthtml]的字段值转换为类型HtmlField But HtmlField represents the type of the field and not the type of the field value. 但是HtmlField表示字段的类型,而不是字段值的类型。 Thus HtmlField html will be assigned with null . 因此, HtmlField html将被赋值为null Check this MSDN page for a reference of all publishing field types and value types. 检查此MSDN页面以获取所有发布字段类型和值类型的参考。
    I am not sure what the field value type of a HtmlField is. 我不确定HtmlField的字段值类型是什么。 Probably just string . 可能只是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. 这样就实现了数据层和表示层的分离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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