Umar bin Khattab reported,

The Messenger of Allah ﷺ said, "Deeds are only according to intention, and a person has only what he intended. So whoever migrated for the sake of Allah and His Messenger, his migration was for Allah and His Messenger, and whoever migrated to gain some worldly thing or to marry a woman, his migration was for whatever he migrated toward."

Muslim 4928

Sahih

حدثنا محمد بن رمح بن المهاجر أخبرنا الليث ح و حدثنا أبو الربيع العتكي حدثنا حماد ابن زيد ح و حدثنا محمد بن المثنى حدثنا عبد الوهاب يعني الثقفي ح و حدثنا إسحق بن إبراهيم أخبرنا أبو خالد الأحمر سليمان بن حيان ح و حدثنا محمد بن عبد الله بن نمير حدثنا حفص يعني ابن غياث ويزيد بن هارون ح و حدثنا محمد بن العلاء الهمداني حدثنا ابن المبارك ح و حدثنا ابن أبي عمر حدثنا سفيان كلهم عن يحيى بن سعيد بإسناد مالك ومعنى حديثه وفي حديث سفيان سمعت عمر بن الخطاب على المنبر يخبر عن النبي ﷺ

مسلم 4928صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Muslim
Book
Leadership
Chapter
His saying ﷺ, "Actions are judged by intention." And that it includes military expeditions and other deeds
Numbering
Darussalam 4928
Muslim 4928