问题描述
我有一个名为名称"的列表,其中包含名称代码和名称.
我还有一个名为"Employee"的列表,其中包含员工名称和名称(作为查找字段).
我可以使用以下代码将值插入员工"列表.
I have a list named "Designation" that contains a Designation Code and Designation Name.
I have another list named "Employee" that contains Employee Name and Designation Name (as a Lookup Field).
I am able to insert values to "Employee" list using the following code.
protected void AddEmp(object sender, EventArgs e)
{
string emp_name = txtEmployeeName.Text;
string emp_designation = ddlDesignation.SelectedValue;
using (SPSite site = new SPSite("My Site"))
{
using (SPWeb web = site.OpenWeb())
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
web.AllowUnsafeUpdates = true;
SPList splist_employees = web.Lists["Employee"];
SPList splist_designations = web.Lists["Designations"];
SPListItemCollection splc_items = splist_employees.Items;
SPListItem spli_item = splc_items.Add();
SPFieldLookup lookup = splist_employees.Fields["Designated"] as SPFieldLookup;
string fieldName = lookup.LookupField;
spli_item["Employee Name"] = emp_name;
spli_item[fieldName] = GetLookFieldIDS(emp_designation, splist_designations);
spli_item.Update();
});
}
}
}
public static string GetLookFieldIDS(string lookupValues, SPList lookupSourceList)
{
string id = string.Empty;
SPFieldLookupValueCollection lookupIds = new SPFieldLookupValueCollection();
SPQuery query = new Microsoft.SharePoint.SPQuery();
query.Query = "<where><eq><fieldref name="Designation" /><value type="Text">" + lookupValues + "</value></eq></where>";
SPListItemCollection listItems = lookupSourceList.GetItems(query);
foreach (Microsoft.SharePoint.SPListItem item in listItems)
{
id = item.ID.ToString();
}
return id;
}
在这里,我在查询中将字段名称指定为"Designation".
但是,我想根据用户端给出的值来查找字段名称,而不是将字段名称硬编码为"Designation".
任何帮助是极大的赞赏.
预先感谢.
Here I have given the field name as ''Designation'' inside the query.
But, I want to find the field name based on the value given from the user end instead of hard-coding the field name as ''Designation''.
Any help is greatly appreciated.
Thanks in advance.
推荐答案
SPFieldLookupValue value = new SPFieldLookupValue(id, id.ToString());
item["field_name"] = value.ToString();
您还需要对代码进行一些更正.首先,是否有理由使用RunWithElevatedPrivledges?不应随便使用.如果用户没有创建列表项的权限,那么他们是否应该甚至可以使用该功能?如果确实需要使用RunWithElevatedPrivledges,则应该在其上下文中获取SPWeb对象.有了它,就可以使用当前用户的上下文来获取SPWeb对象,而不必使用RunWithElevatedPrivledges.
您设置web.AllowUnsafeUpdates = true;但永远不要将其返回false.下次调用SPWeb.Invalidate()时为true,它将恢复为false.但是,与此同时,您在环境中暴露了一个安全漏洞.由于您使用提升的特权运行,因此甚至不需要设置AllowUnsafeUpdates.
您还使用OpenWeb()获取SPWeb对象,该对象默认情况下将使用rootweb.您应该改为使用SPSite.RootWeb属性.这样会使用较少的资源,不需要处理.
您应该使用防御性编码,并在使用列表之前检查列表是否有效,或检查异常是否有效.您无法控制列表是被删除还是重命名,拥有足够特权的任何人都可以通过UI进行操作,而无需了解.如果使用的是2010,请使用SPWeb.TryGetList方法.
当然,所有这些都应放置在不直接在UI中定向的已定义数据和/或业务层中.
You also have a few thing that need corrected with your code. First, is there a reason to use RunWithElevatedPrivledges? It should not be used casually. If the user doesn''t have the permissions to create a listitem then should they even have access to the functionality? If you do need to use RunWithElevatedPrivledges then the SPWeb object should be obtained within its context. As you have it, the SPWeb object is obtained using the context for the current user making the use of RunWithElevatedPrivledges irrelevant.
You set web.AllowUnsafeUpdates = true; but never return it to false. True the next time a SPWeb.Invalidate() call is made it will revert back to false. However, in the mean time you have exposed a security hole to your environment. Since you are running with elevated privileges setting AllowUnsafeUpdates isn''t even necessary.
You are also using OpenWeb() to get the SPWeb object which by default will use the rootweb. You should use the the SPSite.RootWeb property instead. This uses fewer resources and doesn''t need to be disposed of.
You should be using defensive coding and check if the lists are valid, or handle exceptions if not, before using them. You have no control over whether the lists have been removed or renamed, anyone with sufficient privileges can do so via the UI without you knowing about it. If you are using 2010 use the SPWeb.TryGetList method.
And of course all of this should be placed in a defined data and/or business layer not directing in the UI.
这篇关于向包含查找字段的列表中添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!