Sitecore: Follow relational links in Sitecore Query - sitecore

I have an item structure that doens't correspond to "documents" in the standard Sitecore sense. It corresponds to data items.
One of the relationships that my items have is the following:
Person -> State -> Country
Where a Person has a link field to a State and state's have link fields to a Country.
My question is: How would one write a query to retrieve all of the People who have an eventual link to a Country of a certain abbreviation?
I'm stumped as to how to do it in Sitecore Query. My current solution is using LINQ and several queries.

Assuming that the links between these items where created using a Reference field, such as a Droplink, Treelist, Droptree, etc., you can traverse the references/referrers using the Sitecore Links database.
Here is an example (untested) that starts with a country item and fetches any referring states. The same query is performed on the states to fetch any referring people. The results are added to a generic List<Item> collection.
List<Item> peopleList = new List<Item>();
var countryReferences = Globals.LinkDatabase.GetReferences(countryItem);
var referringStates = countryReferences.Select(c => c.GetTargetItem()).Where(c => c.Template.Key == "state");
foreach (var state in referringStates)
{
var stateReferences = Globals.LinkDatabase.GetReferences(state);
var referringPeople = stateReferences.Select(s => s.GetTargetItem()).Where(s => s.Template.Key == "person");
foreach (var person in referringPeople)
{
peopleList.Add(person);
}
}

Related

Ember.js Multiple Selection in a Table

I'm newbie in Ember.js framework, and I have a problem, I need a multiple selection of rows in a table. I'm using jQuery Datatables, with Ember-Data, and I need to have an ID of the selected rows, something like push to the array and I have no clue how to do this.
For multiple selection, make sure you initialize the table with the select option set to "multi":
this.$("#myDT").DataTable({
select: "multi"
});
When you want to get the list of all the rows selected, use a jQuery selector to get all the rows that have the selected class and get their data. In this example, the ID is the first column in the data, hence the [0]
var selectedRows = Ember.$('#myDT tbody tr.selected');
var selectedIDs = [];
Ember.$.each(selectedRows, function (i, element) {
selectedIDs.push(table.row(element).data()[0]);
});
You can read more about DataTables API (for things like getting row data using the row.data() method here: https://datatables.net/reference/api/

How to apply Entity_Filter as OR in Dynamics NAV WebService

I am consuming a Dynamics NAV 2009 WebService (SOAPish). Specific I am talking about the ReadMultiple method MSDNA
The filterArray takes an array of Entity_Filter objects. Each Entity_Filter has a Field and Criteria property.
Now I want to know, how I can apply the filters as OR not and.
For Example I am passing two filters:
Entity_Filter { Field: "Name", Criteria: "*banana*" }
Entity Filter { Field: "Color", Criteria: "yellow"}
If I send this to the ReadMultiple I get all entries that have "banana" as part of the name AND the color yellow. What do I have to do, to get all Entries that have "banana" as part of the name OR the color yellow?
Unfortunately, there is no way to apply an 'OR' filter on a record. ReadMultiple method applies filters the same way C/AL methods SETFILTER or SETRANGE do. It has to be done in two separate requests.
On the second thought, you can do it in a single request, but with an OData service using Linq:
NAV nav = new NAV(new Uri("http://ServerName:Port/DynamicsNAV71/OData/Company('Your Company Name')"));
nav.Credentials = CredentialCache.DefaultCredentials;
var items = from i in nav.ItemsList where i.Name.Equals("Banana") || i.Color.Equals("Yellow") select i;
Where 'ItemsList' is an OData web service of type 'Page'

Sitecore Search Orderby

I am seeing a very strange behaviour from the Sitecore 7.1 Search when ordering by a string field. The code is something like this:
var indexableItem = new SitecoreIndexableItem(Sitecore.Context.Item);
var searchIndex = ContentSearchManager.GetIndex(indexableItem);
var context = searchIndex.CreateSearchContext();
var results = new List<Item>();
var count = 0;
var totalSearchResults = 0;
var contactColleagues = context.GetQueryable<SearchResultItem>()
.FirstOrDefault(x => x.ItemId == Sitecore.Context.Item.ID);
if (contactColleagues != null)
{
var items = contactColleagues.GetDescendants<ColleagueSearchResultItem>(context)
.Where(x => x.TemplateId == Templates.Colleague);
items = items.OrderBy(x => x.Surname);
var resultItems = items.Page(this.PageNumber, PageSize).GetResults();
}
This all works as expected and returns my dataset ordered by surname as expected. That is until a certainly individual comes along who's surname is "Or". Now Sitecore returns this persons name at the start of the list, no matter what we do.
After some testing I found the same issue happened if I decided to call someone "Andy And", this would appear at the being of the list before "Jeff Aardvark".
I'm assuming this is a bug in the way the data is being presented to the Lucene index.
Has anyone come across this, or have any thoughts about how this could be worked around?
Thanks in advance.
I think you have a problem with stop words. The default analyzer removes them when the item is crawled. You can however prevent this behaviour.
This post explains how to to turn off the stop words filter:
http://blog.horizontalintegration.com/2015/01/08/sitecore-standard-analyzer-turn-off-the-stop-words-filter/
If you look at the surname field in the index using Luke (https://code.google.com/p/luke/) is the value blank? It sounds like potentially dangerous query values are being stripped out either at the index level or as they are being loaded from the index.

Retrieve field groups from SharePoint CSOM

I want to retrieve the existing field groups (from root) and display them in a dropdown list.
I'm using this code to retrieve all the columns (and display which group they're belonging to):
var web = clientContext.Web;
FieldCollection rootFields = web.Fields;
clientContext.Load(
rootFields,
fields => fields
.Include(field => field.Group)
);
clientContext.ExecuteQuery();
foreach (Field _fields in rootFields)
{
fieldsList.Add(new SelectListItem { Text = _fields.Group });
}
This shows a few hundred groups (duplicates ofc), I want to narrow it down to just the few groups that exist, and sort out the duplicates. Or is there another way to do this?
You can do it via Linq:
Distinct will sort the duplicates out of your result.
var results = rootFields.ToList().Select(field => field.Group).Distinct();
foreach (var_group in results)
{
fieldsList.Add(new SelectListItem { Text = _group });
}

Can I use the DynamoDB .NET Object Persistence Model when I have a Global Secondary Index?

I have a table in Dynamo with a hash & range index plus a secondary global index with just a hash. If I try to Query or Save an object I get the following error:
Number of hash keys on table TableName does not match number of hash keys on type ObjectModelType
(Replacing TableName and ObjectModelType with the actual table and model type)
I have the hash properties (both the primary and secondary) decorated with DynamoDBHashKey
Googling the error turns up exactly zero results
Update: Ok, so not exactly zero, obviously it now returns this question!
Update the second: I've tried using the helper API & it works just fine, so I am assuming at this point that the Object Persistence Model doesn't support Global Secondary Indexes
I encountered the same problem and found FromQuery worked, although QueryFilter is actually from the DocumentModel namespace:
var queryFilter = new QueryFilter(SecondaryIndexHashKeyColumn, QueryOperator.Equal, "xxxx");
queryFilter.AddCondition(SecondaryIndexRangeKeyColumn, QueryOperator.LessThan, DateTime.Today);
var items = context.FromQuery<MyItem>(new QueryOperationConfig { IndexName = SecondaryIndexName, Filter = queryFilter }).ToList();
Thank you Brendon for that tip! I was able to adapt it to get deserialization of a multiple result set.
var queryFilter = new QueryFilter(indexedColumnName, QueryOperator.Equal, targetValue);
Table table = Table.LoadTable(client, Configuration.Instance.DynamoTable);
var search = table.Query(new QueryOperationConfig { IndexName = indexName, Filter = queryFilter });
List<MyObject> objects = new List<MyObject>();
List<Document> documentSet = new List<Document>();
do
{
documentSet = search.GetNextSetAsync().Result;
foreach (var document in documentSet)
{
var record = JsonConvert.DeserializeObject<MyObject>(document.ToJson());
objects .Add(record);
}
} while (!search.IsDone);
Thanks x1000!! :)