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?
 
Are you familiar with the SharePoint object model? It's best using it as opposed to doing stuff directly in the DB. Apparently you can induce db locks if you aren't careful.

I'd recommend writing something in Visual Studio that uses the SP Object Model to do it. I'd say you could use an SPQuery object with some CAML to pull back what you want.

VinceB1 is your man for SP programming. I have done bits and pieces but not on a huge scale.
 
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
 
Normally I just write a couple of methods in a console application and then call it from a batch file where i can pass in parameters etc.

If you want something to live within SharePoint you could do a webpart or something. There is a timer service in SharePoint which you could probably hook into to schedule this automatically.

Another option may be to try SharePoint designer and build a custom workflow that will do what you need to do. I don't think there is any easy way without much effort.
 
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!
 
Can you do something like...

Code:
foreach (SPField field in sourceList.Fields)
   {
      ...
      //when here you can browse the field value and its properties
      //e.g. field.Title will get the title value
      ....
   }

Think of field as an SPField object which has methods and properties. You can get all the info on SPField here

Edit: maybe you aren't asking that. Do you mean how can you pick random fields?

I'd do a count on the number of items in your list, generate a number of random numbers within that range, check if a list item exists for that value, and then copy the values into a list or something similar. Then open new list and write values from previous list. You with me?
 
Last edited:
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!
 
OK so this would be my logic...

-Initiate SP object for source list
-Count number of rows in list
-Generate how many random numbers you need and store they in some form of array
-Iterate through array for all the selected random rows copying out the values.

At this point you should have several items storing the 3 properties you need.

-Then open new SP object and your target list
-Iterate through your values from previous section and then write these to the new list

It's a bit too early in the morning for me but hopefully that makes sense!

Edit: what version of SharePoint is this?
 
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?
 
Are you trying to run this on the web front end? You can't run it from a dev box. In SP2010 you can do this remotely as far as I know.

When you build the project the exe should be build which will contain the SP dll as part of it.

And yes, an array of random source item numbers should do the trick.
 
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.
 
You will need to build the app on a development box, compile into a dll, copy the dll onto the server and then perhaps execute it from cmdline or batch file.

If you can install VS onto the SharePoint box then that would be useful as you can see the SP objects and their properties while debugging.
 
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!
 
Are you getting any errors in the event log? If you enclose your code within try catch blocks then write out the exception to the console.

Edit: I think you need to be logged in as system account in order to guarantee that this will work. In your code you can use SPSecurity.RunWithElevatedPrivileges to ensure code runs for people with lower permissions set.
 
Last edited:
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.
 
Back
Top Bottom