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