There are many questions on this Dvar Torah simply because this is one of the most confusing pieces in all of Tanach. So if you have any questions I would suggest you try to fast for a year, get the Ben Ish Chai to come to you in a dream and ask him.
39:11-19 “And it was on this day and he (Yosef) came into the house to do his work…. And she (the wife of Potifar) grabbed him by his clothes saying lay with me!”
(Sota 36b) Yosef who was mekadesh Hashem secretly received a letter of Hashem’s name added upon his own (Y-eh-osaif). How was Yosef Mekadesh Hashem? For it says, “and it was on this day and he came into the house to do his work,” Rav Yochanan says both Yosef and the wife of Potifar had in mind to do an averah (immorality), but since Yosef was able to stop himself from sinning he was Mekadesh Hashem’s name. This line of Rav Yochanan is an argument between Rav and Shmuel. One says he came to do his regular work and one says to do his needs (immorality). “She grabbed his clothes saying lay with me!” At that moment the image of Yaakov Avinu appeared to him (in the window) and said, “Yosef, in the future your brothers names are going to be written on the Afod with you in between them. Is it your will to erase your name and write the ‘Haroeh Zonos’?” Ad Kan Divrei Hagemarah
The Maharsha asks, what in the world is going on in this Gemarah? Yosef, according to Rav Yochanan (and either Rav or Shmuel) apparently succumbed to his desires, was saved miraculously by his father’s image, and for this he was Mekadesh Hashem in which he got an additional letter added on to his name? What did Yosef do that was great? It sounds (Chalila) just the opposite – that he went in to have immorality with Ashes Potifar. It’s good that he was able to stop himself, but this is called being Mekadesh Hashem?
The Ben Ish Chai adds by asking what the big deal was with Yosef overcoming his evil inclination. We have seen time and time again how many Tanaaim, Amoraim, Rishonim and even Achronim were willing or did give up their lives rather to succumb to lewdness. For example the Yetzer Harah once decided to test Masya Ben Cheresh (a Tana), so he (the Yetzer) dressed up as a beautiful woman and stood in front of Masya. Wherever Masya turned the woman would appear, so having no other choice he blinded himself with an iron prod (Hashem then sent Raphael to heal him after promising to no longer test him with taavas isha). A little extreme, but we see and have seen, how one can fight against the Yetzer Hara, so what was so great about Yosef?
Answers the Ben Ish Chai with a chidush. A normal human being will have a constant conflict his entire life with his Yetzer Hara. Sometimes he wins, but sometimes he doesn’t. It is a back and forth battle until the day of his death. The reason for this is because when that person wins a battle, it means he is pushing his Yetzer away for the time being, but the Yetzer will return and fight even harder next time. This is all because if one does defeat his Yetzer, it is done by pushing it away, but rarely does he get rid of the Yetzer for good. For example if a person has a Tayva (Desire) to look at inappropriate images, and uses all of his power to stop himself, what will he do? He’ll run away from the images, or do something else to get his mind off the images for the time being. The problem with this is that the desire for doing that sin still remains inside him, he just stopped it for the time being. (Obviously this doesn’t have to be true by every single person or by every single sin.)
Yosef on the other hand was not so. Yosef wanted to get rid of his Yetzer Hara for good, so he fought in a different manner. He brought himself to the point where the sin was right in front of him, where the tayva would be the greatest, and yet he was able to stop himself. Yosef knew the only way he could completely stop the Yetzer from ever trying to fight with him, would be to prove to the Yetzer that even if the sin was in his face he still wouldn’t do it. By using his father’s image (according to this view Yosef himself thought of Yaakov to assist him in his fight against the Yetzer) he was able to control himself even at the time of the maasa itself, and say to the Yetzer, “you have nothing on me!” All the other stories consist of Tzaddikim who are able to push off the Yetzer, but there still remains a slight spark of desire in them, and therefore the battle rages on and on until his death. Yosef took on the Yetzer head on and came out successful. Yosef did this because he was afraid that if he were to leave a spark of desire in him, he may not have succeeded throughout his Galus in Mitzrayim. Thus he took on the Yetzer with all his strength and literally proved to the Yetzer that there is nothing he can do to make him sin. (Please don’t try this at home!) This says the Ben Ish Chai is why Yosef was Mekadesh Sham Shamayim to the point where he got a letter of Hashem’s name inserted into his own.