The Twelfth Doctor existed as a virtual construct in a world created by the Monks, so that they could plan the perfect invasion of Earth.
The Doctor shared the history and personality of his non-virtual counterpart, only parting with that history shortly after the events aboard Chasm Forge. He was a perfect duplicate and, like the rest of the people living within the Shadow World, he believed himself to be real.
He was sent by the Pope to read the Veritas, a document where anyone who read it killed themselves. He read it, and realized that he was part of a simulation created by aliens that wished to invade Earth. Before being deleted, he used the sonic sunglasses to send an email to his real world counterpart to warm him of the impending invasion. (TV: Extremis)