Abu Hurairah reported that the Messenger of Allah, upon whom be peace, said, “When one of you relieves himself, he should neither face the qiblah nor turn his back on it.” This was related by Ahmad and Muslim.
The prohibition implies that it is only disliked. As Ibn ‘Umar related that he once went to Hafsah’s home, where he saw the Messenger of Allah relieving himself while facing Syria with his back to the Ka’bah. This is related by “the group.” Some reconciliate these hadith by saying that in the desert it is forbidden to face or turn one’s back on the Ka’bah, while it is permitted in buildings. Said Marwan al-Asghar, “I saw ‘Umar sitting on his she-camel and facing the qiblah while urinating. I said, ‘O father of ‘Abdurahman… is this not forbidden?’ He said, ‘Certainly not… This has been prohibited only in open areas. If there is a barricade (or separator) between you and the qiblah, there is nothing wrong with it.”
This is related by Abu Dawud, Ibn Khuzaimah and al-Hakim. Its chain is hassan as Ibn Hajr said in Fath al-Bari.