关闭

sharepoint 查阅项 赋值

标签: sharepointexceptionstringlistnull文档
2377人阅读 评论(1) 收藏 举报
分类:
在项目中,经常会涉及列表或者文档库之间的相互引用,而这个时候我们用的更多的就是查阅项(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,名字 ,即显示出来的两个字段
                    }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:419348次
    • 积分:4575
    • 等级:
    • 排名:第6666名
    • 原创:65篇
    • 转载:139篇
    • 译文:0篇
    • 评论:22条
    最新评论