Base solution for your next web application
Open Closed

Getting the value of a user setting #2512


User avatar
0
alukaszewski created

How do I get the value of a user setting using a controller? I am trying:

bool UseAutoRefresh = SettingManager.GetSettingValueForUser("UseAutoRefresh");

but Visual Studio is throwing error "No overload for method 'GetSettingValueForUser' takes 1 arguments."

I am able to use SettingManager.GetSettingValue("settingname") in the same controller without a problem - am I missing something?

Also, is it possible to directly access User Settings from Client Side JavaScript - or do I have to use Ajax call for Controller action result?

Thanks.


7 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you want to use GetSettingValueForUser, you need to pass userId and tenantId. GetSettingValue gets the setting value for current logged in user.

    Settings can be used in client side, see <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Setting-Management#DocGettingValuesClient">http://aspnetboilerplate.com/Pages/Docu ... luesClient</a>.

    Just set IsVisibleToClients to true when you are defining the setting.

  • User Avatar
    0
    alukaszewski created

    I followed this guide to extend the User Entity, simply adding to User.cs and ran a migration:

    <a class="postlink" href="https://aspnetzero.com/Documents/Extending-Existing-Entities">https://aspnetzero.com/Documents/Extend ... g-Entities</a>

    ...so I did not 'define' my new setting and there is nowhere to set IsVisibleToClients to true. Is that the wrong way to implement user-based settings, have I misunderstood? Is it possible to get the value of an extended property using SettingManager or is that only for retrieving values defined using SettingProvider?

    If I follow your suggestion of defining a new setting using SettingProvider, would that then be stored in the AbpSettings table instead of AbpUsers and show user specific values for the TenantId and UserId field instead of NULL?

    Can I just add additional User based settings to the AppSettingProvider.cs?

  • User Avatar
    0
    alukaszewski created

    OK, I'm really stuck with this one.

    I am able to EXTEND an existing entity (the user) and can use the existing CreateEditUser modal and UserSettings modal to display and change the vale (it's a checkbox/boolean) - but I am lost as to HOW to retrieve that value using abp.settings (client side) or using a controller action. The stored value is in the abpUsers table.

    or,

    I am able to add new pre-defined User Visible settings and I can retrieve the values of these using client side abp.settings (and probably with a controller action) (these are stored in abpSettings table) but I am completely lost as to how to implement the display/changing of that value in the CreateEditUser modal or UserSettings modal.

    Basically, I'd like to implement a per-user true/false setting (called AutoRefresh) that the user can access via their own settings, or the admin can set on a user account via the user editing. When a page loads, I'd like to test for that value and run code as appropriately.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In your case, it is possible to do it in both ways (Extending the User entity or adding a user specific setting).

    • If you choose the extending User entity way,

      • you cannot access it directly from client side. You need to make some extra work for it.
      • You can put a field for create/edit user modal and update user entity.
    • If you choose the setting way,

      • You can access it from client side easily
      • You can put a field for create/edit user modal, and when creating/updating a user entity, you also need to change the value of setting as well.

    Since you know your system best, you need to decide if this AutoRefresh is a user setting or a property of a user. Please let us know if there are some unclear points.

  • User Avatar
    0
    alukaszewski created

    I'd like "AutoRefresh" true/false to be a property of the user - this is so that an Admin can pre-create a user and set this property in CreateEditUser modal AND the user can access the property from the MySettings modal. I have already got that far and I can display/set the property in those modals I mentioned.

    What I am now stuck with, is how to read that value from any page or a controller action.

    I'm almost getting there, I feel very close to a solution by using "abp.services.app.profile" in client side script and I see the http 200 response body containing the current user profile data - but for some reason I am unable to capture that response into an object.

  • User Avatar
    0
    alukaszewski created

    I'm now having some success by simply using an AJAX POST with url: abp.appPath + 'api/services/app/profile/GetCurrentUserProfileForEdit' and storing the response in abpUser object. This returns all current user profile properties which I can then inspect via code. Is there a better way?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you don't access this information so often, you can use it this way. Otherwise, you can cache it on the server side (good for MPA) or on the client side (good for SPA).