您的位置:首页 > 其它

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 });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: