public
static
string GetUrlForVersion(
this SPListItem item,
string versionLabel,
int? docStoreVersion =
null)
{
string relatedDocumentVersionUrl =
string.Empty;
// This check is done in SPFile.GetFileVersion (so we could only perform it when we know we don't have a file)
// but we do it here first to save effort in the case that we aren't working with a file (to save that more expensive check).
if (
string.IsNullOrEmpty(versionLabel))
{
relatedDocumentVersionUrl = item.Url;
}
else
{
SPFile file = item.File;
if (file !=
null)
{
SPFileVersion fileVersion;
bool refersToLatestVersion = file.GetFileVersion(versionLabel, out fileVersion, docStoreVersion);
if (refersToLatestVersion)
{
relatedDocumentVersionUrl = item.Url;
}
else
if (fileVersion !=
null)
{
relatedDocumentVersionUrl = fileVersion.Url;
}
}
if (
string.IsNullOrEmpty(relatedDocumentVersionUrl))
{
var itemVersion = item.Versions.GetVersionFromLabel(versionLabel);
if (itemVersion !=
null)
{
relatedDocumentVersionUrl = itemVersion.Url;
}
}
if (
string.IsNullOrEmpty(relatedDocumentVersionUrl))
{
throw
new SPException(
string.Format(
"The '{0}' version of document '{1}' cannot be found. It may have been deleted.", versionLabel, item.Name));
}
}
return relatedDocumentVersionUrl;
}
public
static Stream OpenBinaryStreamForVersion(
this SPFile file,
string versionLabel,
int? docStoreVersion)
{
Stream bStream =
null;
SPFileVersion fileVersion;
bool refersToLatestVersion = file.GetFileVersion(versionLabel, out fileVersion, docStoreVersion);
if (refersToLatestVersion)
{
bStream = file.OpenBinaryStream();
}
else
if (fileVersion !=
null)
{
bStream = fileVersion.OpenBinaryStream();
}
return bStream;
}
public
static
int? GetDocStoreVersion(
this SPListItem item)
{
return GetDocStoreVersion(item.File);
}
public
static
int? GetDocStoreVersion(
this SPFile file)
{
int? docStoreVersion =
null;
if (file !=
null)
{
docStoreVersion = (
int)file.Properties[Constants.Properties.VTI_DOCSTOREVERSION];
}
return docStoreVersion;
}
public
static
int? GetDocStoreVersion(
this SPFileVersion fileVersion)
{
int? docStoreVersion =
null;
if (fileVersion !=
null)
{
docStoreVersion = (
int)fileVersion.Properties[Constants.Properties.VTI_DOCSTOREVERSION];
}
return docStoreVersion;
}
/// If the fileversion with the provided versionLabel has been promoted via publishing, then we will return the
/// fileversion that has been published (that will not have a mathing versionLabel).
/// NOTE: This can never return the current version as it is not in the SPFile.Versions collection.
/// versionLabel: The version of the document to get
/// docStoreVersion: If provided, will get the version using this value (this doesn't change when a version is published), else will attempt to figure it out.</param>
/// returns: If the file version is the latest version (need to access the SPFile directly)
public
static
bool GetFileVersion(
this SPFile file,
string versionLabel, out SPFileVersion fileVersion,
int? docStoreVersion =
null)
{
bool refersToLatestVersion =
false;
fileVersion =
null;
int docStoreVersionLatest = file.GetDocStoreVersion().Value;
if (docStoreVersion.HasValue && docStoreVersion.Value == docStoreVersionLatest)
{
// The doc store version matches the current version
refersToLatestVersion =
true;
}
else
if (
string.IsNullOrEmpty(versionLabel) || versionLabel == file.UIVersionLabel)
{
// The version we are looking for matches the current version
refersToLatestVersion =
true;
}
else
if (docStoreVersionLatest <
3)
{
// The first doc store version number is 2, therefore it is the only version that there has been
refersToLatestVersion =
true;
}
else
if (docStoreVersion.HasValue)
{
// If a doc store version has been provided we can find it in the previous versions
fileVersion = file.Versions.Cast<SPFileVersion>().FirstOrDefault(v => v.GetDocStoreVersion() == docStoreVersion.Value);
}
else
{
// Get previous version by version label
fileVersion = file.Versions.GetVersionFromLabel(versionLabel);
if (fileVersion ==
null)
{
// We don't have the docstore version to find but if the previous verions docstore version minus the next version docstor version = 1, then it has been published.
// Note that this only covers the case where the version gets published, when multiple versions have been deleted this may fail to return a version.
// ... I have not included this code for brevity ...
}
}
if (!refersToLatestVersion && fileVersion ==
null)
{
string errMsg =
"Failed to locate specific file version, URL: {0}, VersionLabel: {1}, DocStoreVersion: {2}";
string docStoreVersionString = docStoreVersion.HasValue ? docStoreVersion.Value.ToString() :
"Unspecified";
Log.WriteLog(
string.Format(errMsg, file.ServerRelativeUrl, versionLabel, docStoreVersionString), TraceSeverity.High, DiagnosticServiceLogger.LogCategories.GroupsAndProjects);
}
return refersToLatestVersion;
}