locked
Copying data from one list to another RRS feed

  • Question

  • Frustrated Newbie ...

    I have two lists. Both on same SharePoint Site. List A holds a date I wish to place in list B. The two lists have a Company field with exact Company. I want to query List A for the Company name and if it's the same as the Company name in List B, return the Date field.

    I have tried everything I could find in Google and still confused...

    Thank you ahead of time.

    Warprunner01 

     
    Tuesday, May 27, 2014 2:58 PM

Answers

  • If you want to handle this through code try this.

       using(SPSite site = new SPSite(""))
                {
                using (SPWeb web = site.OpenWeb())
                {
                SPList listA = web.Lists["ListA"];
                SPList listB = web.Lists["ListB"];
                foreach(SPListItem itemA in listA.Items)
                {
                SPQuery query = new SPQuery();
                query.Query="<Where><Eq><FieldRef Name='Country' /><Value Type='Text'>"+itemA ["Country"].ToString ()+"</Value></Eq></Where>";
                SPListItemCollection items = listB.GetItems (query);
                foreach (SPListItem itemB in items)
                {
                    //return the date field
                }
                }
                }


    Tuesday, May 27, 2014 3:18 PM

All replies

  • If you want to handle this through code try this.

       using(SPSite site = new SPSite(""))
                {
                using (SPWeb web = site.OpenWeb())
                {
                SPList listA = web.Lists["ListA"];
                SPList listB = web.Lists["ListB"];
                foreach(SPListItem itemA in listA.Items)
                {
                SPQuery query = new SPQuery();
                query.Query="<Where><Eq><FieldRef Name='Country' /><Value Type='Text'>"+itemA ["Country"].ToString ()+"</Value></Eq></Where>";
                SPListItemCollection items = listB.GetItems (query);
                foreach (SPListItem itemB in items)
                {
                    //return the date field
                }
                }
                }


    Tuesday, May 27, 2014 3:18 PM
  • Don't mind trying but where do I put the code?
    Tuesday, May 27, 2014 3:25 PM
  • Hello Warprunner01,

    >Don't mind trying but where do I put the code?

    How and when you want to do it? shravan solution is code based and you need to use that code in visual studio.

    One out of box solution is, you can open both lists in datasheet view and filter data by "Company name" column then manually copy and paste the data from listA to listB.

    Now another way is designer, but designer solution works based on event like: add / update event.

    So you need to tell us when you want this.


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Wednesday, May 28, 2014 9:16 AM
    Moderator
  • If this is not event based, you can create a console applition and put the above code  and run it manually or schedule it. 

    If this is a event based you can create a Visual Stuido Workflow with the code i have given. 

    I am not big fan of SharePoint Designer workflow when multiple items needed to be updated at the same time.

    Wednesday, May 28, 2014 4:19 PM