Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Vijitha 8 posts 98 karma points
    Jun 16, 2022 @ 08:42
    Vijitha
    0

    Database Query

    How can I query the umbraco database to get data?

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Jun 16, 2022 @ 10:09
    Marc Goodson
    0

    Hi Vijitha

    Normally you wouldn't! As accessing the database is slow.

    Umbraco has a published cache, and usually you query and display content from the Cache:

    The 'UmbracoHelper' is a good starting point to query your published data:

    https://our.umbraco.com/Documentation/Reference/Querying/UmbracoHelper/

    If you are talking about updating Umbraco data programmatically, eg as part of an import, or in a custom package, then you can use the Umbraco Services gateway to access the ContentService and MediaService which enable you to query, and update data directly in the database - but this is slow, and shouldn't be used on the frontend.

    https://our.umbraco.com/Documentation/Reference/Management/Services/ContentService/

    Finally if you have a custom database table, that you need to query, perhaps a table of products not managed by Umbraco that you need to query data from, then you can connect directly to the Umbraco database, and use SQL.

    Umbraco ships with a lightweight ORM called NPoco, which means you can create a c# class that represents your database table, and write SQL to pull back data,

    There is some info here about Creating a custom database table:

    https://our.umbraco.com/documentation/extending/database/ (also some examples here of using NPoco in V8 that will be similar: https://codeshare.co.uk/blog/how-to-do-crud-operations-with-npoco-in-umbraco-8/)

    regards

    marc

  • Vijitha 8 posts 98 karma points
    Jun 16, 2022 @ 11:08
    Vijitha
    0

    Thank you Marc...Actually my requirement is to show the name of the logged in user instead of email in the website. I am using active directory authentication for members. I tried adding identity to the context but its getting overwritten.

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Jun 18, 2022 @ 20:16
    Marc Goodson
    100

    Hi Vijitha Ahh I see!! In which case I think you can use the IMemberManager to access your logged in members information...

    https://our.umbraco.com/Documentation/Reference/Querying/IMemberManager/

    There is a GetCurrentMemberAsync method I think you can use

    And you can use AsPublishedContent to give you access to all the properties configured on your Member Type for the current logged in Member...

    Regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft