Above is a screenshot from DocAuto’s (IMFO revolutionary, explosively powerful, and surgically accurate) admin tool for SharePoint called SPOrganizer (https://www.docauto.com/products/sporganizer/). What you see is the user interface where you can turn on and off the various Features in a SharePoint Site Collection.
This list of features is driven by a database table stored on the customer’s machine. It looks like this:
Ultimately, a C# method is submitted a list of properties, works through the list, and performs read/update operations on each property. Here is an example:
This Switch-Case block has more than 100 Case statements. That’s a lot of code to write! And if there is a typo in any of the property names, it won’t work correctly. Since we already have this data stored in a database, the solution is to use that data to create the shell of the code we need—automatically. It’s very simple. Here is the SQL code:
SELECT ' case "' + Name + '": currentClientContext.Load(siteCollection, s => s.' + Name + '); break; ' FROM SPProperties WHERE TypeName='SPSiteCollection'
We tell SQL Management Explorer to display the results in text:
And we simply cut the results and paste them into our C# code:
And just like that, we have created perfect no-typo code that would have taken a couple of hours to write by hand. Of course there is more work to do in the customizing of code for each individual property, but many won’t need a change at all.