Created
September 23, 2014 10:30
-
-
Save sihugh/636aad61d22ebe094d89 to your computer and use it in GitHub Desktop.
A utility class for helping determine if a SharePoint item update event is a result of a publish, approve or just an update.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Use in "Updating" event handler to determine if an update is a publish event. | |
/// </summary> | |
public class ItemVersionReader | |
{ | |
public Version BeforeVersion { get; private set; } | |
public int PublishingLevel { get; private set; } | |
public int ModerationStatus { get; private set; } | |
public bool IsPublishing | |
{ | |
get { return PublishingLevel == 2 && ModerationStatus == 0; } | |
} | |
public ItemVersionReader(SPItemEventProperties properties) | |
{ | |
BeforeVersion = GetBeforeVersion(properties); | |
PublishingLevel = GetAfterLevel(properties); | |
ModerationStatus = GetAfterModerationStatus(properties); | |
} | |
private static Version GetBeforeVersion(SPItemEventProperties properties) | |
{ | |
//Ends up something like "V1.1" | |
string beforeVersion = GetPropertyValue(properties.BeforeProperties, "vti_sourcecontrolversion"); | |
//Not used at present, but helpful for debugging | |
string afterVersion = GetPropertyValue(properties.AfterProperties, "vti_sourcecontrolversion"); | |
return ParseVersionString(beforeVersion, properties.ListItem.Url); | |
} | |
private static Version ParseVersionString(string version, string itemUrl) | |
{ | |
var majorMinor = new string[0]; | |
if (version.StartsWith("V", StringComparison.OrdinalIgnoreCase)) | |
{ | |
version = version.Substring(1); | |
majorMinor = version.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries); | |
} | |
else | |
{ | |
Debug.WriteLine("The before version of the file at " + itemUrl + | |
" is of an unknown format: " + version); | |
} | |
if (majorMinor.Length == 2) | |
{ | |
int majorBeforeVersion, minorBeforeVersion; | |
if (int.TryParse(majorMinor[0], out majorBeforeVersion) | |
&& int.TryParse(majorMinor[1], out minorBeforeVersion)) | |
{ | |
return new Version(majorBeforeVersion, minorBeforeVersion); | |
} | |
} | |
return null; | |
} | |
private static int GetAfterLevel(SPItemEventProperties properties) | |
{ | |
//Should be 1 or 2 or 255 similar | |
// | |
// beforeLevel afterLevel Meaning | |
// 255 255 minor checkin or overwrite of minor version | |
// 255 2 major checkin | |
// 2 2 publish / approve | |
//Not used at present, but helpful for debugging | |
string beforeLevel = GetPropertyValue(properties.BeforeProperties, "vti_level"); | |
string afterLevel = GetPropertyValue(properties.AfterProperties, "vti_level"); | |
int level; | |
if (!int.TryParse(afterLevel, out level)) | |
{ | |
level = -1; | |
} | |
return level; | |
} | |
private int GetAfterModerationStatus(SPItemEventProperties properties) | |
{ | |
//Should be 0 (Approved), 1 (Rejected), 2 (Pending) or 3 (Cancelled) | |
string afterModerationStatus = GetPropertyValue(properties.AfterProperties, "vti_doclibmodstat"); | |
int moderationStatus; | |
if (!int.TryParse(afterModerationStatus, out moderationStatus)) | |
{ | |
moderationStatus = -1; | |
} | |
return moderationStatus; | |
} | |
private static string GetPropertyValue(SPItemEventDataCollection props, string propertyName) | |
{ | |
object property = props[propertyName]; | |
if (property != null) | |
{ | |
string propertyValue = property.ToString(); | |
Debug.WriteLine(propertyValue); | |
return propertyValue; | |
} | |
else | |
{ | |
Debug.WriteLine(propertyName + " is null"); | |
return ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment