Note: Parts Three and Four are back to front! Sorry for the mix up.
A tomb of some sort is discovered at “Libredon" which may or may not have been either Iria Flavia, Compostela, or someplace in between. We are talking somewhere between 818 and 842 and that’s the closest we can get. Pelayo the shepherd is either involved, or not, according to the version you read. Either way, Theodemir the bishop of Iria Flavia is summoned and Lo! the remains contained therewith are immediately proclaimed to be those of the Apostle of Jesus, James the Great (so known as to distinguish him from James “The Lesser" who may, or may not, have been the Brother of Jesus). From this vacillation between might and might not illustrates the difficulty in interpreting the story of St. James. If it appears that I am being irreverent and flippant I ask you to bear with me: once you begin to truly investigate the story of the Camino historical discrepancies abound, alas.
This to-ing and fro-ing notwithstanding, the Moors were at the door. They had Mohammed as their Prophet and it would seem that they were invincible. As they moved northwards with increasing rapidity those in somewhat shaky political positions must have realised that they needed something, or Someone to rouse the people against the Infidel. The legendary success at the Battle of Clavijo where Santiago purportedly appeared on a white horse to support the soldiers sounds convincing. Trouble is it was “written up” at least two centuries later, and the battle in question was not in 844 as stated but in 859 under a completely different king.
The authors of the Historia Compostelana must have flunked their History O Levels!