zoukankan      html  css  js  c++  java
  • SharePoint 2010 SPFieldLookup,SPFieldChoice更新值方法

    经过多番努力终于搞定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 });

  • 相关阅读:
    HDU-1702-ACboy needs your help again!(Stack)
    HDU1276-士兵队列训练问题 (Queue)
    HDU1285-确定比赛名次(拓扑+优先队列)
    The Preliminary Contest for ICPC Asia Nanjing 2019
    拓扑排序板子 hihocoder-1174
    BZOJ1066 [SCOI2007]蜥蜴
    BZOJ3888 [Usaco2015 Jan]Stampede
    BZOJ1718 [Usaco2006 Jan] Redundant Paths 分离的路径
    BZOJ1112 [POI2008]砖块Klo
    BZOJ1031 [JSOI2007]字符加密Cipher
  • 原文地址:https://www.cnblogs.com/dexter2003/p/1869971.html
Copyright © 2011-2022 走看看