扩展 linq to sharepoint 以发布 HTML 字段
我创建了一个分部类来扩展默认的 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)));
}
}
}
两个问题:
- 为什么
HtmlField html = item[CONTENT_FIELDtesthtml] as HtmlField;
返回null
,但item.Fields.GetFieldByInternalName
工作正常吗? - 有没有办法从内部使用
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:
- Why does
HtmlField html = item[CONTENT_FIELDtesthtml] as HtmlField;
returnnull
, but theitem.Fields.GetFieldByInternalName
works correctly? - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将
item[CONTENT_FIELDtesthtml]
的字段值转换为HtmlField
类型。但是HtmlField
表示字段的类型,而不是字段值的类型。因此,HtmlField html
将被赋值为null
。检查此 MSDN 页面以获取所有发布字段类型的参考和值类型。我不确定
HtmlField
的字段值类型是什么。可能只是字符串
。所以你应该可以安全地将其转换为字符串:
我认为将值存储在属性中是正确的方法。通过这种方式,您可以实现数据层和表示层的分离。
You are casting the field value of
item[CONTENT_FIELDtesthtml]
to the typeHtmlField
. ButHtmlField
represents the type of the field and not the type of the field value. ThusHtmlField html
will be assigned withnull
. 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 juststring
.So you should be safe to convert it to string:
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.