CRM Online – Retrieve all users

Being reasonably new to the CRM SDK I needed a method to retrieve all users from the system.  This was needed as I was doing a piece of work to replicate permissions between CRM business units and SharePoint.

Retrieving all users via a query expression turned out to be quite simple…

private static EntityCollection RetrieveAllUsers(IOrganizationService service)
        {
            QueryExpression query = new QueryExpression
            {
                EntityName = "systemuser",
                ColumnSet = new ColumnSet(true),
                Criteria =
                {
                    Conditions =
                    {
                        new ConditionExpression
                        {
                            AttributeName = "businessunitid",
                            Operator = ConditionOperator.NotNull
                        }
                    }
                }
            };
            return service.RetrieveMultiple(query);
        }

The EntityCollection returns a number of useful attributes, such as the users’ business unit, whether they have a valid licence, account status and so on.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s