将查找值保存到SharePoint 2010

时间:2012-09-07 00:17:23

标签: sharepoint

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

在下面的代码段中,我正在保存一个新的列表项。它保存没有错误,但字段“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);
        }
    }
}

3 个答案:

答案 0 :(得分:3)

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

您需要将此方法返回的对象存储在列表项字段中,而不是通过哈希表存储属性。在下面的示例中,奖项列表是文档库,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.

答案 1 :(得分:0)

有趣的是,该行

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

是关键。

我在使用下面的代码时遇到了麻烦 - 查找没有一致更新!?

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

答案 2 :(得分:0)

您必须将SPFieldLookUpValue作为字符串添加到HashTable,而不是Lookup的值。 除了int,string,date之外的HashTable上存储的属性将不会在创建文档时进行解析。

    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());
相关问题