SharePoint 2010 SPFieldLookup,SPFieldChoice更新值方法
2010-11-05 15:04
155 查看
经过多番努力终于搞定SPFieldLookup,SPFieldChoice字段在Item中 如何更新值了,记下以备后用!
///////////////////////////////////
#region
//public method
///
<summary>
///
Returns the SPFieldLookupValue instance of a lookup value.
///
The ID value will be obtained using SPQuery.
///
</summary>
///
<param name="_web"></param>
///
<param name="_field"></param>
///
<param name="_lookupvalue"></param>
///
<returns></returns>
public SPFieldLookupValue GetLookupValue(SPWeb _web, SPFieldLookup _field, string
_lookupvalue)
{
string
queryFormat =
@"<Where>
<Eq><FieldRef Name='{0}'><Value Type='Text'>{1}</Eq>
</Where>"
;
string queryText = string
.Format(queryFormat, _field, _lookupvalue);
SPList lookupList = _web.Lists[new Guid
(_field.LookupList)];
SPListItemCollection
lookupItems =
lookupList.GetItems(
new SPQuery
if
(lookupItems.Count > 0)
{
int lookupId = Convert.ToInt32(lookupItems[0][SPBuiltInFieldId
.ID]);
return new SPFieldLookupValue
(lookupId, _lookupvalue);
}
else
{
return null
;
}
}
public void SetFieldValueLookup(SPListItem _item, string _fieldname, string
_lookupValue)
{
if (_item != null
)
{
SPFieldLookup field = _item.Fields.GetFieldByInternalName(_fieldname) as SPFieldLookup
;
_item[_fieldname] = GetLookupValue(_item.Web, field, _lookupValue);
_item.Update();
}
else
{
_item[_fieldname] =
null
;
}
}
///
<summary>
///
Set the values of a Lookup-Field with multiple values allowed.
///
</summary>
public void SetFieldValueLookup(SPListItem
_item,
string _fieldName, IEnumerable<string
> _lookupValues)
{
if (_item != null
)
{
SPFieldLookup
field =
_item.Fields.GetFieldByInternalName(_fieldName)
as SPFieldLookup
;
SPFieldLookupValueCollection
fieldValues =
new SPFieldLookupValueCollection
();
foreach (string lookupValue in
_lookupValues)
{
fieldValues.Add(
GetLookupValue(_item.Web, field, lookupValue));
}
_item[_fieldName] = fieldValues;
_item.Update();
}
}
public void SetFieldValueChoice(SPListItem _item, string _fieldname, string
_choiseValue)
{
SPFieldChoice statusField = _item.Fields.GetFieldByInternalName(_fieldname) as SPFieldChoice
;
_item[statusField.Id] = statusField.Choices[statusField.Choices.IndexOf(_choiseValue)];
_item.Update();
}
() { Query = queryText });
///////////////////////////////////
#region
//public method
///
<summary>
///
Returns the SPFieldLookupValue instance of a lookup value.
///
The ID value will be obtained using SPQuery.
///
</summary>
///
<param name="_web"></param>
///
<param name="_field"></param>
///
<param name="_lookupvalue"></param>
///
<returns></returns>
public SPFieldLookupValue GetLookupValue(SPWeb _web, SPFieldLookup _field, string
_lookupvalue)
{
string
queryFormat =
@"<Where>
<Eq><FieldRef Name='{0}'><Value Type='Text'>{1}</Eq>
</Where>"
;
string queryText = string
.Format(queryFormat, _field, _lookupvalue);
SPList lookupList = _web.Lists[new Guid
(_field.LookupList)];
SPListItemCollection
lookupItems =
lookupList.GetItems(
new SPQuery
if
(lookupItems.Count > 0)
{
int lookupId = Convert.ToInt32(lookupItems[0][SPBuiltInFieldId
.ID]);
return new SPFieldLookupValue
(lookupId, _lookupvalue);
}
else
{
return null
;
}
}
public void SetFieldValueLookup(SPListItem _item, string _fieldname, string
_lookupValue)
{
if (_item != null
)
{
SPFieldLookup field = _item.Fields.GetFieldByInternalName(_fieldname) as SPFieldLookup
;
_item[_fieldname] = GetLookupValue(_item.Web, field, _lookupValue);
_item.Update();
}
else
{
_item[_fieldname] =
null
;
}
}
///
<summary>
///
Set the values of a Lookup-Field with multiple values allowed.
///
</summary>
public void SetFieldValueLookup(SPListItem
_item,
string _fieldName, IEnumerable<string
> _lookupValues)
{
if (_item != null
)
{
SPFieldLookup
field =
_item.Fields.GetFieldByInternalName(_fieldName)
as SPFieldLookup
;
SPFieldLookupValueCollection
fieldValues =
new SPFieldLookupValueCollection
();
foreach (string lookupValue in
_lookupValues)
{
fieldValues.Add(
GetLookupValue(_item.Web, field, lookupValue));
}
_item[_fieldName] = fieldValues;
_item.Update();
}
}
public void SetFieldValueChoice(SPListItem _item, string _fieldname, string
_choiseValue)
{
SPFieldChoice statusField = _item.Fields.GetFieldByInternalName(_fieldname) as SPFieldChoice
;
_item[statusField.Id] = statusField.Choices[statusField.Choices.IndexOf(_choiseValue)];
_item.Update();
}
() { Query = queryText });
相关文章推荐
- sharepoint 2010 FieldLookup 的更新方法
- SharePoint 2010 自定义Timer job 问题总结(拒绝访问,Execute方法不执行,不及时更新)
- sharepoint 2010 FieldLookup 的更新方法
- SharePoint 2010 自定义Timer job 问题总结(拒绝访问,Execute方法不执行,不及时更新)
- sharepoint 2010 FieldLookup 的更新方法
- SharePoint 2010 Reporting Services 报表服务器实例没有正确配置 解决方法
- SharePoint 2010 MasterPage 去Ribbon的方法
- SharePoint 2010 MasterPage去Ribbon 的方法
- sharepoint 2010 中添加webpart时引用dll一些问题及解决方法。
- 使用新的方法来实现SharePoint网站日志更新
- SharePoint【学习笔记】-- 自定义 SharePoint 2010 的配色主题的几种方法
- sharepoint 2010 获取列表文件夹所有数据方法
- SharePoint 2010 MasterPage去Ribbon 的方法
- SharePoint 2010 MasterPager去Ribbon 的方法
- SharePoint 2010 RMS Word 2010 您没有权限打开文档 解决方法
- [SharePoint 2010]在SharePoint中几种访问域用户profile的方法
- SharePoint 2010 常用技巧及方法总结
- SharePoint 2010基于Ad登陆的方法
- SharePoint 2007 到 SharePoint 2010 迁移项目实战心得系列(二):升级方法/步骤
- 复制转移sharepoint 2010 designer做的list workflow的方法