Copy data between lists in Sharepoint

How would I 'drill down'? I'm trying to loop through each field and output the value to a console line, but can't get it to work...

Code:

Code:
SPListItemCollection items = sourceList.Items;

                foreach (SPItem item in items)
                {
                    Console.WriteLine(item.Fields["Title"].GetFieldValueAsText);
                }
 
How would I 'drill down'? I'm trying to loop through each field and output the value to a console line, but can't get it to work...

Code:

Code:
SPListItemCollection items = sourceList.Items;

                foreach (SPItem item in items)
                {
                    Console.WriteLine(item.Fields["Title"].GetFieldValueAsText);
                }

Console.WriteLine(item.Fields["Title"]) should do it?
 
Try this...

Code:
            try
            {
                //Declare sites
                SPSite sourceSite = new SPSite(@"http://moss2007");

                //Declare webs
                SPWeb sourceWeb = sourceSite.RootWeb;

                //Declare list
                SPList tasks = sourceWeb.Lists["Tasks"];

                foreach (SPListItem task in tasks.Items)
                {
                    String var1 = task["Title"].ToString();
                    String var2 = task["Priority"].ToString();
                    String var3 = task["Status"].ToString();

                    CopyItem(var1, var2, var3);
                }

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

This code will select 3 properties from a list and then the values are used as parameters for your new list. You will need to tweak the loop to only do the random items that you require.
 
Strange, that builds now without error, but when I run it on the server I get an error saying that the value doesn't fall within the expected range??
 
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!
 
Not a bother. I'm not sure how you go about adding a new item to a new list. Maybe you can pass all the variables into a create method or perhaps you create a new SPListItem and then assign the values, the call list.Update.

If you get something working then post back the code. Make sure to dispose SPSite and SPWeb objects. You can enclose them in using statements which disposes automatically.

Good luck.
 
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?
 
Very good. Do you mind posting the code for the copy bit? I'm guessing you are just generating a new SPItem and then updating?

What I would do is exactly as you had suggested. Generate 5 unique numbers between 0 and the number of items in your list -1. This is so that you won't get any index overflow errors.

Then I'd iterate through each value in array and pass that value to the item index i.e.

Code:
//Declare list
SPList tasks = sourceWeb.Lists["Tasks"];

foreach (int i in MyArrayList)
{
     String var1 = tasks.Items[i].Fields["Title"].ToString();
     String var2 = tasks.Items[i].Fields["Priority"].ToString();
     String var3 = tasks.Items[i].Fields["Status"].ToString();

     CopyItem(var1, var2, var3);
}
 
Last edited:
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!
 
Great stuff. Glad you got sorted. I normally upload these sorts of scripts to a folder on the WFE server and then just call them using a batch file. You could add some logging so that it will write any errors to the event log so that you can confirm whether it has done what it is meant to do.
 
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?
 
In your loop you will have to check the number hasn't been used already. Not sure how best to do that.

It's probably best to create a load of numbers, shuffle them and then pick the first 5 or something. I haven't a clue!
 
Back
Top Bottom