void Main()
{
var itemId= GetItemId("/sitecore/content").Dump();
//get child items
//Items.Where (i => i.ParentID==itemId).Select (i => new {i.ID, i.Name}).Dump();
//show field information
Fields.Where (f => f.ItemId==itemId && f.Value!="")
.Select (f => new
{
FieldName=Items.Where (i => i.ID==f.FieldId).Select (i => i.Name).Single (),
f.Value,
f.Language
})
.Dump();
}
Guid GetItemId(string itemPath)
{
var pathParts = itemPath.Split(new char[] { '/'}, StringSplitOptions.RemoveEmptyEntries );
var parentId = Guid.Empty;
var id = Guid.Empty;
foreach (var part in pathParts)
{
if (parentId == Guid.Empty && part.Equals("sitecore", StringComparison.OrdinalIgnoreCase))
{
parentId = new Guid("{11111111-1111-1111-1111-111111111111}");
id = parentId;
}
else
{
id = GetChildGuid(part, parentId);
if (id != Guid.Empty)
parentId = id;
else
{
String.Format("Could not find {0}", part).Dump();
break;
}
}
}
return id;
}
Guid GetChildGuid (string childName, Guid parent_guid)
{
return Items.Where(i => i.ParentID == parent_guid && i.Name == childName)
.Select(i => i.ID)
.FirstOrDefault();
}
Thursday, February 09, 2017
A method to get a sitecore item id given a path
This method will give you a sitecore item id back if you give it a fully qualified path as input.
Wednesday, January 18, 2017
How to flatten a list of lists into a flat list in one line of code: SelectMany is your friend
Given that you have a list of lists, how do you flatten it?
how do you flatten it?
Download linqpad sample, here
how do you flatten it?
With the SelectMany LINQ method, it's easy peasy:
var listOfLists = new List<IEnumerable<int>>() { Enumerable.Range(1, 3), Enumerable.Range(20, 3) };
listOfLists.Dump("List of lists");
listOfLists.SelectMany(lol => lol).Dump("Flat list");
Friday, January 13, 2017
Assigning lambda expression to a variable
In the spirit of writing terse code, I was pleasantly surprised that you can assign a lambda expression to a local variable. A quick sample:
Results:
in Func<string, string>, the first string specifies the input parameter's data type, while the last string is for the return type. If you have more than one input parameter, you'd have multiple parameters specified like this <string, string, string, string> and the last one is always for the return type of the function.
void Main()
{
Func<string, string> sayHello = ((input) => { return input + "!!"; });
System.Diagnostics.Debug.WriteLine(sayHello("hello world"));
}
Results:
hello world!!
in Func<string, string>, the first string specifies the input parameter's data type, while the last string is for the return type. If you have more than one input parameter, you'd have multiple parameters specified like this <string, string, string, string> and the last one is always for the return type of the function.
Wednesday, January 11, 2017
My FSharp patter matching code kata
Given a list of numbers, detect when the values 2,3,4 are present one after the other.
Thursday, January 05, 2017
Extension method to dump Highlighted search results in Linqpad
Here's an extension method which lets you output the found results as html.
For example, if you wanted to show the result of a search and replace, given:
The result would be:
See extension method below:
For example, if you wanted to show the result of a search and replace, given:
var text = "An apple a day keeps the doctor away\n you can say that any day of the year today or tomorrow.";
var find = "day";
var replace = "week";
text.HighlightText(find).Dump("Original text");
text.Replace(find, replace).HighlightText(replace).Dump("updated text");
The result would be:
| Original text |
|---|
An apple a
day
keeps the doctor away
you can say that any
day
of the year.
|
| updated text |
|---|
An apple a
week
keeps the doctor away
you can say that any
week
of the year.
|
See extension method below:
Subscribe to:
Posts (Atom)

