I am having a frustrating issue with a programmatically created web application in which a programmatically deployed solution's web parts are not registered as safe when adding to a programmatically created page.
Background: I am creating a winform application that programmatically creates web applications and sites, deploys solution files, creates pages on the site, and finally adds webparts to the page all on a button click. All of the pieces of code work as expected, except the adding of webparts to the page.
The moment the webpart is added to the page, the webpart is not registered as safe. If I close the winformapp and re-execute my code (this time the web application and site have already been created), the webparts are added to the page as expected and everything works great.
I looked at my web.config file and indeed, the web parts are all registered as safe. I tried running the Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
command before adding the webparts to try and commit the changes made when the solution was deployed without any luck.
I even tried threading my application so the web application / site creation is run in a different thread than the page / web part creation.
At this point I am stumped and have resorted to the intelligent community over at stack overflow!
Does anyone have any idea how to register WebParts as safe when programmatically deploying solutions and creating web applications in SharePoint 2013?
I can happily provide code if needed, but was hoping someone might have a quick and easy solution to this issue.