将多个查找值插入/更新到 SPListItem
我的 SharePoint 列表有一列允许多个查找值。我的 C# 控件(在 Web 部件内)允许用户从列表框中进行多项选择。我将这些值拆分为一个数组 - 每个数组成员都是需要在同一 SPListItem 列中更新的选定值。
我知道选择是从列表框中正确传递的 - 我只需要将这组值添加到 SPListItem 中的同一列。
我哪里错了?
SPFieldLookupValueCollection MyCollection = new SPFieldLookupValueCollection();
for (int i = 0; i < MyArrayOfSelections.Length; i++)
{
if (MyLookupList["LookupColumn"].ToString() == MyArrayOfSelections[i].ToString())
{
MyID = int.Parse(MyLookupList[i]["ID"].ToString());
SPFieldLookupValue thisSelection = new SPFieldLookupValue(MyID,MyArrayOfSelections[i].ToString());
MySubCollection.Add(thisSelection);
}
}
ListIWantToUpdate["ColumnWithMultipleLookupSelections"] = SubCollection;
ListIWantToUpdate.Update();
site.Update();
}
My SharePoint list has a column that allows multiple lookup values. My C# control (within a webpart) allows the user to make multiple selections from a list box. I split these values into an array - each array member being a selected value that needs to be updated in the same SPListItem column.
I know that the selections are being passed in properly from the list box - I just need to add this group of values to the same column in the SPListItem.
Where am I going wrong?
SPFieldLookupValueCollection MyCollection = new SPFieldLookupValueCollection();
for (int i = 0; i < MyArrayOfSelections.Length; i++)
{
if (MyLookupList["LookupColumn"].ToString() == MyArrayOfSelections[i].ToString())
{
MyID = int.Parse(MyLookupList[i]["ID"].ToString());
SPFieldLookupValue thisSelection = new SPFieldLookupValue(MyID,MyArrayOfSelections[i].ToString());
MySubCollection.Add(thisSelection);
}
}
ListIWantToUpdate["ColumnWithMultipleLookupSelections"] = SubCollection;
ListIWantToUpdate.Update();
site.Update();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码示例的最后几行令人困惑(也许只是变量命名)。如果您只是更新数据,则永远不需要更新
SPList
对象(这需要特定列表的“管理列表”权限),也不需要更新SPSite< /code> ojbect(要求您是站点管理员或所有者)。因此,普通用户无法成功运行此代码。
The last rows of the code example are confusing (maybe it's just variable naming). If you are just updating the data, you never need to update neither the
SPList
object (this requires "Manage lists" permission on the particular list, norSPSite
ojbect (requires you to be site administrator or owner). So, this code will not run succcessfuly for a regular user.