0

I have created a user in Content Hub with Username as [email protected]. For its Email property, I have set the same value i.e., [email protected]

Assuming that there could be multiple users with same Username and same Email, I would like to fetch all the users by passing email as parameter. The email can be a user's Username or Email or both.

I'm using Postman to test the queries.

When I execute this query, the output is as expected. There is only one returned result and the identifier is that of the User's.

{{Hostname}}/api/entities/query?query=Definition.Name=='User' AND String('Username')=='[email protected]'

But, when I execute this query, there is an output with 1 returned result, but the identifier is that of User Profile, which is not desired. I'm expecting the User Identifier in results and not the User Profile details.

{{Hostname}}/api/entities/query?query=String('Email')=='[email protected]'

I also tried this, which returns no results:

{{Hostname}}/api/entities/query?query=Definition.Name=='User' AND String('Email')=='[email protected]'

How can I fetch the User's details (and not User Profile) by passing email as the parameter.

UPDATE:

Here is the result, when I hit the UserToUserProfile URL

enter image description here

1 Answer 1

1

In order to fetch the User by email, you will need to run two queries as below.

  1. Search for the email property on the UserProfile entity definition with the query as below.
/api/entities/query?query=Definition.Name=='M.UserProfile' AND String('Email')=='{Email_Address}'

Since email property exist on M.UserProfile entity, hence we first need to get the user profile.

  1. Once the user profile is retrieved, we can fetch the User by reading UserToUserProfile relation in the above api response.

enter image description here

This relation contains the User associated with the user profile. Using this way we can fetch the user details using email in Content Hub.

Moreover, in case you are using Web SDK, you can use following snippet to fetch user by email.

using Stylelabs.M.Base.Querying;
using Stylelabs.M.Base.Querying.Linq;
using Stylelabs.M.Framework.Essentials.LoadConfigurations;
using Stylelabs.M.Sdk.Contracts.Base;
using Stylelabs.M.Sdk.WebClient;
using Stylelabs.M.Sdk.WebClient.Authentication;
using System;
using System.Linq;
using System.Threading.Tasks;

string email = "{{EmailAddress}}";
var query = Query.CreateQuery(entities =>
            from e in entities
            where e.DefinitionName == "M.UserProfile"
            && e.Property("Email") == email
            select e);
           
var queryResult = await MClient.Querying.SingleAsync(query, EntityLoadConfiguration.Full);
var userProfileRelation = await queryResult.GetRelationAsync<IChildToOneParentRelation>("UserToUserProfile");
if (userProfileRelation != null)
{
   var userProfileId = userProfileRelation.GetId();
   var user = await MClient.Entities.GetAsync(userProfileId.Value, EntityLoadConfiguration.Full);
}

Hope this helps!!!

8
  • Thank you. It will be 3 queries. The first will be get list of "UserToUserProfile" URLs. The Second, will be hitting this URL to get another URL which will be like "/api/entities/1001", where 1001 is the User ID. Then the Third query of this URL where finally I can see the User Identifier. Is that correct. There should be a better way, I'm assuming!
    – sukesh
    Commented Jun 18 at 6:06
  • 1
    @sukesh No just you need to to read UserToUserProfile relation value /api/entities/12799080/relations/UserToUserProfile, where 12799080 is the user profile id. This is one to one mapping between user and user profile, so there will no list. Reading the href value inside above url would give you all the properties of User. Hope this helps!!!
    – ckhanna
    Commented Jun 18 at 6:12
  • 1
    Moreover, this could all be done in 2 line of code in case you are using Web SDK..But since you are using Rest api, therefore this will be the only way.
    – ckhanna
    Commented Jun 18 at 6:13
  • 1
    @sukesh I have uploaded the code snippet required to get the user by email in case you are using Web SDK. With the sdk, you can effectively read the relation values. Hope that helps!!!
    – ckhanna
    Commented Jun 18 at 6:57
  • 1
    @sukesh I have included the namespaces to be included. This is a LINQ query. You can find details on the same here doc.sitecore.com/ch/en/developers/cloud-dev/…. Hope this helps!!!
    – ckhanna
    Commented Jun 18 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.