Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. SharePoint
  4. Add values to a list contains a Lookup Field

Add values to a list contains a Lookup Field

Scheduled Pinned Locked Moved SharePoint
sharepointdatabasehelpannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Georby
    wrote on last edited by
    #1

    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.

    N 1 Reply Last reply
    0
    • G Georby

      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.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups