Campaigns against Egypt
Following the consolidation of Fatimid rule in Ifriqiya, al-Mahdi's first objective was Egypt, the gateway to Syria and Iraq, the old heartlands of the Islamic world and seat of his Abbasid rivals. Tripoli, the easternmost city of the Aghlabid domain, had submitted to the Fatimids following the fall of the Aghlabid emirate, but the local Hawwara Berbers quickly came to resent the overbearing behaviour of the Kutama soldiery, as well as the heavy tax demands placed upon them. A first uprising and siege of Tripoli in 910–911 was followed by a general revolt of the Hawwara in summer 912. The Fatimid governor of Tripoli fled, and all Kutama in the city were slaughtered. Al-Qa'im was put in charge of a combined land and naval expedition, laying siege to Tripoli until it capitulated in June 913. Al-Qa'im left one of the principal Kutama generals, Habasa ibn Yusuf, there, to prepare the further eastward expansion of the Fatimid empire. In January 914, Habasa led an army east, and captured Barqa, the capital of Cyrenaica.
Following this first success, on 11 July al-Qa'im left Raqqada at the head of another army and went east to assume command of the expedition. Al-Qai'm ordered Habasa to await his arrival at Barqa, but Habasa, driven by ambition, led his forces into Egypt, entering Alexandria on 27 August.
At Tripoli, the fortuitous presence of al-Qa'im and his men prevented an attack by a rebel fleet from Sicily, that had recently sacked Sfax further north. Al-Qa'im arrived in Alexandria on 6 November 914, where he imposed the Fatimid call to prayer, a Kutama governor, and an Isma'ili qadi (judge). In early December, as the Nile floods withdrew and allowed the passage of armies along the river, the Fatimid army set out for Fustat in two columns: Habasa ahead, with al-Qa'im following behind. The Abbasid governor, Takin al-Khazari, refused calls to surrender, and instead called for aid from Baghdad and mobilized his forces to defend the river crossing at the pontoon bridge to Rawda Island and Giza. The Fatimid army tried to capture the bridge on 15 December, but was beaten back: Takin's Turkic horse-archers inflicted heavy casualties on the Kutama lancers. The Egyptian forces pursued the Kutama into the night, but during the pursuit the inexperienced levies fell into an ambush, saving the Fatimid army from a complete rout. Some of the Egyptians (Christian Copts and Muslims alike) corresponded with al-Qa'im, revealing the continued presence of an element of possible sympathizers and, according to Heinz Halm, possibly the presence of a Fatimid da'i in Fustat.
Unable to cross the river to Fustat, al-Qa'im led a large part of his army around Takin's defences and into the fertile Fayyum Oasis, where they could find provisions. The Kutama initially plundered the area, but al-Qa'im restored order and imposed a regular tax regime on the inhabitants. At this point, al-Qa'im and Habasa, who had remained behind in command of the bulk of the Fatimid army at Giza, fell out when al-Qa'im ordered Habasa replaced. On 8 January 915, in a large-scale battle at Giza, the Fatimids were decisively defeated; Fatimid sources unanimously attribute this defeat to Habasa, who fled the battlefield, despite al-Qa'im's exhortations to stand firm. The pro-Fatimid accounts maintain that al-Qa'im launched three attacks on the enemy and caused many casualties, but these embellishments cannot hide the fact that the battle was a disaster: with his army collapsing, al-Qa'im retreated to Alexandria, which he entered on 23 January.
Despite the setback, in his letters to his father, and the surviving sermons that he delivered in Alexandria, al-Qa'im appears not to have lost confidence in his ultimate success. At Alexandria, he held a number of Friday prayer sermons (khutbah), propagating the Isma'ili and Fatimid cause. For a while he also engaged in negotiations with some Egyptian defectors, who asked for aman from al-Qa'im, and raised the prospect of the capitulation of Fustat. It appears that al-Qa'im himself was not entirely convinced of the sincerity of such proposals, which became impossible when the Abbasid commander-in-chief Mu'nis al-Muzaffar arrived at Fustat in April 915.
Soon after, Habasa with thirty of his closest followers deserted al-Qa'im and made for Ifriqiya; alarmed by this, al-Qa'im evacuated Alexandria hastily and without battle, leaving much of his armament and equipment behind. The city was quickly reoccupied by the Abbasids. Al-Qa'im arrived at Raqqada on 28 May 915. In his rear, Cyrenaica rose in revolt and overthrew Fatimid control; in Barqa, the entire Kutama garrison was killed.
Despite the failure of the first attempt, preparations began immediately for a second assault on Egypt, starting with the recapture of Cyrenaica. This was accomplished with the surrender of Barqa after an 18-month siege, in April 917. Al-Qa'im set out from Raqqada on his second campaign against Egypt on 5 April 919. His vanguard arrived before Alexandria on 9 July 919, while al-Qa'im with the main body of his army came in September/October. The Abbasid governor of the city fled, and Alexandria surrendered without battle. Having already acknowledged Fatimid sovereignty during the first invasion and hence now considered in revolt, the city was sacked by the Fatimid troops.
As in 914, the Abbasid governor concentrated the few local forces at Giza, to defend the Nile crossing, until Baghdad could send reinforcements: once again, Mu'nis al-Muzaffar was entrusted with the high command, departing for Egypt on 23 February 920. Although al-Qa'im corresponded with several key figures in Fustat, including the former Tulunid vizier, Abu Bakr Muhammad ibn Ali al-Madhara'i, who kept him informed of the Abbasid garrison's state, he made no move to exploit the weakness of the Fustat garrison and storm Giza. It is possible that al-Madhara'i played a double game, trying to delay an attack until fresh Abbasid troops arrived. Al-Qa'im remained at Alexandria for the remainder of the year, as reinforcements continued to arrive from Ifriqiya. These included the Fatimid fleet, 80 ships strong under the eunuch Sulayman, but the Fatimid ships were decisively defeated by an Abbasid squadron under Thamal al-Dulafi on 12 March at Abukir.
After the failure of his navy and the arrival of Abbasid reinforcements, and with the supply situation in Alexandria worsening, al-Qa'im decided to repeat the manoeuvre of 914: on 30 July he left Alexandria and, bypassing Giza, took over the fertile Fayyum Oasis, which could provide provisions and a base of operations. As before, he proceeded to tax the inhabitants, as if he were the rightful ruler of Egypt. Another Kutama force took over Upper Egypt in spring 921 up to the Coptic bishopric of al-Ushmuniyya. Not only did this increase the area under taxation for al-Qa'im, but it also ended the grain supply of Fustat from there.
For an entire year, both sides avoided open conflict, and engaged rather in a diplomatic and propaganda battle. Mu'nis offered promises of safe-conduct (aman), as well as recognition of the Fatimids as autonomous rulers of Ifriqiya in the style of the Aghlabids, if al-Qa'im submitted to the Abbasid caliph. Al-Qa'im rejected these overtures in a letter that reiterated the Fatimids' claims to universal dominion as the rightful heirs of Muhammad. A fragment of a long poem by al-Qa'im, exhorting the inhabitants of Fustat to emulate the Ifriqiyan example and follow the rightful Fatimid da'wa, also survives, via a copy sent to Baghdad. At the same time, the Fatimid commander sent appeals to the two holy cities of Islam, Mecca and Medina, urging them to recognize the Fatimids' claims to sovereignty over the Islamic world. His requests were ignored.
The impasse was broken by the Abbasids in May/June 921: Thamal's fleet captured Alexandria, and then sailed down the Nile to support Mu'nis' attack on Fayyum. The Abbasid forces blockaded the sole connection of Fayym with the Nile at Illahun, cutting al-Qa'im and his men in the oasis from the rest of the country. Once the Abbasid forces began to advance into the oasis, on 8 July al-Qa'im ordered the retreat: all heavy equipment was left behind, while he and his men made their way through the desert to the coastal road to Barqa, an arduous march in which many perished.