In 1825 he was translated from the Canongate to Lady Yester's Church, and was appointed a chaplain in ordinary to the king in 1830. He was made principal clerk of the General Assembly in 1827, but unsuccessfully contested the moderatorship with Thomas Chalmers in 1832. In 1834 he became minister of the old church of St. Giles's, Edinburgh, in 1837 Principal of the United College, St Andrews, and in 1840 Dean of the Chapel Royal, Stirling. In 1840, also, he was elected Principal of the University of Edinburgh.