浅谈使用Linq to sharepoint时可能遇到的2个问题

1.你使用了Metadata column,你发现你点不出这些column
你用SPMetal.exe得到的类是不包括Metadata column的,解决办法是使用partial class和提供的ICustomMapping接口,新建一个类似于以下的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
namespace CLPCablingEntityClass
{
public partial class PO : ICustomMapping
{
private const string PO_STATUS_FIELD = "CLPP_PO_Status";
private const string LOCATION_FIELD = "CLPP_PO_Location";
[CustomMapping(Columns = new String[] { PO_STATUS_FIELD, LOCATION_FIELD })]
public void MapFrom(object listItem)
{
SPListItem item = (SPListItem)listItem;
this.CLPPPOStatus = item[item.Fields.GetFieldByInternalName(PO_STATUS_FIELD).Id] as TaxonomyFieldValue;
this.CLPPPOLocation = item[item.Fields.GetFieldByInternalName(LOCATION_FIELD).Id] as TaxonomyFieldValue;
}
public void MapTo(object listItem)
{
SPListItem item = (SPListItem)listItem;
item[item.Fields.GetFieldByInternalName(PO_STATUS_FIELD).Id] = this.CLPPPOStatus;
item[item.Fields.GetFieldByInternalName(LOCATION_FIELD).Id] = this.CLPPPOLocation;
}
public void Resolve(RefreshMode mode, object originalListItem, object databaseListItem)
{
SPListItem originalItem = (SPListItem)originalListItem;
SPListItem databaseItem = (SPListItem)databaseListItem;
TaxonomyFieldValue originalStatusValue = (TaxonomyFieldValue)originalItem[originalItem.Fields.GetFieldByInternalName(PO_STATUS_FIELD).Id];
TaxonomyFieldValue originalLocationValue = (TaxonomyFieldValue)originalItem[originalItem.Fields.GetFieldByInternalName(LOCATION_FIELD).Id];
TaxonomyFieldValue dbStatusValue = (TaxonomyFieldValue)databaseItem[databaseItem.Fields.GetFieldByInternalName(PO_STATUS_FIELD).Id];
TaxonomyFieldValue dbLocationValue = (TaxonomyFieldValue)databaseItem[databaseItem.Fields.GetFieldByInternalName(LOCATION_FIELD).Id];
if (mode == RefreshMode.OverwriteCurrentValues)
{
this.CLPPPOStatus = dbStatusValue;
this.CLPPPOLocation = dbLocationValue;
}
else if (mode == RefreshMode.KeepCurrentValues)
{
databaseItem[databaseItem.Fields.GetFieldByInternalName(PO_STATUS_FIELD).Id] = this.CLPPPOStatus;
databaseItem[databaseItem.Fields.GetFieldByInternalName(LOCATION_FIELD).Id] = this.CLPPPOLocation;
}
else if (mode == RefreshMode.KeepChanges)
{
if (this.CLPPPOStatus != originalStatusValue)
{
databaseItem[databaseItem.Fields.GetFieldByInternalName(PO_STATUS_FIELD).Id] = this.CLPPPOStatus;
}
else if (this.CLPPPOStatus == originalStatusValue && this.CLPPPOStatus != dbStatusValue)
{
this.CLPPPOStatus = dbStatusValue;
}
if (this.CLPPPOLocation != originalLocationValue)
{
databaseItem[databaseItem.Fields.GetFieldByInternalName(LOCATION_FIELD).Id] = this.CLPPPOLocation;
}
else if (this.CLPPPOLocation == originalLocationValue && this.CLPPPOLocation != dbLocationValue)
{
this.CLPPPOLocation = dbLocationValue;
}
}
}
private TaxonomyFieldValue _cLPPPOStatus;
public TaxonomyFieldValue CLPPPOStatus
{
get
{
return _cLPPPOStatus;
}
set
{
if ((value != _cLPPPOStatus))
{
this.OnPropertyChanging(PO_STATUS_FIELD, this._cLPPPOStatus);
_cLPPPOStatus = value;
this.OnPropertyChanged(PO_STATUS_FIELD);
}
}
}
private TaxonomyFieldValue _cLPPPOLocation;
public TaxonomyFieldValue CLPPPOLocation
{
get
{
return _cLPPPOLocation;
}
set
{
if ((value != _cLPPPOLocation))
{
this.OnPropertyChanging(LOCATION_FIELD, this._cLPPPOLocation);
_cLPPPOLocation = value;
this.OnPropertyChanged(LOCATION_FIELD);
}
}
}
}
}

2. 你的list使用了Workflow,当你用linq to sharepoint去get正在有工作流运行的listitem时,你会得到一个Specified cast is not valid.的error
这是SPMetal工具的一个bug,它把工作流状态column Generate成System.Nullable类型的字段,但是他实际上是一个Object类型的字段,http://msdn.microsoft.com/en-us/library/ee536245.aspx。
解决办法是手动去修改SPMetal Generate出来的类,把FieldType = "WorkflowStatus"的field的Nullable<>类型改回Object,类似以下代码,做好准备,每一次你从site重新Generate这个类的时候,你都要手动改所有涉及workflow的代码。
private Object _cableRequest0;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate();
partial void OnCreated();
#endregion
public CableRequestCableRequest()
{
this.OnCreated();
}
[Microsoft.SharePoint.Linq.ColumnAttribute(Name = "CableReq", Storage = "_cableRequest0", ReadOnly = true, FieldType = "WorkflowStatus")]
public Object CableRequest0
{
get
{
return this._cableRequest0;
}
set
{
if ((value != this._cableRequest0))
{
this.OnPropertyChanging("CableRequest0", this._cableRequest0);
this._cableRequest0 = value;
this.OnPropertyChanged("CableRequest0");
}
}

转载于:https://www.cnblogs.com/sharepoint2010/archive/2012/12/16/2820333.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值