1

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...

Rob Taylor
  • 197
  • 2
  • 7

2 Answers2

1

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...

Rob Taylor
  • 197
  • 2
  • 7
  • That surprises me, but unless you have a lot of users you can always get the entire user table and filter the list using linq – Eric Herlitz Apr 10 '14 at 12:21
  • I can confirm this does work. We have 1.2 million users in our system, so it was the best approach (considering that I need to add the attribute to almost 1.15m users!!) – Rob Taylor Apr 11 '14 at 13:37
0

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);
}
RBaarda
  • 558
  • 3
  • 10