I will be creating a series of blog posts in the coming weeks addressing issues that a new developer to Umbraco may be confused about. In this post, I will tackle the somewhat simple question of whether it is best to use GUID or Content ID when referencing content.
Umbraco is the leading .net c# content management system, and for good reason. It’s friendly to use, and friendly to developers too.
More and more .net developers are turning to Umbraco for its versatility and support and therefore there are more new developers on the platform than ever. I built my first Umbraco site when it was version 4 (it’s now version 13 and a completely different beast) and like any good CMS, there are multiple ways you can build and structure your site.
I will be creating a series of blog posts in the coming weeks addressing issues that a new developer to Umbraco may be confused about. In this post, I will tackle the somewhat simple question of whether it is best to use GUID or Content ID when referencing content.
While it may not always have been the case, for a good number of years now, every piece of content (including things like member ID’s) has both an integer ID AND a GUID to identify it.
For example, to retrieve data for the homepage you would first go into the content tree within the Umbraco admin, find and select your content, then click 'info' which will give you details about this content including both the ID and the GUID.
Once you have these, you can use them in your code, something like this for its integer ID.
1
|
var homepageContent = Umbraco.Content(1345);
|
or to retrieve the same content using the GUID reference you would use
1
2
|
var guid = new Guid("9eleb4ef-30c2-4549-9385-8cbf93c7fa3f");
var homepageContent = Umbraco.Content(guid);
|
The content ID and the GUID used in the example code above are an example and would be different for your piece of content.
On the face of it, there’s not much difference in the code, so what exactly is the difference between the integer ID and the GUID?
- Content ID: A unique integer assigned to each piece of content upon creation. This is simple and very convenient. However, the value of the ID can change during data migrations or deployments across different environments (for example between development and live databases)
- GUID (Globally Unique Identifier): A string of characters guaranteed to be unique, even across diverse systems. It remains constant for a specific piece of content, regardless of its location.
GUID:
- Cross-environment references: When you need to ensure content remains accessible even when migrating between development, staging, and production environments, GUIDs are your best bet.
- Integration with external systems: If you're working with external systems that rely on unique identifiers, using GUIDs ensures seamless communication.
- Future-proofing your code: As your Umbraco development continues, relying on GUIDs can prevent issues down the line, especially if data migrations become necessary.
Content ID:
- Simple, within-environment scenarios: If you're working solely within a single Umbraco instance and don't anticipate future migrations, content IDs can be a simpler choice.
- Content pickers in the back office: While I have never experienced this myself. I have been told that some of Umbraco's built-in content pickers only utilise content IDs for navigation.
This may be more of a problem in earlier versions of Umbraco, but not an issue that has arisen on any of my developments, especially since version 9.
The answer is pretty obvious
While content IDs offer a degree of convenience, I recommend using GUIDs for all developments. Their reliability across environments and future-proofing capabilities outweigh the simplicity of content IDs. After all, look at the code examples above, using a GUID is generally 1 line of code more. Remember, a little extra effort upfront can save you headaches later.
Hey, I really could do with your help! If you find this article interesting, could you please do me a favour by either sharing it on your site or on social media. I would love to hear yours and other peoples' thoughts on this subject. And if this or any other content on the site has helped you and you would like to show your appreciation, then you can always
buy me a coffee ☕️ It would make the time I put into this more than worthwhile! Thank you 😃