Skip to content

Instantly share code, notes, and snippets.

@alasvant
Created January 23, 2021 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alasvant/c446b68cda607ab734110af73aef317b to your computer and use it in GitHub Desktop.
Save alasvant/c446b68cda607ab734110af73aef317b to your computer and use it in GitHub Desktop.
Some sample helper extension methods to be used in custom Episerver import processing to get Episerver properties.
/// <summary>
/// Sets the content status to draft.
/// </summary>
/// <param name="rawProperties">Import raw properties</param>
/// <returns>True if the status was set to draft otherwise false.</returns>
public static bool SetAsDraft(this Dictionary<string, RawProperty> rawProperties)
{
bool isSet = false;
// TODO: should we add the properties if missing?
// should we remove the return value and just log a message?
if (rawProperties.HasEntries())
{
// set pending publish
if (rawProperties.ContainsKey(MetaDataProperties.PagePendingPublish))
{
rawProperties[MetaDataProperties.PagePendingPublish].Value = bool.TrueString;
isSet = true;
}
if (rawProperties.ContainsKey(MetaDataProperties.PageWorkStatus))
{
// value 2, CheckOut, The version is currently in progress
// valie 3, CheckedIn, A writer has checked in the version and waits for the version to be approved and published
// the import doesn't actually at the moment care about this value, only the PagePendingPublish
// set it CheckOut and if someday the import uses VersionStatus this should be changed to 3, so that the page is in Ready for publish status
rawProperties[MetaDataProperties.PageWorkStatus].Value = ((int)VersionStatus.CheckedOut).ToString(CultureInfo.InvariantCulture);
}
if (rawProperties.ContainsKey(MetaDataProperties.PageDeleted))
{
rawProperties[MetaDataProperties.PagePendingPublish].Value = bool.TrueString;
}
if (rawProperties.ContainsKey(MetaDataProperties.PageDeletedBy))
{
rawProperties[MetaDataProperties.PageDeletedBy].Value = null;
}
if (rawProperties.ContainsKey(MetaDataProperties.PageDeletedDate))
{
rawProperties[MetaDataProperties.PageDeletedDate].Value = null;
}
if (rawProperties.ContainsKey(MetaDataProperties.PageStartPublish))
{
rawProperties[MetaDataProperties.PageStartPublish].Value = null;
}
if (rawProperties.ContainsKey(MetaDataProperties.PageStopPublish))
{
rawProperties[MetaDataProperties.PageStopPublish].Value = null;
}
}
return isSet;
}
/// <summary>
/// Sets the content type id.
/// </summary>
/// <param name="rawProperties">Raw properties dictionary.</param>
/// <param name="guidString">Value for the content type id.</param>
/// <returns>True if the property was set otherwise false.</returns>
public static bool SetContentTypeId(this Dictionary<string, RawProperty> rawProperties, string guidString)
{
// we don't validate the "arguments", done by the SetRawPropertyValue method
return rawProperties.SetRawPropertyValue(MetaDataProperties.PageTypeID, guidString);
}
/// <summary>
/// Gets the content name value.
/// </summary>
/// <param name="rawProperties">Raw properties dictionary.</param>
/// <returns>Content name value or null if the property doesn't exist..</returns>
public static string GetContentName(this Dictionary<string, RawProperty> rawProperties)
{
string contentName = null;
if (rawProperties.HasEntries() && rawProperties.ContainsKey(MetaDataProperties.PageName))
{
contentName = rawProperties[MetaDataProperties.PageName].Value;
}
return contentName;
}
/// <summary>
/// Gets the content type name value.
/// </summary>
/// <param name="rawProperties">Raw properties dictionary.</param>
/// <returns>Content type name value or null if the property doesn't exist..</returns>
public static string GetContentTypeName(this Dictionary<string, RawProperty> rawProperties)
{
string contentTypeName = null;
if (rawProperties.HasEntries() && rawProperties.ContainsKey(MetaDataProperties.PageTypeName))
{
contentTypeName = rawProperties[MetaDataProperties.PageTypeName].Value;
}
return contentTypeName;
}
/// <summary>
/// Gets the language branch identifier (like en, sv, fi, etc) value.
/// </summary>
/// <param name="rawProperties">Raw properties dictionary.</param>
/// <returns>Language branch identifier value or null if the property doesn't exist</returns>
public static string GetLanguageId(this Dictionary<string, RawProperty> rawProperties)
{
string languageId = null;
if (rawProperties.HasEntries() && rawProperties.ContainsKey(MetaDataProperties.PageLanguageBranch))
{
languageId = rawProperties[MetaDataProperties.PageLanguageBranch].Value;
}
return languageId;
}
/// <summary>
/// Gets the content guid value.
/// </summary>
/// <param name="rawProperties">Raw properties dictionary.</param>
/// <returns>Content guid value or null if the property doesn't exist.</returns>
public static string GetContentGuid(this Dictionary<string, RawProperty> rawProperties)
{
string contentGuid = null;
if (rawProperties.HasEntries() && rawProperties.ContainsKey(MetaDataProperties.PageGUID))
{
contentGuid = rawProperties[MetaDataProperties.PageGUID].Value;
}
return contentGuid;
}
/// <summary>
/// Gets the content type id value.
/// </summary>
/// <param name="rawProperties">Raw properties dictionary.</param>
/// <returns>Content type id value or null if the property doesn't exist.</returns>
public static string GetContentTypeId(this Dictionary<string, RawProperty> rawProperties)
{
string contentTypeId = null;
if (rawProperties.HasEntries() && rawProperties.ContainsKey(MetaDataProperties.PageTypeID))
{
contentTypeId = rawProperties[MetaDataProperties.PageTypeID].Value;
}
return contentTypeId;
}
/// <summary>
/// Gets the content url segment value.
/// </summary>
/// <param name="rawProperties">Raw properties dictionary.</param>
/// <returns>Content url segment value or null if the property doesn't exist.</returns>
public static string GetUrlSegment(this Dictionary<string, RawProperty> rawProperties)
{
string urlSegement = null;
if (rawProperties.HasEntries() && rawProperties.ContainsKey(MetaDataProperties.PageURLSegment))
{
urlSegement = rawProperties[MetaDataProperties.PageURLSegment].Value;
}
return urlSegement;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment