During the 12th century, Aranjuez (then known as "Aranz", "Aranzuel", "Aranzuegue" or "Almuzundica") was a small hamlet under the influence of the castle of Oreja. Following the 1108 Castilian and Leonese defeat to the Almoravids at Uclés, Muslims took control of the area. "Aurelia" (Oreja) was retaken by Alfonso VII, the Emperor after the 1139 surrender of the besieged castle. Alfonso VIII donated Oreja and its dependent hamlets in 1171 to the Order of Santiago and its Grand Master Pedro Fernández. Aranjuez was definitively secured in 1178.