Tuesday, April 30, 2013

Find out name of last checked in user programatically using TFS 2010 SDK

Below is a code snippet used in a POC related to automated code review mechanism using FxCop and TFS 2010. The details of how that code review is going to work cannot be revealed now. May be after incorporation of the same into our projects and with permission.

Basics about TFS programming using TFS 2010 SDK

If you are new to TFS and programming TFS system, please read my earlier post about the same. Only change from the earlier post is the location of TFS SDK dlls. In VS 2010 the location is changed to

[Install drive]:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0

The main thing you need to know in programming TFS is, how to create the service object, which is interacting with the different features of TFS, such as work items, source code handling, user management etc...And of course the TFS object model exposed via the sdk dlls.

Getting the details of last checked in user of TFS file / folder

Once you obtained the instance to the corresponding service its the matter of querying for the result or performing the action using suitable method. In this case the TFS Service we need to get is VersionControlServer object and the change sets can be obtained using the  QueryHistory method.

//tfsFilePath should be in the TFS format ie starting with $\<TFS project>\<folder>
        
private string GetLastCheckedInUser(string tfsFilePath)
        {
            IGroupSecurityService grpservice = GetGroupSecurityService();
            VersionControlServer vcs = GetVersionControlServer();
 
            System.Collections.IEnumerable enumerableResult = vcs.QueryHistory(
                path: tfsFilePath,
                version: VersionSpec.Latest,
                deletionId: 0,
                recursion: RecursionType.Full,
                user: "",
                versionFrom: null,
                versionTo: VersionSpec.Latest,
                maxCount: 1,
                includeChanges: true,
                slotMode: true,
                includeDownloadInfo: false,
                sortAscending: false);
            //sortAscending give the last checked in user. enumerableResult is supposed to contain only one item as the maxCount is 1
            foreach (Changeset cs in enumerableResult)
            {
                string comitter = cs.Committer;
                //eventhough cs.CommitterDisplayName property is available, sometime it may return the result as domain\\username.So convert it to displayName using group account service.
                return grpservice.ReadIdentityFromSource(SearchFactor.AccountName, cs.Committer).DisplayName;
            }
            return null;
        }

If you alter the QueryHistory method you can achieve so many things.

Saturday, April 20, 2013

K-MUG - Global Azure Bootcamp @ Athulya, Kochi Infopark on 27 Apr 2013 Saturday

Hope everyone will use this opportunity to reach the cloud world.

Registration: http://globalazurebootcamp-eorg.eventbrite.com/
Details: http://kochibootcamp.azurewebsites.net/
K-MUG page: http://k-mug.org/content/AzureBootCamp.aspx




Monday, April 15, 2013

Where is vjslib.dll located?

Recently I had to prepare for a session related to "Programming for non programmers". I thought of starting with what is computer and programming using some day to day examples. But there is no meaning in talking 1 hour about programming to such an audience who comes from non programming background. To obey the famous "A picture is worth a thousand words" saying, I thought of giving a demo of programming to the audience. But it was tougher than showing a demo in a session about Node.JS or DSL because in normal technical sessions we can show demo using the corresponding language and the related tool. Here which language I should choose? In what tool I can select? Will the surroundings of Visual Studio take the attention of these people from the code window? 

Karel the Robot learns programming

These questions lead me to the famous Karel programming language. The first time, I came to know about Karel the Robot is in the programming paper CS106a of Stanford university syllabus. Don't think that, I studied in Stanford but attended their free online programming course via iTunesU iPad application. But most of the Karel runtime environments are available in Java and Java is not setup in our machines. Googled for online simulators but failed. So decided to go with one .net implementation found in the below site.

http://www.acthompson.net/DotNet/Karel.htm

Location of vjslib.dll

I downloaded the project but it failed during build. The reasons was simple. Its based on Visual J# which was included in earlier days of .Net and slowly removed from Visual Studio suite. It needs a dll named vjslib.dll. I searched for all the folders in my machine related to .Net 2.0 but cannot find. 

Finally google gave me the location of the Microsoft Visual J# Version 2.0 Redistributable Package which contains the vjslib.dll file. Installed it and things started working.Below the location where we can find the vjslib.dll file

C:\Windows\Microsoft.NET\Framework\v2.0.50727\vjslib.dll

Location of Microsoft Visual J# Version 2.0 Redistributable Package 
http://www.microsoft.com/en-us/download/details.aspx?id=4712

I don't think this post will be useful if you are developing a new project. But useful, if you had to maintain or work on legacy code base or ended up in a sample which is created using VJ# assemblies.

Monday, April 8, 2013

Is my SQL query running inside transaction scope?

Just another tip I got during inspecting a query written for a SSIS package. ie from the same incident which I explained in my last post about SQL. There was some part of SQL stored procedure which is not supposed to run in transaction. Since we don’t have transaction suppression support in SQL Server, the alternative took was as follows.

  • Find out whether the stored procedure is running in transaction.
  •  If so throw error. Since this SSIS package is supposed to be called from multiple places we cannot assume that it will not be called without transaction.

How to check for presence of transaction inside SQL SP

Its simple as checking the return value of XACT_STATE() function anywhere in SQL.The code snippet is given below for reference.
IF XACT_STATE() <> 0
BEGIN
    DECLARE @ProcName sysname = OBJECT_NAME(@@PROCID);
    RAISERROR('Stored procedure "%s" cannot be executed in a transaction.', 16, 1, @ProcName)
    RETURN;
END;

Putting it in a SP.

CREATE PROCEDURE dbo.spWillNotRunInTransaction 
AS
BEGIN
IF XACT_STATE() <> 0
BEGIN
    DECLARE @ProcName sysname = OBJECT_NAME(@@PROCID);
    RAISERROR('Stored procedure "%s" cannot be executed in a transaction.', 16, 1, @ProcName)
    RETURN;
END;
SELECT 'I am Independent...'
END
GO

Real time application

One of the real time use I could see is to simulate the .net model transaction suppression as mentioned below inside any stored procedure.


using (TransactionScope txScope =
           new TransactionScope(TransactionScopeOption.Suppress))
    {
        // Code indluding DB queries
        txScope.Complete();
    }



A link I obtained which explains about the unavailability of transaction suppression in SQL Server and how to tackle the situation by alternatives.
http://blogs.msdn.com/b/sqlprogrammability/archive/2008/08/22/how-to-create-an-autonomous-transaction-in-sql-server-2008.aspx

Monday, April 1, 2013

SQL query to get the SP name inside stored procedure.

Recently I had to scratch a SSIS package which was developed by Microsoft consultant. Got some interesting mechanisms while inspecting his stored procedures. Among those, the most noticed one was the technique to get the name of the stored procedure inside the same SP.If we see from the procedure execution point of view this is the current executing SP.
create PROCEDURE usp_WhatIsMyName
AS
BEGIN
    DECLARE @ProcName sysname = OBJECT_NAME(@@PROCID);
    select @ProcName --Returns usp_WhatIsMyName    
END
GO



This seems more like reflection in .Net where we can program the meta data. You may explore more on SQL meta data programming here.