【问题标题】:Save lookup value to SharePoint 2010将查找值保存到 SharePoint 2010
【发布时间】:2012-09-07 00:17:22
【问题描述】:

我已经弄清楚如何将记录添加到库中。我唯一想弄清楚的是如何(或者可能在哪里)从查找列表中保存用户的选择?

在下面的代码 sn-p 中,我正在保存一个新的列表项。它保存时没有错误,但字段“AwardType”和“AwardReason”是查找字段,虽然我没有收到错误,但没有任何内容保存到它们。如何保存到用户的查找字段选择?

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
    using (SPWeb web = site.OpenWeb())
    {
        using (FileStream fs = (new FileInfo(fileUpload.PostedFile.FileName)).OpenRead())
        {
            SPList list = web.Lists["Awards"];
            Hashtable ht = new Hashtable();
            ht.Add("wfRecipientName", txtRecipientName.Text);
            ht.Add("Office", txtOrganization.Value);
            ht.Add("AwardType", ddAwardTypes.SelectedValue);
            ht.Add("AwardReason", ddAwardReasons.SelectedValue);

            SPFile destfile = list.RootFolder.Files.Add(fileUpload.FileName, fs, ht, false);
        }
    }
}

【问题讨论】:

    标签: sharepoint


    【解决方案1】:

    使用 SPFieldLookupValue(ID, Value) 存储查找的值。

    您需要将此方法返回的对象存储在列表项字段中,而不是通过哈希表存储在属性中。在下面的示例中,Award 列表是文档库,AwardType 是类型查找的字段。

    SPList list = web.Lists["Awards"];
    Hashtable ht = new Hashtable();
    ht.Add("Office", "Chicago"); // standard property
    SPFile file = list.RootFolder.Files.Add(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName), fs, ht, true);
    SPListItem item = file.Item; // get the item for the just-added file.
    
    // assign the lookup column using SPFieldLookupValue
    item["AwardType"] = new SPFieldLookupValue(
        Int32.Parse(DropDownList1.SelectedValue),
                DropDownList1.Text);
    item.Update();  // to save the lookup column.
    

    【讨论】:

      【解决方案2】:

      有趣的是,这条线

      SPListItem item = file.Item; // get the item for the just-added file.
      

      是关键。

      我在使用下面的代码时遇到了问题 - 查找没有持续更新!?

      file.Item["AwardType"] = new SPFieldLookupValue(
          Int32.Parse(DropDownList1.SelectedValue),
                  DropDownList1.Text);
      

      【讨论】:

        【解决方案3】:

        您必须将 SPFieldLookUpValue 作为字符串添加到 HashTable,而不是 Lookup 的值。 HashTable 中存储的除 int、string、date 以外的属性在 Document 创建时不会被解析。

            SPFieldLookupValue v = new SPFieldLookupValue(item["lookUpField"].ToString());
        Hashtable documentProperties = new Hashtable();
        documentProperties.Add("key", v.ToString());
         SPFile file = docLib.RootFolder.Files.Add("fileName", memoryStream, documentProperties, true);
        

        对于复杂对象也可以像 SPUser 那样做。

        SPFieldUserValue userValue = new SPFieldUserValue(web, web.CurrentUser.ID, web.CurrentUser.LoginName);
          documentProperties.Add("SPuSER", userValue.ToString());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-13
          • 1970-01-01
          • 2011-10-10
          相关资源
          最近更新 更多