Click here to Skip to main content
14,211,647 members
Rate this:
Please Sign up or sign in to vote.
See more:
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())
                        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);




        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;

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.
Updated 25-May-12 1:06am
Not Active 25-May-12 8:19am
What version of SharePoint are you using?
Georby 28-May-12 1:49am
I'm using SharePoint 2010 version.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Assuming the value of the dropdownlist is the ID of the lookup item

SPFieldLookupValue value = new SPFieldLookupValue(id, id.ToString());
item["field_name"] = value.ToString();

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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 25 May 2012
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100