Code of the Day : Cleaning invalid SPConnectedServiceApplication objects
Last modified by sbrickey on 4/16/2013 3:06 PM
Following on the tails of a previous post [Code of the Day : SharePoint Service App cleanup], I found that I had to do some cleanup to the remnants left behind from prior deployments.
The basic background is:
The SharePoint Service Application Architecture is based heavily on the use of WCF services for communication between the requesting computer (often a Web Front End) and the servicing computer (often an app tier server). The topology service (in conjunction with the internal load balancer) is used to help track endpoints, and route them from the requesting code to the servicing code.
When developing a new custom service application, it is understandable that you'd be creating the service apps in the environment to test.
If/when a problem is identified, the code is removed from SP, changes are made, and the new code is redeployed.
Except that service applications tend to leave things behind, unless you are careful about cleaning up after yourself (see previous article).
Here's a SQL query for the SharePoint_Config database that identifies any SPConnectedServiceApplication objects that don't belong to anything.
This can be used to serve two purposes.
1. you can use the IDs with the SharePoint API to clean up the remnants (note that the actual changes/fixing occurs with the API... DO NOT CHANGE THE DATABASE).
2. you can use this to confirm that your custom solutions won't leave the farm in a bad situation.
Cleaning up your orphaned SPConnectedServiceApplication objects
NOTE: this code MAKES CHANGES. IRREVERSIBLE CHANGES. If you are not comfortable running this code, DON'T.