C#: Suppressing Certificate Errors in Code

Normally when access a page over ssl via a browser, you will get a dialog box asking if you wish to proceed if there are any certificate errors. If, however, you are calling the code via the object model, you do not get prompted – instead an exception is thrown. To mitigate for this, it is possible to specify a delegate method to validate certificates – the example below ignores all certificate errors (requires a reference to the System.Security.Cryptography.X509Certificates namespace to match the delegate signature):

public static bool TrustAllCertsValidate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
return true;

To use this and ignore certificate errors, add a reference to the System.Net namespace and add the following line before accessing your web resource (e.g. web service).

ServicePointManager.ServerCertificateValidationCallback += TrustAllCertsValidate;

C#: Using Trace Levels

To add levels of tracing into an application, that generate differing levels of output based on a setting in your applications config file, simply add a switch to the system.diagnostic node of your config file (create the system.diagnostics node if it doesn’t exist). Set the value of this switch to the level of trace required (0-4).

            <add name="MyTraceSwitch" value="4" />

To write trace output using that switch, simply create a TraceSwitch object that references the switch in your config file. Note that this will subsequently contain boolean properties that reflect your trace value specified in your config switch. Note that if your config file does not contain the reference switch, all of these values will be set to false (i.e. tracing will be turned off).
You can then simply perform a Trace.WriteLineIf based on these properties.

TraceSwitch traceSwitch = new TraceSwitch("MyTraceSwitch", "My Trace Switch");
Trace.WriteLineIf(traceSwitch.TraceError, "ERROR: hello world");
Trace.WriteLineIf(traceSwitch.TraceWarning, "WARNING: hello world");
Trace.WriteLineIf(traceSwitch.TraceInfo, "INFO: hello world");
Trace.WriteLineIf(traceSwitch.TraceVerbose, "VERBOSE: hello world");

To capture output, add a tracelistener or use software such as debugview.

SharePoint Conference 2008 (Seattle)

Bill Gates announced hosted SharePoint, targeted at less than 5000 users. A couple of questions that come to my mind are: is there bdc support and can developers deploy solutions / features to hosted SharePoint sites?

There was no mention of the next version of SharePoint. There was some talk about a Silverlight blueprint for SharePoint and I was particularly impressed by the Silverlight faceted search implemented at General Mills.

My personal favourite sessions so far were
Stress Testing Using Visual Studio Team System Test Edition (Presented by Ben Curry).
This was a good walk through on how to implement web and load testing using VSTT.

Rendering Data in SharePoint Using AJAX and LINQ (presented by Steve Peschka).
I was really impressed by the use of LINQ to perform cross list “joins”. There was a particularly cool demo at the end of performing a linq query that joined a SharePoint list to some data in a xml file and on sql server.

TFS: Deleting workspaces for other users

I recently was making some changes on a build server to TFS, but was getting an error due to a collision in mappings of working folders between pre-existing workspaces on that machine. I didn’t have access to the account that the pre-existing workspaces where used under, but needed to delete them anyway. I ended up deleting the workspaces by running the following commands from the VS2005 command prompt:
To list tfs workspaces on a machine for all users run the following command:

tf workspaces /server:<tfsservername> /computer:<currentmachinename> /owner:*

To delete a workspaces for any user, run the following command (this may prompt you for credentials – you must run it as a user who has the TFS admin workspaces permission).

tf workspace /delete "<tfsworkspacename>";<username> /server:<tfsservername>