Q: Do I have to pray before I sit in the Masjid?
A: The 2-rakah prayer to “greet the masjid” before sitting is sunnah based on the following conditions: 1. That the one who enters is in a state of ritual purity from the 2 hadaths (minor and major). 2. It is not a prohibited time (except for when the Imam is delivering the sermon in the masjid). 3. He is not the khateeb. 4. He has not entered the masjid to pray Eid. 5. The obligatory prayer has not already started. 6. He is not the caretaker of the masjid who repetitively enters. 7. It is not Masjid al-Haram.
Summarized from Hashiyat al-Khalwati ala al-Muntaha Vol. 1 Pg 499 and Hidayat al-Raghib Vol. 2 Pg 202 Courtesy of د. مطلق الجاسر