Now one for the SharePoint people.  You have ran SPDisposeCheck on the dire piece of code your lackey wrote (or youself for that matter) and forgot to run the SPDisposeCheck.  Whoosh loads of entries to validate and change the code for. 

Being good people using TFS for source code and task management you want an entry for each item to check.

Using the built in XML handling facilities from PowerShell this is an easy task.

First run SPDisposeCheck with the output set to XML You will get something like this

<?xml version=”1.0″ encoding=”ibm850″?>
<ArrayOfProblem xmlns:xsi=”
http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
  <Problem>
    <ID>SPDisposeCheckID_110</ID>
    <Module>BinaryJam.CrappyCode.dll</Module>
    <Method>BinaryJam.CrappyCode.ApplicationPages.EditSiteProperties.GetPortalRootWeb</Method>
    <Assignment>local2 := new Microsoft.SharePoint.SPSite(local0).{Microsoft.SharePoint.SPSite}get_RootWeb()</Assignment>
    <Line>0</Line>
    <Notes>Constructor called for Microsoft.SharePoint.SPSite but not assigned. This type should be assigned and subsequently disposed</Notes>
  </Problem>
</ArrayOfProblem>

 

You can Easily read XML files in PowerShell with a simple statement

$xmldata = [xml](Get-Content c:testout.xml)

Accessing the Elements is then just object notation

$xmldata.ArrayOfProblem.Problem

Will return an array of problem items that can be accessed in a foreach $problem and with $problem.ID

So the full code to do this is

 cls
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.Client”)
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.WorkItemTracking.Client”)

[psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer(“http://binaryjam.com:8080/tfs/defaultcollection”)
$WorkItemStore=$tfs.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])

$project=$WorkItemStore.Projects["MyProject"]

$type=$project.WorkItemTypes["Task"]

$xmldata = [xml]

(Get-Content c:testout.xml) foreach ($problem in $xmldata.ArrayOfProblem.Problem){ $item = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem $type $item.Title = “This is an automated entry based on the output from an SPDisposeCheck” $item.AreaPath = “MyProject” $item.IterationPath = “MyProjectIteration 2? $item.Description = “ID : ” + $problem.ID + ” Module : ” + $problem.Module + ” Method : ” + $problem.Method + ” Assignment : ” + $problem.Assignment + ” Line : ” + $problem.Line + ” Notes : ” + $problem.Notes + ” more : http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#” + $problem.ID $item.save() } 

You will want to test this on a TEST tfs project (something I recommend every team have else how are you going to figure out how it works)

Advertisements