如何通过C#在SharePoint 2007中测试查找列表项,并在缺少时添加它?

如何通过C#在SharePoint 2007中测试查找列表项,并在缺少时添加它?,c#,sharepoint,sharepoint-2007,C#,Sharepoint,Sharepoint 2007,嗨,谢谢你的关注 背景 尝试向查找列添加值时遇到问题 我正在使用SharePoint 2007,该应用程序必须在.NET 2.0中运行。语言是C#某些查找列将允许多个值。 问题: 使用C#,我如何做#的2-4项: 尝试将列表项添加到SP列表 对于任何查找列,请检查它们引用的SP列表,以查看该列表是否包含我尝试添加的值 如果该值在查找列表中不存在,请添加它 将新添加的查找值与我最初尝试添加的列表项相关联 当然,我已经在谷歌上搜索过了,但我还是被卡住了。下面是一些来自微软的代码,这是一个开始,但它仍

嗨,谢谢你的关注

背景 尝试向查找列添加值时遇到问题

我正在使用SharePoint 2007,该应用程序必须在.NET 2.0中运行。语言是C#某些查找列将允许多个值。

问题: 使用C#,我如何做#的2-4项:

  • 尝试将列表项添加到SP列表
  • 对于任何查找列,请检查它们引用的SP列表,以查看该列表是否包含我尝试添加的值
  • 如果该值在查找列表中不存在,请添加它
  • 将新添加的查找值与我最初尝试添加的列表项相关联
  • 当然,我已经在谷歌上搜索过了,但我还是被卡住了。下面是一些来自微软的代码,这是一个开始,但它仍然不能让我继续(没有评论,对我来说也不直观):

    即使有微软的例子,我也不知道如何真正做到这一点


    非常感谢您的帮助。

    如果我收到“StackOverflow不是一个租用编码器”的评论,我还想补充一点:我必须在远程连接的SP服务器上开发,由于IT部门正在使用某种插件,我无法调试。这就像蒙着眼睛编码一样。再次感谢您的帮助。谢谢sga101!我会查一查。
    //untested pseudocode - hope this points you in the right direction
    
    SPList lookupItems = ... // add code here
    SPList list = ... // add code here
    string lookupFieldName = "LookupValue"; // change to the appropriate value
    
    foreach(SPListItem item in list.Items)
    { 
        string value = (string)item[lookupFieldName];
        if(value.Contains("#")) // value containing hash is most likely a lookup value already
        {
             // use SPFieldLookupValue to get actual value
             SPFieldLookupValue currentValue = new SPFieldLookupValue(value);
             value = currentValue.LookupValue;
        }
    
        // Get the list item (you will need this to find out its id value)
        SPListItem lookupItem = GetLookupListItem(lookupList, value);
        if(lookupItem == null)
        {
            //If it doesn't exist, create it
            lookupItem = AddNewLookupItem(lookupList, value);
            SPFieldLookupValue lookupValue = new      SPFieldLookupValue(lookupItem.ID,value));
            item["LookupValue"] = lookupValue.ToString();
            item.Update();
        }
    }
    SPListItem GetLookupListItem(SPList lookupList, string value)
    {
        // iterate through list to find item
        // use a list query if the list is too big for this to perform well (see here: http://msdn.microsoft.com/en-us/library/ie/ms456030.aspx)
        foreach(SPListItem item in lookupList)
        {
            string itemValue = (string)item[0]; // assuming lookup list has one field of type string containing lookup value
            if(value == itemValue)
            {
                return item;
            }
        }
        return null;
    }
    
    SPListItem AddLookupListItem(SPList list, string value)
    {
        SPListItem newItem = list.Add();
        newItem[0] = value;// assuming lookup list has one field of type string containing lookup value
        newItem.Update();
    }
    
    //untested pseudocode - hope this points you in the right direction
    
    SPList lookupItems = ... // add code here
    SPList list = ... // add code here
    string lookupFieldName = "LookupValue"; // change to the appropriate value
    
    foreach(SPListItem item in list.Items)
    { 
        string value = (string)item[lookupFieldName];
        if(value.Contains("#")) // value containing hash is most likely a lookup value already
        {
             // use SPFieldLookupValue to get actual value
             SPFieldLookupValue currentValue = new SPFieldLookupValue(value);
             value = currentValue.LookupValue;
        }
    
        // Get the list item (you will need this to find out its id value)
        SPListItem lookupItem = GetLookupListItem(lookupList, value);
        if(lookupItem == null)
        {
            //If it doesn't exist, create it
            lookupItem = AddNewLookupItem(lookupList, value);
            SPFieldLookupValue lookupValue = new      SPFieldLookupValue(lookupItem.ID,value));
            item["LookupValue"] = lookupValue.ToString();
            item.Update();
        }
    }
    SPListItem GetLookupListItem(SPList lookupList, string value)
    {
        // iterate through list to find item
        // use a list query if the list is too big for this to perform well (see here: http://msdn.microsoft.com/en-us/library/ie/ms456030.aspx)
        foreach(SPListItem item in lookupList)
        {
            string itemValue = (string)item[0]; // assuming lookup list has one field of type string containing lookup value
            if(value == itemValue)
            {
                return item;
            }
        }
        return null;
    }
    
    SPListItem AddLookupListItem(SPList list, string value)
    {
        SPListItem newItem = list.Add();
        newItem[0] = value;// assuming lookup list has one field of type string containing lookup value
        newItem.Update();
    }