In a event receiver depending on some business rules, I need to copy the Document Set CONTENTS to another existing document set. However I cant find how to do it. I have the code that gets the source document set and the target document set. However I dont know how to copy the files.
And I can not use the DocumentSet.CopyTo, because the document set already exists, and it will overwrite the metadata on the existing document set.
private void CopyAgendaPointAttachmentsToRootSite(SPListItem agendaPointItem)
{
try
{
if (agendaPointItem != null)
{
SPWeb currentSite = agendaPointItem.ParentList.ParentWeb;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(currentSite.Site.RootWeb.Url))
{
using (SPWeb elevatedTargetWeb = site.OpenWeb())
{
SPList targetList = GetAgendaPointProposedTargetLibrary(agendaPointItem, elevatedTargetWeb);
DocumentSet targetDocumentSet = DocumentSet.GetDocumentSet(targetList.RootFolder);
SPDocumentLibrary targetDocumentLibrary = (SPDocumentLibrary)targetList;
SPContentTypeId targetCTId = targetList.ContentTypes.BestMatch(new SPContentTypeId(MeetingsCommon.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_ID));
DocumentSet documentSet = DocumentSet.GetDocumentSet(agendaPointItem.Folder);
if (documentSet != null)
{
foreach (SPFile file in documentSet.Folder.Files)
{
string copyToUrl = string.Concat(elevatedTargetWeb.Url, "/", targetList.RootFolder.Url, "/", agendaPointItem.Name, "/", file.Name);
}
}
}
}
});
}
}
catch (Exception ex)
{
Logger.LogError("AgendaPointsEvents", "CopyAgendaPointAttachmentsToRootSite(SPListItem agendapointItem)", ex);
throw;
}
}
Update:1 The exception thrown is Value does not fall within the expected range in the Copy Line, I checked with the debugger the url and its fine, because I can just copy and paste it in the browser and it works
Update 2:
Both DocumentSets are in different SPWeb objects