1

In SharePoint Server 2013 I have a 4 list

  1. Common List
  2. HR list
  3. Finance list
  4. IT List

How can I achieve this solution please Modify this code Empstrength textbox I type above 50 and I select a HR list the data will store in commonlist and HR list.

protected void Button1_Click(object sender, EventArgs e) {
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["CommonList"];
                    SPListItem listItem = list.Items.Add();

                    listItem["Department"] = Department.Text;
                    listItem["EmpStrength"] = EmpStrength.Text;
                    listItem["Description"] = Description.Text;
                    listItem["DueDate"] = DueDate.Text;

                    //UniqueId = int.Parse(listItem["ConnectID"].ToString());
                    // listItem["ConnectID"] = UniqueId + 1;
                    listItem["Status"] = Status.Text;
                    web.AllowUnsafeUpdates = true;
                    listItem.Update();
                    //Guid id = listItem.UniqueId;
                }
            }

        if ( 50 <= int.Parse(EmpStrength.Text))
        {
            Department.SelectedItem.Text = "HRList";
            SPSite site = new SPSite(SPContext.Current.Web.Url);
            SPWeb web = site.OpenWeb();
            SPList lis = web.Lists["HRList"];

            SPListItem lisItem = lis.Items.Add();
            lisItem["EmpStrength"] = EmpStrength.Text;
            lisItem["Status"] = Status.Text;
            web.AllowUnsafeUpdates = true;
            lisItem.Update();
        }
        else if (50 <= int.Parse(EmpStrength.Text))
        {
            Department.SelectedItem.Text = "ITList";
            SPSite site = new SPSite(SPContext.Current.Web.Url);
            SPWeb web = site.OpenWeb();
            SPList lis = web.Lists["ITList"];

            SPListItem liss = lis.Items.Add();
            liss["EmpStrength"] = EmpStrength.Text;
            liss["Status"] = Status.Text;
            web.AllowUnsafeUpdates = true;
            liss.Update();
        }
        else
        {
            Department.SelectedItem.Text = "FinanceList";
            SPSite site = new SPSite(SPContext.Current.Web.Url);
            SPWeb web = site.OpenWeb();
            SPList lis = web.Lists["FinanceList"];

            SPListItem liss = lis.Items.Add();
            liss["EmpStrength"] = EmpStrength.Text;
            liss["Status"] = Status.Text;
            web.AllowUnsafeUpdates = true;
            liss.Update();
        }
    }
}
}

Department.SelectedItem.Text = "ITList";
Department.SelectedItem.Text = "HRList";
Department.SelectedItem.Text = "FinanceList";

is it correct..?

Dropdown selection not working here I entered above 50 it will store in Common list and HRlist. In dropdown I selected ITList or Finance List it will stored in only HRList and Commonlist. Please anyone modify this code.

  • Anyone give me a sample code for this how to achieve this solution. – Elina Jan 12 '16 at 8:31
  • Seeing such webpart, I believe you missed something. You should really empower native features of SharePoint : content types, list schemas, etc... helped with 3rd party product (nintex form for example). It's hard to explain briefly, but I think you should really considering exploring in depth how SP works. Rebuilding the ui is often like opening the pandora's box. – Steve B Jan 12 '16 at 11:53
  • Your dropdown value retrieval logic is wrong. Have a look at my answer how i get the selected value from the dropdown and also use that value in the if condition with an AND operator. – HarryB Jan 13 '16 at 10:47
  • i should give like this but error came .if (int.Parse(EmpStrength.Text) >50 && Department.SelectedValue = "HRList" ) – Elina Jan 13 '16 at 11:05
  • use SelectedItem.Text or show me your dropdown markup – HarryB Jan 13 '16 at 12:29
0

May be this can help you get started:

In the onclick method of you submit button use If Else conditions to check the form values such as :

   using (SPSite site = new SPSite(SPContext.Current.Web.Url))
    {
        using (SPWeb web = site.OpenWeb())
        {


            SPList list = web.Lists["CommonList"];

            SPListItem listItem = list.Items.Add();

            listItem["Department"] = Department.Text;
            listItem["EmpStrength"] = EmpStrength.Text;
            listItem["Description"] = Description.Text;
            listItem["DueDate"] = DueDate.Text;

            //UniqueId = int.Parse(listItem["ConnectID"].ToString());
            // listItem["ConnectID"] = UniqueId + 1;
            listItem["Status"] = Status.Text;
            web.AllowUnsafeUpdates = true;
            listItem.Update();
            //Guid id = listItem.UniqueId;

           if ( 50 <= int.Parse(EmpStrength.Text) && Department.SelectedItem.Text == "HRList")
{


   // SPSite site = new SPSite(SPContext.Current.Web.Url);
    //SPWeb web = site.OpenWeb();
    SPList lis = web.Lists["HRList"];

    SPListItem lisItem = lis.Items.Add();
    lisItem["EmpStrength"] = EmpStrength.Text;
    lisItem["Status"] = Status.Text;
    //web.AllowUnsafeUpdates = true;
    lisItem.Update();
}
else if (50 <= int.Parse(EmpStrength.Text) &&Department.SelectedItem.Text =="ITList")
{

   // SPSite site = new SPSite(SPContext.Current.Web.Url);
   // SPWeb web = site.OpenWeb();
   SPList  lis = web.Lists["ITList"];

    SPListItem liss = lis.Items.Add();
    liss["EmpStrength"] = EmpStrength.Text;
    liss["Status"] = Status.Text;
    web.AllowUnsafeUpdates = true;
    liss.Update();
}
else if(Department.SelectedItem.Text == "FinanceList")
{

   // SPSite site = new SPSite(SPContext.Current.Web.Url);
    //SPWeb web = site.OpenWeb();
    SPList lis = web.Lists["FinanceList"];

    SPListItem liss = lis.Items.Add();
    liss["EmpStrength"] = EmpStrength.Text;
    liss["Status"] = Status.Text;
   // web.AllowUnsafeUpdates = true;
    liss.Update();

}
 web.AllowUnsafeUpdates = false;
        }
    }

Below link shows code example for adding a new list item to a list

https://msdn.microsoft.com/EN-US/library/office/ms470423.aspx

  • Actually i am new to this field i dont know how to store data in 4 list based on that dropdown list. – Elina Jan 12 '16 at 11:38
  • You can get the value of dropdown list either by using dropdownlist.SelectedValue or dropdownlist.SelectedItem or dropdownlist.SelectedIndex properties. I have updated my answer accordingly. Have a look here for more info msdn.microsoft.com/en-us/library/… – HarryB Jan 12 '16 at 11:51
  • Use a native list with your fields. Plug a small a simple workflow that route the item to either of the list, depending on the value. Do not solve this problem with webparts! – Steve B Jan 12 '16 at 12:02
  • hi,Now i edited my code just see my code and give me a solution, Advance thanks. – Elina Jan 13 '16 at 7:47
  • cool. Dont forget to mark as answer :) – HarryB Jan 13 '16 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.