In the beginning of the 8th century when the Visigothic Kingdom was conquered by the Muslim troops of the Umayyad Caliphate, Barcelona was taken by the Muslim wali of Al-Andalus, Al-Hurr ibn Abd al-Rahman al-Thaqafi. After the failure of the Muslim invasion of Gaul at the Battles of Toulouse in 721 and Tours in 732, the city was integrated into the Upper March of Al-Andalus.
From 759 onwards the Frankish Kingdom embarked on the conquest of the territories under Muslim domination. The capture of the city of Narbonne by the forces of the Frankish king, Pepin the Short, brought the border to the Pyrenees. The Frankish advance was met with failure in front of Zaragoza, when Charlemagne was forced to retreat and suffered a setback in Roncevaux in the hands of Basque forces allied with the Muslims. But in 785, the rebellion of the inhabitants of Girona, who opened their gates to the Frankish army, pushed back the border and opened the way for a direct attack against Barcelona. On April 3, 801, Harun, commander of Barcelona accepted terms to surrender the city, worn out by hunger, deprivation and the constant attacks. The inhabitants of Barcelona then opened the gates of the city to the Carolingian army. Louis, Charlemagne's son, entered the city preceded by priests and clergy singing psalms, processing to a church to give thanks to God.
The Carolingians made Barcelona the capital of the County of Barcelona and incorporated it into the Hispanic Marches. Authority was to be exercised in the city by the Count and the Bishop. Bera, son of the Count of Toulouse, William of Gellone, was made the first Count of Barcelona.