How can I ensure my programmatic uploads are done in the correct order?
Posted
by
ccomet
on Stack Overflow
See other posts from Stack Overflow
or by ccomet
Published on 2010-08-11T20:00:18Z
Indexed on
2010/12/22
2:54 UTC
Read the original article
Hit count: 396
In our application, we store two copies of a file - an approved one and an unapproved one. Both track their versions separately. When the unapproved is then approved, all of its versions are added as new versions to the approved file. To do this properly, my code has to upload each version separately into the approved folder, and update the item each time with that version's information.
For some reason, though, this doesn't always work properly. In my latest scenario, the latest version was uploaded first, and then all of the remaining versions were uploaded afterwards. However, my code explicitly is supposed to upload the other versions first, that's the order I wrote it in. Why is this happening? And if it is possible, how do I ensure that the versions are uploaded in the correct order?
Clarification - It's not a problem with the enumeration - I'm getting the previous versions in the correct order. What is happening is that the final version, which is written after the loop, is being uploaded before the loop. Which really doesn't make any sense to me.
Here's a condensed version of the relevant code.
//These three are initialized earlier in the code.
SPList list; //The document library
SPListItem item; //The list item in the Unapproved folder
int AID; //The item id of the corresponding item in the Approved folder.
byte[] contents; //Not initialized.
/* These uploads are happening second when they should happen first. */
if (item.File.Versions.Count > 0)
{
//This loop is actually a separate method call if that matters.
//For simplicity I expanded it here.
foreach (SPFileVersion fVer in item.File.Versions)
{
if (!fVer.IsCurrentVersion)
{
contents = fVer.OpenBinary();
SPFile fSub = aFolder.Files.Add(fVer.File.Name, contents, u1, fVer.CreatedBy, dt1, fVer.Created);
SPListItem subItem = list.GetItemById(AID);
//This method updates the newly uploaded version with the field data of that version.
UpdateFields(item.Versions.GetVersionFromLabel(fVer.VersionLabel), subItem);
}
}
}
/* This upload happens first when it should happen last. */
//Does the same as earlier loop, but for the final version.
contents = item.File.OpenBinary();
SPFile f = aFolder.Files.Add(item.File.Name, contents, u1, u2, dt1, dt2);
SPListItem finalItem = list.GetItemById(AID);
UpdateFields(item.Versions[0], finalItem);
item.Delete();
© Stack Overflow or respective owner