MadzQuestioning
MadzQuestioning

Reputation: 3762

Umbraco Document type field value

I'm really new to C# and Umbraco. I need to retrieve the configuration value in my Controller file. Below is the image of my cms and the name it's using is ApiKeys

enter image description here

Now in my controller I want to retrieve the value of googleMapsApiKey but I have ZERO knowledge on how to accomplish this. Is there a helper or any class that I need to use to retrieve this? Now this googleMapsApiKey is not connected to any particular page meaning I should be able to access this value in any page that I want. Any idea on how to achieve this? Sorry if I can't post any code yet since I have no idea on how to start.

Let's just assume that I want to retrieve the value in one of my function in my Controller. How do I do this?

Upvotes: 0

Views: 278

Answers (1)

Thomas
Thomas

Reputation: 677

First you need to use this Document type (APIKeys in your content tree). You can create a node of this content type (APIKeys), then assign a value for the "Google Maps API key" field.

Then, you want to retrieve this value in your C# code or a view.

To do that, you can do something like this:

var myAPIKey = UmbracoContext.Content.GetSingleByXPath("//{apiKeysDocumentTypeAlias}").Value<IPublishedContent>("googleMapsApiKey");

Replace {apiKeyDocumentTypeAlias} with the APIKeys document type alias

I hope this will help you

Upvotes: 1

Related Questions