Copy data between lists in Sharepoint

I hadn't but that works perfectly!

Now just to limit it to a random percentage of items....I'm off to Google to have a look!

Thanks very much for your help with this! Most appreciated!
 
I've already got that bit in my CopyItems method. At the minute, the app is copying all rows for the three fields into the destination list.

Now I just need to tell it to only copy a selection of rows...

I'm thinking of initializing an array of x values (x determined by 5 percent of the total number of items in the source list) and then looping through generating a random number between 1 and total number of items in the source list until all values in the array are populated.

Then I'd need to pass this to your foreach statement instead of passing sourceList.Items.

Am I on the right track?
 
I seem to have gone backwards a bit!

Yesterday I had the app working - it was copying the correct fields but for all entries in the source list.

Today, I've just finished the random element of it, and it's now only selecting a percentage of rows but copying the text "First Name", "Surname" and "Team" rather than the correct data for each entry! Code below:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare sites
            SPSite sourceSite = new SPSite(@"http://sharepoint");

            //Declare webs
            SPWeb sourceWeb = sourceSite.RootWeb;

            //Declare lists
            SPList sourceList = sourceWeb.GetList("/Lists/Staff Contacts");

            try
            {
                double arrayNum = ((double)sourceList.ItemCount) * 0.05;

                int[] rows = new int[(int)arrayNum];
                Random rnd = new Random();

                for (int i = 0; i < (int)arrayNum; i++)
                {
                    rows[i] = rnd.Next(1, sourceList.ItemCount - 1);
                }

                foreach (int i in rows)
                {
                    String var1 = sourceList.Items[i].Fields["First Name"].ToString();
                    String var2 = sourceList.Items[i].Fields["Last Name"].ToString();
                    String var3 = var1 + " " + var2;
                    String var4 = sourceList.Items[i].Fields["Team"].ToString();

                    CopyItem(var3, var4);
                }

                sourceWeb.Dispose();
                sourceSite.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        static void CopyItem(String a, String b)
        {
            //Declare sites
            SPSite destSite = new SPSite(@"http://sharepoint/communications");

            //Declare webs
            SPWeb destWeb = destSite.OpenWeb();

            //Declare lists
            SPList destList = destWeb.Lists["Staff Satisfaction"];

            SPListItemCollection listItems = destList.Items;

            SPListItem item = listItems.Add();

            item["Staff Member"] = a;
            item["Team"] = b;

            item.Update();

            destWeb.Dispose();
            destSite.Dispose();
        }

        
    }
}

Any ideas? I must have missed something somewhere! (My code for the CopyItems method is include at the bottom)

Cheers!
 
Got it working - it wasn't setting the values properly before passing them to CopyItem.

Had to change it so that it set the ListItem first e.g.
Code:
                    SPListItem item = sourceList.Items[i];
                    String var1 = item["First Name"].ToString();
                    String var2 = item["Last Name"].ToString();
                    String var3 = var1 + " " + var2;
                    String var4 = item["Team"].ToString();

Not sure why that works and the other way didn't but...

OK, so how would I "use" this now? Do I just copy it to the server and then call the .exe in a scheduled task? Or do I need to do something now this is the final version?

Thanks for all your help!
 
I'll look in to doing that. I've ran in to a problem on my first run though!

Somehow (the odds must be quite high!) it's managed to generate the same random number twice, and therefore in my first batch of 10 or so it's created, I have the same person listed twice!

How would I make sure that the random numbers added to the array aren't the same?
 
Back
Top Bottom