Table of Contents

Orchard Snippets

Create a route

public class Routes : IRouteProvider {
	private const string ModuleName = "Microsoft.RoadmapPOC";
 
	public IEnumerable<RouteDescriptor> GetRoutes() {
		return new[] {
			new RouteDescriptor {
				Priority = 9999, // This route is pretty bloody important.
				Route = new Route(
					"Roadmap/{action}",
					new RouteValueDictionary {
						{"area", ModuleName},
						{"controller", "Roadmap"}
					},
					new RouteValueDictionary(),
					new RouteValueDictionary {
						{"area", ModuleName}
					},
					new MvcRouteHandler()
				)
			}
		};
	}
 
	public void GetRoutes(ICollection<RouteDescriptor> routes) 	{
		foreach (var route in GetRoutes()) 		{
			routes.Add(route);
		}
	}
}

Controller

public class RoadmapController : Controller {
	public ActionResult Test() {
		return Content("Hello there!");
	}
 
	[HttpPost]
	public ActionResult Upload() {
		byte[] bytes = new byte[Request.InputStream.Length];
		Request.InputStream.Read(bytes, 0, (int)Request.InputStream.Length);
		var jsonPayload = Request.ContentEncoding.GetString(bytes);
		...
	}
}

Part extraction in a view

// Statically typed.
var contentItem = (ContentItem)Model.ContentItem;
var titlePart = contentItem.As<TitlePart>();
var animalName = titlePart.Title;
var speciesField = (TaxonomyField)contentItem.Parts.SelectMany(x => x.Fields).Single(x => x.Name == "Species");
var species = String.Join(" ", speciesField.Terms.Select(x => Html.ItemDisplayLink(x)));
var genderField = (BooleanField)contentItem.Parts.SelectMany(x => x.Fields).Single(x => x.Name == "Gender");
var genderDescription = genderField.Value == true ? T("male") : T("female");
 
// Dynamically typed.
var contentItem = Model.ContentItem;
var titlePart = contentItem.TitlePart;
var animalName = titlePart.Title;
var speciesField = (TaxonomyField)contentItem.Animal.Species;
var genderField = contentItem.Animal.Gender;
var genderDescription = genderField.Value == true ? T("male") : T("female");
var species = String.Join(" ", speciesField.Terms.Select(x => Html.ItemDisplayLink(x)));

Return two Parts in one Driver

protected override DriverResult Display(StarsPart part, string displayType, dynamic shapeHelper) {
	return Combined(
		ContentShape(
			"Parts_Stars",
				() => shapeHelper.Parts_Stars(BuildStars(part))),
		ContentShape(
			"Parts_Stars_SummaryAdmin",
				() => shapeHelper.Parts_Stars_SummaryAdmin(BuildStars(part)))
		);
}