1

I need to copy SharePoint List items from one SharePoint 2007 farm to another SharePoint 2007 farm.

The following code works fine if the sites or site collection are in the same SharePoint farm.

SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                using (SPSite site = new SPSite(destServerURL))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        SPList destinationList = web.Lists[destinationListName];

                        SPListItem targetItem = destinationList.Items.Add();

                        foreach (SPField f in sourceItem.Fields)
                        {
                            //Copy all except attachments.

                            try
                            {

                                if (!f.ReadOnlyField && f.InternalName != "Attachments" && f.InternalName != "Created" && f.InternalName != "Modified" && f.InternalName != "Content")
                                {
                                    targetItem[f.InternalName] = sourceItem[f.InternalName];
                                    targetItem.Update();
                                }
                            }
                            catch (Exception ex)
                            {
                                //TODO Add handling
                                Helper.LogtoList("Error Message :", string.Format(" {0} Stack Trace: {1} Inner Exception:--{2} Source:-- {3}", ex.Message, ex.StackTrace, ex.InnerException, ex.Source));
                            }
                        }
                        for (int i = sourceItem.Versions.Count - 1; i >= 0; i--)
                        {
                            //set the values into the archive 
                            foreach (SPField sourceField in sourceItem.Fields)
                            {
                                SPListItemVersion version = sourceItem.Versions[i];

                                if ((!sourceField.ReadOnlyField) && (sourceField.Type != SPFieldType.Attachments))
                                {

                                        targetItem[sourceField.Title] = version[sourceField.Title];
                                }
                                else if (sourceField.Title == "Created" && sourceField.Title == "Created By" && sourceField.Title == "Modified" && sourceField.Title == "Modified By")
                                {

                                    targetItem[sourceField.Title] = version[sourceField.Title];
                                }
                            }

                            //update the archive item and 
                            //loop over the the next version
                            targetItem.Update();
                            // Helper.LogtoList("Target Item Title:", String.Format("  {0} Source Item Count {1}", targetItem.Title.ToString(), sourceItem.Versions.Count));
                        }

                        //now get the attachments, they are not versioned
                        foreach (string attachmentName in sourceItem.Attachments)
                        {
                            SPFile file = sourceItem.ParentList.ParentWeb.GetFile(
                            sourceItem.Attachments.UrlPrefix + attachmentName);
                            targetItem.Attachments.Add(attachmentName, file.OpenBinary());
                            // Helper.LogtoList("Target Item attachment count", String.Format("Target Item attachment count {1} Attachment Name:  {0}", attachmentName, targetItem.Attachments.Count.ToString()));
                        }

                        targetItem.Update();
                        Helper helper = new Helper();
                        helper.ItemsCopied++;
                        // sourceItem.Delete();
                    }
                }
            });

Environment

a) Two SharePoint 2007 farm in the same domain

b) Each SharePoint farm has a front end server and a individual backup server.

Error I get the following error when i try to copy the SharePoint item to another farm. The web application at [URL] could not be found. Verify that you have typed the url correctly

Steps till now

a) Changed project type to X64 but the same error persists

  • SSOM requires that the objects (SPWeb, SPList) be present on the farm. This solution won't work for copying items to a remote farm, instead you'll need to look into content migration. This can be accomplished in various ways, or using a 3rd party tool. – Trevor Seward May 16 '16 at 16:49
1

As Trevor has suggested in comments you can't use SSOM code to copy list items from one farm to other farm.

Other alternative for SharePoint 2007 are:

1) Lists Web Service (http:///_vti_bin/Lists.asmx):

You can use List Web Service to copy items. The Lists Web service provides methods for working with SharePoint lists, content types, list items, and files. There are following methods that can be utilized with the Lists web service:

  1. AddAttachment
  2. AddDiscussionBoardItem
  3. AddList AddListFromFeature
  4. ApplyContentTypeToList
  5. CheckInFile
  6. CheckOutFile
  7. CreateContentType
  8. DeleteAttachment
  9. DeleteContentType
  10. DeleteContentTypeXmlDocument
  11. DeleteList
  12. GetAttachmentCollection
  13. GetList
  14. GetListAndView
  15. GetListCollection
  16. GetListContentType
  17. GetListContentTypes
  18. GetListItemChanges
  19. GetListItemChangesSinceToken
  20. GetListItems
  21. GetVersionCollection
  22. UndoCheckOut
  23. UpdateContentType
  24. UpdateContentTypeXmlDocument UpdateList UpdateListItems

For reference: http://geekswithblogs.net/mcassell/archive/2007/08/22/Accessing-Sharepoint-Data-through-Web-Services.aspx

https://msdn.microsoft.com/en-us/library/lists.lists.getlistitems(v=office.12).aspx

2) Create list Template

  1. Save the source list as a template with content
  2. Then copy the .stp file from Source site to destination site
  3. Create a new list based on this template at destination site.

3) Export - Import using excel

You can copy a list from one SharePoint site to another by exporting the list to a Windows SharePoint Services-compatible spreadsheet program such as Microsoft Office Excel, and then importing the spreadsheet data into another list.

When you export an Excel table to a SharePoint site, each column in a SharePoint list is assigned one of the following data types:

  1. Text (single line)
  2. Text (multiple lines)
  3. Currency
  4. Date/time
  5. Number
  6. Hyperlink

Therefore, calculated fields will not be copy and so you have to manual create the calculated fields and insert the formula manually.

The importing spreadsheet data can be found inside the Site Actions > Create, go to the Custom Lists column and select the Import Spreadsheet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.