Copy data between lists in Sharepoint

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
What's the best way to copy data between sharepoint lists? I have a list which I want to copy a random percentage of records from every month to another list.

I've written an SQL script to do this within the database (I'm struggling to calculate tp_Size though) but have read that this isn't best practice?

How should I go about doing this? I need to copy 2 fields for around 10 - 20 rows each month, so not a massive amount of data.

Thanks,
 
Same site. Baiscally I've got a list of contacts with various bits of info about them, and I want to copy certain bits of information into another list, for a random 5 percent of the contacts.

Is this possible?
 
I've heard of it (I'd seen that it was a better way to do what I'm trying to do, rather than SQL), but I can't say I'm familiar with it.

Would I be writing a sort of stand alone app which I schedule, or does it plug in to Sharepoint somehow?

VinceB1 rings a bell, I think he's helped me before :D
 
Right, I'm stuck already!

Following your link, I've referenced my sharepoint site and my source & destination lists.

How do I specify which fields from sourceList I want to copy, and that I only want to copy a random selection?

Your link seems to just loop through all fields?

Thanks!
 
Yeah I think you understand me, sorry if I'm being a bit vague!

My sourceList has say 20 fields, but I only need to copy the contents of 3 of them. It's always the same 3, this isn't the random part!

Then I need to just copy a random percentage of rows for the 3 fields above.

It's basically a list of staff which I need to choose a random percentage of to survey. The staff list contains all sorts of rubbish I don't need, just the staff member's name (2 fields) and team.

Thanks for sticking with me!
 
So I'd have an array of random row numbers which I'd use as my source items? That sounds like it should work.

It's WSS 3.0, I've just tried to run my app and I get an error saying that my Microsoft.Sharepoint reference cannot be found. I'd initially referenced the remote copy of this (I'm developing on a different machine than the server Sharepoint is installed on) but I've tried creating a local copy and it say's it cannot find that either.

Any idea where I'm going wrong?
 
So I'd need to install VS on the Sharepoint server itself?

When I said run in my previous post, I meant Debug. That's where I get the reference not found error.
 
I've built my application and then copied it from my local project folder to the sharepoint server (is this right?)

It then seems to run (I don't get the reference error anymore) but it doesn't seem to do what I've told it to (I'm just looping through each field and writing the title to the console).

Sorry for the constant questions!
 
Event viewer shows a filenotfound exception.

I'm logged in as Domain Admin when I run this, so don't think it's a permissions issue.

I'll try writing the exception to the console.
 
Right, the exception led me to a problem with my destination list which I'm not using yet (just trying to get it to select the right data to copy) so I commented that code out and it ran fine, writing the title of each field to a line in the console.

Not really sure what the problem is with my destination list (it's on a sub site) but that's definitely causing the problem.

At least it's running though!
 
Code:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Declare sites
                SPSite sourceSite = new SPSite(@"http://sharepoint");
                SPSite destSite = new SPSite(@"http://sharepoint/communications");

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

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

                foreach (SPField field in sourceList.Fields)
                {
                    Console.WriteLine(field.Title);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

It's falling over at line 25 (declaring destList) saying that it can't find the file specified!
 
Seems to be. I've just written every variable to a console line and they all have the right values until it falls over at the destList stage, but that's definitely the right list.

Not sure where to go from here!
 
Got it working! Used:

Code:
SPList destList = destWeb.Lists["Staff Satisfaction"];

Not sure why that made any difference....now on to the actual application!
 
OK, I've got my CopyItems method and now I'm stuck on trying to get it to return a random selection of rows. I have this so far:

Code:
SPQuery query = new SPQuery();
                query.RowLimit = 10;
                Random rnd = new Random();
                IOrderedEnumerable<SPListItem> Items = sourceList.GetItems(query)
                                                                .Cast(SPListItem)
                                                                .AsEnumerable()
                                                                .OrderBy((i => rnd.Next()));

But I'm getting an error on build for the .Cast part. This site I took this from had angled parenthesis around SPListItem as well as normal parenthesis, but this also produces an error.

Any ideas?
 
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
            {
                SPQuery query = new SPQuery();
                query.RowLimit = 10;
                Random rnd = new Random();
                IOrderedEnumerable<SPListItem> Items = sourceList.GetItems(query)
                                                                .Cast(SPListItem)
                                                                .AsEnumerable()
                                                                .OrderBy((i => rnd.Next()));
                foreach (SPListItem item in Items)
                {
                    Console.WriteLine(item[SPBuiltInFieldId.Title].ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        private void CopyItem(String a, String b, String c)
        {
            //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["Title"] = a;
            item["Title"] = b;
            item["Title"] = c;

            item.Update();
        }

        
    }
}

Probably massively inefficient so let me know if you spot anything I could tweak!

I know I'm setting the title field to three different variables in my CopyItem method, but that's because I haven't got the names of the fields yet - I'll change this later!

I've taken the random rows code from here.

Thanks again.
 
I'll have a go at getting the info from one field as you suggest.

Thanks for this, no rush for me, it's more of a learning thing that I could actually use in the end.

Thanks for your help so far!
 
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);
                }
 
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??
 
Back
Top Bottom