The village of Abrantes and the Abrantes Castle were conquered from the Moors by Afonso I of Portugal in 1148. After 1172, Abrantes was a military outpost, under the dominion of the Order of Saint James of Compostela. In 1173, the region, including its castle, were donated to the Order of Saint James of the Sword, a distinct order. Pope Nicholas IV (around 1290) released this Order from the jurisdiction of the Castilian grand master of Santiago de Compostela Order in Uclés. It received its municipal foral in 1179, in compensation for its resistance against the Moroccan Abem Jacob. Its creation in 1179, was based on the "concelhos perfeitos" ("perfect municipality") model; they were formed on the specific day that settlers were invited to populate them; the administrative regime, the organization of the municipal authority and geographic distribution were planned as part of system of populating uninhabited lands, and extending the political influence of the Kingdom. Its geography contributed to the colonization: