Iterating Build Definitions (IBuildDefinition) In TFS – Blocked File Error

When programmatically iterating build definitions in TFS (IBuildDefinition), with code similar to the following (at least, under the same constructs):

[csharp]
TeamFoundationServer tfsServer = TeamFoundationServerFactory.GetServer(sTFSName);
IBuildServer buildServer = (IBuildServer)tfsServer.GetService(typeof(IBuildServer));
IBuildDefinition[] buildDefinitions = buildServer.QueryBuildDefinitions(sTeamProjectName);
foreach(IBuildDefinition build in buildDefinitions)
{
// do stuff
}
[/csharp]

You may encounter the following error:

The file you are attempting to save or retrieve has been blocked from this Web site by the server administrators.

To get around this problem, you are going to have to pass some credentials into the TeamFoundationServer constructor.

This can either be done with implicit Windows credentials:

[csharp]
TeamFoundationServer tfsServer = new TeamFoundationServer(“http://tfsserver:8080”, System.Net.CredentialCache.DefaultCredentials);
[/csharp]

or it can be done with explicit credentials:

[csharp]
TeamFoundationServer tfsServer = new TeamFoundationServer(“http:// tfsserver:8080”, new NetworkCredential(“user”, “password”, “domain”));
[/csharp]

Share

Getting SharePoint ULS Log Location Without Reverting To Literals

The ULS logs are pretty great, especially when you have pre-existing, non-development dependent errors occurring that make absolutely freakin no sense. It’s pretty easy to get the path using the SPDiagnosticsService object.

[csharp]

public string ULSPath()
{
try
{
return SPDiagnosticsService.Local.LogLocation;
}
catch (SqlException sqlEx)
{
SPDiagCommonHelper.TraceException(sqlEx);
}
catch (SPException spEx)
{
SPDiagCommonHelper.TraceException(spEx);
}
return string.Empty;
}
[/csharp]

Share

Creating SecurityElement Objects from FileStream’s

When developing security tooling it is frequent that you will want to create new SecurityElement‘s for security object encoding.

To do this, you can use the below CreateSecurityElementSegment method, which simple takes a string parameter representing the path to the file for the FileStream object hydration.

[csharp]
private static SecurityElement CreateSecurityElementSegment(string path)
{
FileStream stream = null;
try
{
stream = new FileStream(path, FileMode.Open, FileAccess.Read);
}
catch (Exception exception)
{
// Do stuff
}
var encodingArray = new[] { Encoding.UTF8, Encoding.ASCII, Encoding.Unicode };
bool flag = false;
SecurityElement element = null;
for (int i = 0; !flag && (i < encodingArray.Length); i++) { try { if (stream != null) { stream.Position = 0L; element = SecurityElement.FromString(new StreamReader(stream, encodingArray[i], true).ReadToEnd()); } flag = true; } catch (Exception exception) { // do stuff } } if (stream != null) { stream.Close(); } if (!flag) { throw new Exception("Security Exception"); } return element; } [/csharp]

Share