sharepoint 查阅项 赋值

在项目中,经常会涉及列表或者文档库之间的相互引用,而这个时候我们用的更多的就是查阅项(lookup),以前没有去关注取值或者赋值的问题,今天正好碰到一个Case,就顺道总结一下。我们知道链接和图片的字段我们通过SPLinkFieldValue取值,那么查阅项通过什么来取值呢?答案是:SPFieldLookupValueCollection或者SPFieldLookupValue,为什么会有两种类型呢?起源是在添加查阅项字段时,是否选择了“允许多值”。一旦允许多值,就必须的通过SPFieldLookupValueCollection来做,以下是我整理的代码,仅供记录:

  1:            SPFieldLookup fieldLookup =(SPFieldLookup)list.Fields[_DisplayName];

  2:              if (fieldLookup.GetFieldValue(item[_ColName].ToString()).GetType().Name == "SPFieldLookupValueCollection")

  3:              {

  4:                  SPFieldLookupValueCollection projects =

  5:                      (SPFieldLookupValueCollection)fieldLookup.GetFieldValue(item[_ColName].ToString());

  6:

  7:                  foreach (SPFieldLookupValue lookupValue in projects)

  8:                  {

  9:                       //lookupValue.LookupValue;

  10:                       //lookupValue.LookupId.ToString();

  11:                  }

  12:              }

  13:              else

  14:              {

  15:                  SPFieldLookupValue projects =

  16:                      (SPFieldLookupValue)fieldLookup.GetFieldValue(item[_ColName].ToString());

  17:

  18:                  //projects.LookupValue;

  19:                  //projects.LookupId.ToString();

  20:              }

 

===========================================================================================================

比如有一个 SPList 其中一列 "Depart" 为查阅项,如何对这列进行赋值

SpField field = list.Fields["Depart"];
int id = ParseLookupId(value, fieldSchemaXml);
SPFieldLookupValue fieldValue = new SPFieldLookupValue(id, value);

private int ParseLookupId(string fieldValue, string fieldMessage)
  {
  int num = 0;
  try
  {
  XmlDocument document = null;
  string fieldName = string.Empty;
  string listId = string.Empty;
  try
  {
  document = new XmlDocument();
  document.LoadXml(fieldMessage);
  listId = document.DocumentElement.Attributes["List"].Value.ToString();
  fieldName += document.DocumentElement.Attributes["ShowField"].Value.ToString();
  }
  catch (Exception er)
  {
  }
  Guid id = new Guid(listId);
  SPList list = this.Web.Lists[id];
  foreach (SPListItem item in list.Items)
  {
  try
  {
  if (item[fieldName].ToString() == fieldValue)
  {
  num = Int32.Parse(item["ID"].ToString());
  }
  }
  catch (Exception er)
  {
  }
  }
  }
  catch (Exception er)
  {
  }
  return num;
  }

====================================================================================================

 using (SPWeb web = site.OpenWeb(""))
                    {

                        SPList list = web.Lists["类型列表"];
                        web.AllowUnsafeUpdates = true;
                        foreach (SPFolder folder in list.RootFolder.SubFolders)
                        {
                            if (folder.Name == "文件名称")
                            {
                                SPListItem item = folder.Item;

                                SPFieldLookup lookup = (SPFieldLookup)list.Fields["类型"];
                                if (lookup != null)
                                {
                                    //都不成功  直接"1;#文件夹"也不行
                                    item["类型"] = new SPFieldLookupValue(1, "文件夹");
                                    //item["类型"] =lookup.GetFieldValueAsText(new SPFieldLookupValue(1,"文件夹")) ;
                                    item.Update();//会提示只读域  不允许更新类似的错误提示
                                }
                            }
                        }
                        web.AllowUnsafeUpdates = false;
                    }
=======================================================================

 SPFieldLookup lookup = (SPFieldLookup)list.Fields["Conference"];
                    if (lookup != null)
                    {
                        item["Conference"] = new SPFieldLookupValue(id, cfTitle);   //id,名字 ,即显示出来的两个字段
                    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值