Last active
January 6, 2022 22:40
-
-
Save jhorsman/84674a2874dc80ea69e8e03f521c8c23 to your computer and use it in GitHub Desktop.
TcmUri object for .NET
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
using System; | |
using System.Text.RegularExpressions; | |
using Tridion.ContentManager.CoreService.Client; | |
namespace TridionUtils | |
{ | |
/// <summary> | |
/// Creates TcmUri class from a string. Makes it easy to retrieve TcmUri properties like PublicationId, ItemId, ItemTypeId and Version. | |
/// Usage: var PubliationId = new TcmUri("tcm:45-3456-64").PublicationId; | |
/// </summary> | |
public class TcmUri | |
{ | |
public const string Null = "tcm:0-0-0"; | |
public int ItemId { get; set; } | |
private int _publicationId; | |
public bool IsVersionless; | |
public int PublicationId | |
{ | |
get { if(_publicationId == 0) return ItemId; else return _publicationId;} | |
set { _publicationId = value;} | |
} | |
public int ItemTypeId { get; set; } | |
public int Version { get; set; } | |
public ItemType ItemType | |
{ | |
get { return (ItemType)ItemTypeId; } | |
} | |
public bool isValid { get; set; } | |
public TcmUri(string uri) | |
{ | |
Regex re = new Regex(@"tcm:(\d+)-(\d+)-?(\d*)-?v?(\d*)"); | |
Match m = re.Match(uri); | |
if (m.Success) | |
{ | |
isValid = true; | |
_publicationId = Convert.ToInt32(m.Groups[1].Value); | |
ItemId = Convert.ToInt32(m.Groups[2].Value); | |
if (m.Groups.Count > 3 && !string.IsNullOrEmpty(m.Groups[3].Value)) | |
{ | |
ItemTypeId = Convert.ToInt32(m.Groups[3].Value); | |
} | |
else | |
{ | |
ItemTypeId = (int)ItemType.Component; | |
} | |
if (m.Groups.Count > 4 && !string.IsNullOrEmpty(m.Groups[4].Value)) | |
{ | |
Version = Convert.ToInt32(m.Groups[4].Value); | |
IsVersionless = false; | |
} | |
else | |
{ | |
Version = 0; | |
IsVersionless = true; | |
} | |
} | |
else | |
{ | |
isValid = false; | |
} | |
//add logging if fails, outputting the uri parameter | |
} | |
public TcmUri(int publicationId, int itemId, int itemTypeId, int version) | |
{ | |
PublicationId = publicationId; | |
ItemId = itemId; | |
ItemTypeId = itemTypeId; | |
Version = version; | |
} | |
public TcmUri(int itemId, Tridion.ContentManager.CoreService.Client.ItemType itemType, int publicationId) | |
{ | |
string uri = string.Format("tcm:{0}-{1}-{2}", publicationId, itemId, itemType); | |
new TcmUri(uri); | |
} | |
public override string ToString() | |
{ | |
return IsComponentUri() ? | |
string.Format("tcm:{0}-{1}", PublicationId, ItemId) : string.Format("tcm:{0}-{1}-{2}", PublicationId, ItemId, ItemTypeId); | |
} | |
private bool IsComponentUri() | |
{ | |
return ItemType == ItemType.Component; | |
} | |
internal static bool IsValid(string value) | |
{ | |
TcmUri toTest = new TcmUri(value); | |
return toTest.isValid; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment