locked
Get a value from a lookup column RRS feed

  • Question

  • Hello,

    In my NewForm.aspx, I would like to have a lookup column to a column that is an another sub-site ?
    How can I do that in c# ?

    Thanks in advance !! :-)
    Friday, February 19, 2010 1:45 PM

Answers

  • Here is another way, let me know it works for you or not

    First add following in your newform.aspx
    <wssuc:InputFormSection runat="server"
        id="ContactID"
    	Title="Contact ID">
    	<Template_InputFormControls>
    	<wssuc:InputFormControl runat="server"  LabelAssociatedControlId="ddlContactID">		
    		<Template_Control>
    			<asp:DropDownList ID="ddlContactID" runat="server" class="ms-long" </asp:DropDownList>           
    		</Template_Control>
    		</wssuc:InputFormControl>
    	</Template_InputFormControls>
    </wssuc:InputFormSection>
    Then add following in page load event, Here I am pupolating data fro contact lists and add name (First Name + " " + Last Name)

    protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                SPSite currentSite = new SPSite("your subsite");
                SPWeb web = currentSite.OpenWeb();
                SPList ContactList = web.Lists["Contacts"];
    
                    if (ContactList.Items.Count > 0)
                    {
                        string ContactName = string.Empty;
                        foreach (SPListItem item in ContactList.Items)
                        {
                            ContactName = Convert.ToString(item["First Name"]) + " " + Convert.ToString(item["Last Name"]);
                            ListItem listItem = new ListItem(ContactName, Convert.ToString(item["Title"]));
                            this.ddlContactID.Items.Add(listItem);
                        }
                    }
                web.Dispose();
                currentSite.Dispose();
            }
        }
    Other then see you should also see

    http://cl.codeplex.com/
    http://filteredlookup.codeplex.com/
    http://cl.codeplex.com/

    Keep us update .

    Ashish
    • Marked as answer by Aaron Han - MSFT Wednesday, February 24, 2010 1:33 AM
    • Unmarked as answer by Nekrofage Thursday, February 25, 2010 3:46 PM
    • Marked as answer by Nekrofage Friday, February 26, 2010 12:02 PM
    Friday, February 19, 2010 2:31 PM
  • Thanks !!

    It works !! :-)

    We must add in the beginning of the file :

    <%@ Register TagPrefix="wssuc" TagName="InputFormSection" Src="/_controltemplates/InputFormSection.ascx" %>
    <%@ Register TagPrefix="wssuc" TagName="InputFormControl" Src="/_controltemplates/InputFormControl.ascx" %>
    
    Another question : how to get the value of this dropdown column ?
    • Marked as answer by Nekrofage Friday, February 26, 2010 12:02 PM
    Friday, February 26, 2010 10:49 AM
  • Thanks to you help, I find this tips :

    string getValueColorname = this.ddlcolorName.SelectedItem.Value;

    In my case, here, I get the value of the color not the text.
    • Marked as answer by Nekrofage Friday, February 26, 2010 12:02 PM
    Friday, February 26, 2010 11:53 AM

All replies


  • You can set programatically lookup column settings as follow:

    private void SetCrossSiteLookupColumn(SPFieldLookup lookup, SPWeb web, SPList list, SPField field)
    {
        lookup.LookupWebId = web.ID;
        lookup.LookupList = list.ID.ToString("B");
        lookup.LookupField = field.InternalName;
        lookup.Update();
    }

    or use existed Cross-site lookup column with additional useful features:

    http://www.artfulbits.com/SharePointProducts/CrossSiteLookup.aspx

     - Yura
    • Marked as answer by Aaron Han - MSFT Wednesday, February 24, 2010 1:33 AM
    • Unmarked as answer by Nekrofage Thursday, February 25, 2010 3:46 PM
    Friday, February 19, 2010 1:56 PM
  • Here is another way, let me know it works for you or not

    First add following in your newform.aspx
    <wssuc:InputFormSection runat="server"
        id="ContactID"
    	Title="Contact ID">
    	<Template_InputFormControls>
    	<wssuc:InputFormControl runat="server"  LabelAssociatedControlId="ddlContactID">		
    		<Template_Control>
    			<asp:DropDownList ID="ddlContactID" runat="server" class="ms-long" </asp:DropDownList>           
    		</Template_Control>
    		</wssuc:InputFormControl>
    	</Template_InputFormControls>
    </wssuc:InputFormSection>
    Then add following in page load event, Here I am pupolating data fro contact lists and add name (First Name + " " + Last Name)

    protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                SPSite currentSite = new SPSite("your subsite");
                SPWeb web = currentSite.OpenWeb();
                SPList ContactList = web.Lists["Contacts"];
    
                    if (ContactList.Items.Count > 0)
                    {
                        string ContactName = string.Empty;
                        foreach (SPListItem item in ContactList.Items)
                        {
                            ContactName = Convert.ToString(item["First Name"]) + " " + Convert.ToString(item["Last Name"]);
                            ListItem listItem = new ListItem(ContactName, Convert.ToString(item["Title"]));
                            this.ddlContactID.Items.Add(listItem);
                        }
                    }
                web.Dispose();
                currentSite.Dispose();
            }
        }
    Other then see you should also see

    http://cl.codeplex.com/
    http://filteredlookup.codeplex.com/
    http://cl.codeplex.com/

    Keep us update .

    Ashish
    • Marked as answer by Aaron Han - MSFT Wednesday, February 24, 2010 1:33 AM
    • Unmarked as answer by Nekrofage Thursday, February 25, 2010 3:46 PM
    • Marked as answer by Nekrofage Friday, February 26, 2010 12:02 PM
    Friday, February 19, 2010 2:31 PM
  • Hello Ashish Kanoongo ,

    Does it possible to insert your first code into a customized NewForm.aspx ?

    I got this error too :

    Unknown server tag '...'.

    Thanks in advance.
    Thursday, February 25, 2010 3:54 PM
  • Hello

    You can add code in following format after namespace tag

    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                SPSite currentSite = new SPSite("your subsite");
                SPWeb web = currentSite.OpenWeb();
                SPList ContactList = web.Lists["Contacts"];

                    if (ContactList.Items.Count > 0)
                    {
                        string ContactName = string.Empty;
                        foreach (SPListItem item in ContactList.Items)
                        {
                            ContactName = Convert.ToString(item["First Name"]) + " " + Convert.ToString(item["Last Name"]);
                            ListItem listItem = new ListItem(ContactName, Convert.ToString(item["Title"]));
                            this.ddlContactID.Items.Add(listItem);
                        }
                    }
                web.Dispose();
                currentSite.Dispose();
            }
        }
    </script>

    And do not for got update your parset path tage in web config as mentioned below and let me know it works for your or not

    <PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />


    Ashish Kanoongo, MCP, MCSD, MCTS
    Thursday, February 25, 2010 9:43 PM
  • Thanks !!

    It works !! :-)

    We must add in the beginning of the file :

    <%@ Register TagPrefix="wssuc" TagName="InputFormSection" Src="/_controltemplates/InputFormSection.ascx" %>
    <%@ Register TagPrefix="wssuc" TagName="InputFormControl" Src="/_controltemplates/InputFormControl.ascx" %>
    
    Another question : how to get the value of this dropdown column ?
    • Marked as answer by Nekrofage Friday, February 26, 2010 12:02 PM
    Friday, February 26, 2010 10:49 AM
  • item["fieldname"] = this.ddlContactID.Text;

    Let me know it works for you or not.
    Friday, February 26, 2010 11:38 AM
  • Thanks to you help, I find this tips :

    string getValueColorname = this.ddlcolorName.SelectedItem.Value;

    In my case, here, I get the value of the color not the text.
    • Marked as answer by Nekrofage Friday, February 26, 2010 12:02 PM
    Friday, February 26, 2010 11:53 AM