Skip to content

Instantly share code, notes, and snippets.

@vaderj
Last active June 11, 2018 20:36
Show Gist options
  • Save vaderj/e2d05406dfa6022c1072 to your computer and use it in GitHub Desktop.
Save vaderj/e2d05406dfa6022c1072 to your computer and use it in GitHub Desktop.
(Not working) Copy file with versions & metadata #c# #SharePoint
static void SCRAPcopySomeFilesWithVersions(SPListItem srcItem, SPList destList)
{
SPFile oFileSrc = srcItem.File;
//Console.WriteLine(oFileSrc.Title.ToString());
byte[] srcByteArray = null;
SPFile SPFileDestination = null;
SPFileCollection collFilesDest = destList.RootFolder.Files;
SPTimeZone timezone = destList.ParentWeb.RegionalSettings.TimeZone;
// hashtable of properties is required as its
System.Collections.Hashtable properties = new System.Collections.Hashtable();
// These are the Managed Metadata columns which need special handleing to populate
List<string> TaxonomyColumnTitles = new List<string>();
TaxonomyColumnTitles.Add("Department-Team");
TaxonomyColumnTitles.Add("Document Category");
TaxonomyColumnTitles.Add("Intended Audience");
TaxonomyColumnTitles.Add("Keywords");
Console.WriteLine("Verion count: " + srcItem.Versions.Count); //diagnostic purposes - remove after things work
try
{
foreach (SPFileVersion version in srcItem.File.Versions)
{
//SPListItemVersion version = srcItem.Versions[i];
SPListItem oListItemDest = destList.Items.Add();
foreach (SPField field in srcItem.Fields)
{
// ASL: 1/12/2015
// Thank you to the autor of https://social.msdn.microsoft.com/Forums/sharepoint/en-US/91197d0f-9dd4-4025-9275-975c31f59c0f/copy-a-list-item-from-one-custom-list-to-another
// for help identifying the fields that were causing me a great deal of frustration.
SPField created = oListItemDest.Fields["Created"];
SPField author = oListItemDest.Fields["Created By"];
created.ReadOnlyField = false;
author.ReadOnlyField = false;
if (
field.ReadOnlyField == false &&
field.InternalName != "Attachments")
{
if (field.InternalName == "TaxKeywordTaxHTField" || field.InternalName == "ab87212ecb31457da49f0c3c15c1c18a" || field.InternalName == "ofb6abdf9fc642c5b0e1b6f524d21bfd" || field.InternalName == "oc39bf9fa8d0498fb775dfa8c14b8879" || field.Type == SPFieldType.Invalid)
{
// The first condition is for the columns that only allow a single value, whereas the else condition is when the field supports multiple Managed Metadata values
foreach (string columnName in TaxonomyColumnTitles)
{
if (columnName == "Department-Team" || columnName == "Document Category")
{
var sourceValue = srcItem[columnName] as TaxonomyFieldValue;
var targetField = SPFileDestination.Item.Fields[columnName] as TaxonomyField;
targetField.SetFieldValue(SPFileDestination.Item, sourceValue);
}
else if (columnName == "Intended Audience" || columnName == "Keywords")
{
var sourceValue = srcItem[columnName] as TaxonomyFieldValueCollection;
var targetField = SPFileDestination.Item.Fields[columnName] as TaxonomyField;
targetField.SetFieldValue(SPFileDestination.Item, sourceValue);
}
}
}
else
{
SPFileDestination.Item[field.InternalName] = srcItem[field.InternalName];
//properties.Add(version[field.InternalName].ToString(),version[field.InternalName]);
}
}
}
string fileFolderBuilder = srcItem.File.ServerRelativeUrl.Replace("/resources/", "");
srcByteArray = version.OpenBinary();
//SPUser spFileAuthor = destList.ParentWeb.EnsureUser(oFileSrc.Author.LoginName);
//SPUser spFileModifiedby = destList.ParentWeb.EnsureUser(oFileSrc.ModifiedBy.LoginName);
//SPFile oFileDest = collFilesDest.Add(fileFolderBuilder, srcByteArray, properties, spFileAuthor, spFileModifiedby, oFileSrc.TimeCreated, oFileSrc.TimeLastModified, true);
SPUser spFileAuthor = destList.ParentWeb.EnsureUser(version.CreatedBy.LoginName);
SPUser spFileModifiedby = destList.ParentWeb.EnsureUser(version.CreatedBy.LoginName);
SPFileDestination = collFilesDest.Add(fileFolderBuilder, srcByteArray, properties, spFileAuthor, spFileModifiedby, version.Created, oFileSrc.TimeLastModified, true);
// * oListItemDest["Title"] = srcItem["Title"];
// * oListItemDest["Comments"] = srcItem["Comments"];
// * oFileDest.Item["Created"] = oFileSrc.Item["Created"];
// * oFileDest.Item["Modified"] = oFileSrc.Item["Modified"];
RestoreProperties(version.Properties, SPFileDestination.Properties);
//oListItemDest = SPFileDestination.Item;
//oFileDest.Item.UpdateOverwriteVersion();
//oListItemDest["Created"] = oFileDest.TimeCreated;
//oListItemDest["Created"] = srcItem["Created"];
//oListItemDest["Title"] = srcItem["Title"];
SPFileDestination.Item.UpdateOverwriteVersion();
}
}
catch (Exception e)
{
Console.WriteLine("An error occurred: '{0}'", e);
//ErrorList.Add(e.ToString());
//Console.WriteLine("An email was sent with the error that occured");
}
} // Was not working correctly, have not deleted yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment