Add values to a list contains a Lookup Field
-
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 = ""+lookupValues + ""; SPListItemCollection listItems = lookupSourceList.GetItems(query); foreach (Microsoft.SharePoint.SPListItem item in listItems) { id = item.ID.ToString(); } return id; } 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.
-
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 = ""+lookupValues + ""; SPListItemCollection listItems = lookupSourceList.GetItems(query); foreach (Microsoft.SharePoint.SPListItem item in listItems) { id = item.ID.ToString(); } return id; } 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.
Format your code snippets when posting don't post to multiple forums. You have already asked in Q&A
Failure is not an option; it's the default selection.