Copy data between lists in Sharepoint

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!
 
Glad you are getting somewhere. What you do if your stuck is to try and get it working for only one row at a time. That will you will be able to call it from a batch file and pass the params in using the args[] input values.

Stick up your code and I'll have a look. Where is VinceB1 when you need him!
 
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!
 
There must be something wrong with the path that you are providing to the destination list. Does the line where you declare destWeb work ok?

Maybe something is mucking around considering you are trying to work with two separate locations at one time. I'm not sure if there are any issues around this.
 
Are the items in the same subsite? If so then try...

Code:
SPList destList = sourceWeb.GetList("/Lists/Staff Satisfaction");

Edit: no it looks from your urls that they are in different webs.

Edit 2: no try this...

Code:
SPList destList = destWeb.GetList("/COMMUNICATIONS/Lists/Staff Satisfaction");
//You might need to add the subsite name in here
 
Last edited:
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!
 
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!

Did you try the update that I suggested in the last post?
 
Try separating it out into it's own method and then init your objects within. Are you debugging on the server? If so can you do a watch on the destList object and filter down through the properties to make sure you can see the list and the items?
 
Hold on here. I think I'm being stupid.

You aren't actually using destList anywhere. If the items are being written to console then surely it is working correctly?!

Edit: stick up the code you are trying to run without anything commented out! I'm confused now.
 
Got it working! Used:

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

Not sure why that made any difference....now on to the actual application!
 
Ah good stuff. Maybe it is because with the sourcesite you had called rootweb which is different from doing openweb on a subsite.

I think the confusion was with something that you had commented out and so wasn't in your original code posted ^^.
 
Now what I'd do is have some method to do the copy to the other list i.e.

Code:
private void CopyItem(a, b, c) {
   //open list
   //create a new SPListItem
   //assign values to the SPListItem in the desired list
}

You could call the method by doing something like:
Code:
foreach (SPField field in sourceList.Fields)
                {
                    ...
                    //some logic to work out if the field is desired or not
                    if one of the 3 fields then...
                    add value to list                  
                }
               when finished iteration then add 3 values in the list to new list using
             CopyItem(value1, value2, value3);

...bla bla

Another thing to note is to make sure and Dispose of any SPWeb/SPSite objects e.g.

sourceSite.Dispose();
destSite.Dispose();
 
Last edited:
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.
 
Probably best to try and get this working for just one row before trying the random thing.

Could you try something like row.Items[1].Field["FieldName"].Value?

I have no idea if that will return anything - it's just a guess!
 
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!
 
I have loaded my VM but something has gone pear shaped with tons of SQL errors appearing all over the place. I think an upgrade to SQL 2008 R2 has broken something.

Code:
SPListItemCollection spCol = sourceWeb.Lists["Tasks"].Items;

This will give you all the items in a particular list so you can do what you want with the contents. If you drill down into spCol.Fields or whatever then you should be able to see what is going on.
 
Back
Top Bottom