Episerver empty attribute

500 Views Asked by At

I'm trying to search all community users to get a list of people who do not have a custom attribute set.

Using the query StringCriterion, is there a way to match against null rather than a value?

I've tried the following code, but it doesn't work.

    private static UserCollection GetAllUsersWithNoCode()
    {
        UserQuery query = new UserQuery();
        StringCriterion criterion = new StringCriterion();
        criterion.Value = null;
        query[AttributeNames.CommunityUser.AUTO_LOGIN_TOKEN] = criterion;
        return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(query);
    }

Thanks in advance for anyone who can help...

2

There are 2 best solutions below

2
On BEST ANSWER

Just had a reply back from Episerver support. They state that it's not possible to query custom attributes this way. The solution is to manually add rows to the DB (for all users) with empty strings and then use the string.empty value to find the results.

Not quite the answer I was hoping for, but I'll test and update with results later...

0
On

I see this is already solved but there is a way to check for null values. You could check for null using something like this, I'm not sure if this will work if the attribute isn't created for the particular object(s): Consider this pseudo code, I haven't tested if this will compile.

public class NullStringCriterion : StringCriterion
{
    public override string GetQuery(string propertyName)
    {
        return String.Format(" ({0} IS NULL) ", propertyName);
    }
}

and then:

private static UserCollection GetAllUsersWithNoCode()
{
    UserQuery query = new UserQuery();
    query[AttributeNames.CommunityUser.AUTO_LOGIN_TOKEN] = new NullStringCriterion();
    return CommunitySystem.CurrentContext.DefaultSecurity.GetQueryResult(query);
}