Developer forum

Forum » PIM » Query for properties

Query for properties

Aki Ruuskanen
Aki Ruuskanen
Reply

Hi,

Is this possible?

I have a property field of type checkboxlist. 

I would need a query that returns all products where that property is added. But only the products where no checkboxes is checked. 

I tried "IsEmpty" but that one return also products there the property is not added.

Can that be done?

Regards / Aki 


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I doubt that you can, as in both cases nothing will be added to the index. 

Can you add a "None selected" option to the checkbox list?

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Thanks. That makes sense.

Do you know if there are any documentation about creating code expressions for PIM Queries? (Or macros)

Regards / Aki

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Nope, sorry, I am not aware of any....

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Any DW-guys that can point me in the right direction in how to code query expressions in PIM?

 

 

TermCode.png
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Aki,

 

Here's a Macro we developed a while back 

 

using System;
using System.Collections.Generic;
using System.Linq;
using Dynamicweb.Extensibility.Macros;

namespace Dna.RepositoryExtensions
{
    /// <summary>
    /// Represents user macro
    /// </summary>
    /// <seealso cref="Macro" />
    /// <autogeneratedoc />
    public class GetRealUser : Macro
    {
        private static readonly Dictionary<string, Func<object>> SupportedActionsInternal = new Dictionary<string, Func<object>>()
        {
            {
                "SecondaryUserIdOrPrimaryUserId",
                () => Extensions.Users.GetRealUser()?.ID ?? -1
            }
        };

        private static readonly object LockObject = new object();

        /// <summary>
        /// Evaluates the specified action.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <returns>The value of the action that was evaluated; <c>null</c> if the action did not evaluate to a specific value.</returns>
        public override object Evaluate(string action)
        {
            lock (LockObject)
            {
                if (SupportedActionsInternal.ContainsKey(action))
                {
                    try
                    {
                        return SupportedActionsInternal[action]();
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                }
            }

            return null;
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public override string Name => "Rizzo Macros";

        /// <summary>
        /// Gets the supported actions.
        /// </summary>
        /// <value>The supported actions.</value>
        public override IEnumerable<string> SupportedActions
        {
            get
            {
                IEnumerable<string> result;
                lock (LockObject)
                {
                    result = SupportedActionsInternal.Keys.ToList();
                }
                
                return result;
            }
        }
    }
}

It uses this method that lives elsewhere

using System;
using Dynamicweb.Configuration;
using Dynamicweb.Security.UserManagement;

namespace Dna.Extensions
{
    public static class Users
    {
        /// <summary>Gets the user of the user who logged in, not the impersonated user.
        /// </summary>
        public static User GetRealUser()
        {
            var user = User.GetCurrentUser(PagePermissionLevels.Frontend);

            if (user == null)
            {
                return null;
            }
            
            var impersonationSetting = SystemConfiguration.Instance.GetValue("/Globalsettings/Modules/Users/Impersonation");
            if (!"Full".Equals(impersonationSetting, StringComparison.CurrentCultureIgnoreCase))
            {
                return user;
            }
            
            var secondaryUser = user.CurrentSecondaryUser; 
            return secondaryUser ?? user;
        }
    }
}

And it looks like this in the backend

https://www.screencast.com/t/DstepFBxifUp

 

Happy coding,

Nuno

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Thanks Nuno. Appriciated!

Regards /Aki

 

You must be logged in to post in the forum