4

Can we use first name in the xDB contact facet and make a search from the UI using an api. I dont want to fetch all the users and do a for loop and then search for the user name like in each iteration.

Can someone please suggest me a best approach for this. This requirement is for ajax call to fetch all users starting with the text on key press.

  • Has any of the provided answers helped you? – Dmytro Shevchenko Feb 13 '17 at 6:35
  • Hi Dmytro, I am checking the recommended solutions. Will update you as soon as I find any. – Meghan Feb 13 '17 at 7:17
3

API endpoint

There is an api route for that (it is being used by exp. profile in fact):

[host]/sitecore/api/ao/v1/contacts/search?pageSize=100&pageNumber=1&sort=visitCount%20desc&match=john

This will search for name and email occurances.

API method

If you want to use your custom controller, you can use the below method from API:

CustomerIntelligenceManager.ContactSearchProvider.Find(new ContactSearchParameters())

Content search

In case it is required to use more complex queries you can use analytics index as any other to search. You should use Sitecore.ContentSearch.Analytics.Models.IndexedContact as an Indexable POCO class

using (var context = ContentSearchManager.GetIndex("sitecore_analytics_index").CreateSearchContext())
{
     var query = context.GetQueryable<IndexedContact>().Where(c=> c.Firstname.Equals("John"));
}
  • 1
    Good idea about using the index. BTW, ContactSearchProvider is just another interface to the Analytics index. – Dmytro Shevchenko Feb 8 '17 at 14:58
  • I'd like to add that a contact in Sitecore is not necessarily a user (from membership table). So you would need some mechanism to mark a contact that's also a user. An example of this are contacts that was uploaded using csv file through the list manager – Rey Rahadian Feb 11 '17 at 1:13
1

First, you'll need to create an index on that field to ensure good query performance. Execute this command from the MongoDB console:

db.Contacts.ensureIndex({ "Personal.FirstName": 1 }, { "sparse": true })

Then you can query the Collection database in your C# code:

ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["analytics"];
var url = new MongoUrl(connectionStringSettings.ConnectionString);
var client = new MongoClient(connectionStringSettings.ConnectionString);
MongoServer server = client.GetServer();

MongoDatabase database = server.GetDatabase(url.DatabaseName);

string searchName = "Meghan";

MongoCursor results =
    database
        .GetCollection("Contacts")
        .Find(Query.Matches("Personal.FirstName", "^" + searchName))
        .SetFields("Personal.FirstName")
        .SetLimit(10);

foreach (BsonDocument r in results)
{
    string firstName = r["Personal"]["FirstName"].AsString;
}

Keep in mind that you're getting pretty low-level with this, circumventing the Sitecore API. If the structure of data changes in the future, you'll need to adjust the query.

Also, never change contact objects directly.

  • At Symposium 2016 they said that they were soon wrapping Mongo/DocumentDB with a layer that we could write apis against. Do you know if they exists in 8.2u2 yet? Maybe it s a 8.3 update. That would help us not having to go directly to Mongo. – Chris Auer Feb 8 '17 at 15:06
  • @dnstommy That's not released yet. But yeah, in the future it should become easier to query the database. – Dmytro Shevchenko Feb 8 '17 at 15:07
  • 2
    I can not stress the last line of this answer more. Heed the warning. NEVER directly edit a contact object. =) Always use available Sitecore API. – Pete Navarra Feb 10 '17 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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