SPFileVersion usage


123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
         
         
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值