Friday, August 20, 2010

How to unshelve when items are locked in other workspace

Ever faced a problem, where you have been given a shelveset but the owner has locked files in his shelveset.

Here is how you could get those files in your workspace without any issue.

Just execute the below command (put the original values in place of placeholders denoted by <>

tf view /server:<Your Server Name> shelveset:"<ShelvesetName>";<Owner in Domain\UserID format> <Path of item you want to unshelve> > <Path Where you want to copy those files>

For more information you can visit MSDN Documentation

Wednesday, February 17, 2010

How to avoid recurring TFS login from Visual Studio

Every time you open Visual studio (with tfs connected mode), it asks you to enter credentials. Even if you are trying to do some command line stuff for tfs, it will prompt you for credentials.

Let's try to make that easy.

Steps (Windows Vista)

  1. Control Panel, User Accounts, User Accounts (again);
  2. Click "Manage your network passwords" ("Tasks" list on the left);
  3. In the "Stored User Names and Password" dialog, click Add;
  4. Type in your Code-Plex (or any other TFS server for that matter)
    1. Add only the host name in the "Log on to" field ();
    2. Type username in the "Domain\FirstName.LastName" format;
    3. Select "A Windows logon credential" radio button.

 

Steps (Windows 7)

  1. Control Panel, User Accounts
  2. Click "Manage your credentials" (on the left);
  3. In the "Stored Credentials for Automatic logon" page, click "Add a windows credential";
  4. On the resultant page Type in your following details -Plex (or any other TFS server for that matter)
    1. Add only the host name in the "Network details" field (tfs server full name);
    2. Type username in the "Domain\FirstName.LastName" format;
    3. Type Password

Steps (Windows XP)

  1. Control Panel, User Accounts, Advanced tab
  2. Click Manage Passwords
  3. Click Add
  4. Enter the server name (tfs server name), user name (Domain\FirstName.LastName and password).

Friday, September 5, 2008

Bridge Design Pattern using C# .Net

Bridge Design Pattern:

[This article has been created from a presentation that was created by my wife-Anita. Special thanks to Anita for facilitating the presentation.]

Definition: Decouple an abstraction from its implementation so that both can vary independently

You will find many articles describing the theory about using Bridge Design Pattern using c# .net. Many articles including wiki has already listed down possible usage of this design pattern. I will simply start with a problem statement and we will see how this problem can be achieved.

Here is the class diagram of my vacation planner (The problem statement) and the code.

Here is the code

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

class Program

{

static void Main(string[] args)

{

TravelManager tm = new TravelManager();

utilize(tm);

FoodManager fm = new FoodManager();

utilize(fm);

//fm.PayBill();

GuideManager gm= new GuideManager();

utilize(gm);

Console.ReadKey();

}

public static void utilize(AgencyManager am)

{

am.Engage();

am.Release();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

abstract class AgencyManager

{

public abstract void Engage();

public abstract void Release();

//public abstract void PayBill();

//public void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

class TravelManager : AgencyManager

{

public override void Engage()

{

TravelController.BookVehicle();

}

public override void Release()

{

TravelController.ReleaseVehivle();

}

//public override void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

class FoodManager : AgencyManager

{

public override void Engage()

{

FoodController.OrderFood();

}

public override void Release()

{

FoodController.FinishFood();

}

}

class GuideManager : AgencyManager

{

public override void Engage()

{

GuideController.HireGuide();

}

public override void Release()

{

GuideController.DeHireGuide();

}

//public override void PayBill()

//{

// Console.WriteLine("Amount debited to a/c");

//}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace Agency

{

public class TravelController

{

public static void BookVehicle()

{

Console.WriteLine("Vehicle booked");

}

public static void ReleaseVehivle()

{

Console.WriteLine("Vehicle Released");

}

}

public class FoodController

{

public static void OrderFood()

{

Console.WriteLine("Food ordered.");

}

public static void FinishFood()

{

Console.WriteLine("Food finished.");

}

}

public class GuideController

{

public static void HireGuide()

{

Console.WriteLine("Guide hired.");

}

public static void DeHireGuide()

{

Console.WriteLine("Guide dehired.");

}

}

}

As listed down in the above code and class diagram I have a simple abstract class and 3 implementations. Now the problem starts when I start exploring the application more. Let's say I have to add a new method PayBill which are required only for two implementations i.e for TravelManager and GuideManager. However I want to keep the third implementation FoodManager away from this method. Since all of them are implementing the same abstract class, I cannot add this method at the abstract level since in that case it will compulsory for FoodManager to implement that method which I don't want. The other way is to add the method at individual classes. However that will invite unnecessary duplication of code.

There can be someother solution to this problem but we will see how to handle such situation using the Bridge Design Pattern.

So here is the class diagram and the resulting code which shows us how we can resolve this issue.

And here is the code

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

class Program

{

static void Main(string[] args)

{

PaidAgencies agency = new PaidAgencies();

agency.AgencyManager = new TravelManager();

agency.Engage();

agency.Release();

agency.PayBill();

agency.AgencyManager = new GuideManager();

agency.Engage();

agency.Release();

agency.PayBill();

AddOnAgecies freeagency = new AddOnAgecies();

freeagency.AgencyManager = new FoodManager();

freeagency.Engage();

freeagency.Release();

freeagency.Reimburse();

Console.ReadKey();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

/// Abstraction

/// </summary>

public abstract class AgencyAbstraction

{

AgencyManager agencyManager;

public AgencyManager AgencyManager

{

get { return agencyManager; }

set { agencyManager = value; }

}

public abstract void Engage();

public abstract void Release();

}

/// <summary>

/// Refined abstraction

/// </summary>

public class PaidAgencies : AgencyAbstraction

{

public override void Engage()

{

AgencyManager.Engage();

}

public override void Release()

{

AgencyManager.Release();

}

public void PayBill()

{

Console.WriteLine("Amount Debited to Account");

}

}

/// <summary>

/// Refine abstraction 2

/// </summary>

public class AddOnAgecies : AgencyAbstraction

{

public override void Engage()

{

AgencyManager.Engage();

}

public override void Release()

{

AgencyManager.Release();

}

public void Reimburse()

{

Console.WriteLine("Amount credited to Account");

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

/// This is implementor

/// </summary>

public abstract class AgencyManager

{

public abstract void Engage();

public abstract void Release();

}

/// <summary>

/// This is concrete implementor

/// </summary>

public class TravelManager : AgencyManager

{

public override void Engage()

{

TravelController.BookVehicle();

}

public override void Release()

{

TravelController.ReleaseVehicle();

}

}

/// <summary>

/// This is concrete implementor

/// </summary>

class FoodManager : AgencyManager

{

public override void Engage()

{

FoodController.OrderFood();

}

public override void Release()

{

FoodController.FinishFood();

}

}

/// <summary>

/// This is concrete implementor

/// </summary>

class GuideManager : AgencyManager

{

public override void Engage()

{

GuideController.HireGuide();

}

public override void Release()

{

GuideController.DeHireGuide();

}

}

}

using System;

using System.Collections.Generic;

using System.Text;

namespace BridgeDesignPattern

{

/// <summary>

///

/// </summary>

public class TravelController

{

public static void BookVehicle()

{

Console.WriteLine("Vehicle booked");

}

public static void ReleaseVehicle()

{

Console.WriteLine("Vehicle Released");

}

}

public class FoodController

{

public static void OrderFood()

{

Console.WriteLine("Food ordered");

}

public static void FinishFood()

{

Console.WriteLine("Food finished");

}

}

public class GuideController

{

public static void HireGuide()

{

Console.WriteLine("Guide hired");

}

public static void DeHireGuide()

{

Console.WriteLine("Guide dehired");

}

}

}

As you can see now we can add methods at the Abstraction level whereas clients are using the refined abstraction. This way we are free to make changes at the abstract level as well as at the implementation level.

I Hope this explains the bridge design pattern clearly. Please feel free to comment on this article in case you have any queries/suggestions/comments. I have this source code along with a presentation in a zip and I can provide the same free of cost. Email me at SubhashD987[AT]mail.com

Enjoy programming…

Friday, August 8, 2008

Create new folder, Upload document to sharepoint site – Maintain metadata

There was one requirement which I was working on recently. It was very simple to create a user control wherein the user should be able to select the file and upload it to a WSS 3.0 Document Library. Also the second part was user should be able to create folders in the document library and upload document inside the folder.

 

With this very simple task was stated to me, I was very happy to answer my boss on the spot that the, I have the design ready. But boss (being a boss !) told me, Hold On! The requirement also states that the user should be able to see his/her name after uploading document and creating folder. For this I had a very straight forward solution which is inbuild for WSS 3.0. I used following code to upload the document.

    /// <summary>

/// Uploads Document to the Site

    /// foldername: Name of folder to which the document is to be uploaded. (Eg. "Shared Documents")

    /// documentFIleName: FileName of the file to be uploaded. Typically FileUpload control's output after formatting gets in here (Eg. "C:\MyFile.Doc")

/// </summary>

public void UploadDocumentToSite(string foldername, string documentFileName)

{

SPSite site = SPControl.GetContextSite(HttpContext.Current);

SPWeb myWeb = site.AllWebs["123"]; //123 is the site id.

 

try

{

site.AllowUnsafeUpdates = true;

myWeb.AllowUnsafeUpdates = true;

 

SPFolder destFolder = myWeb.GetFolder(foldername);

SPUser user = myWeb.Users["Domain\\UserName"];

 

destFolder.Files.Add(documentFileName, content, user, user, DateTime.Now, DateTime.Now);

         //The above overload used will help me to maintain the metadata such as created by and modified by fields.

 

}

catch (Exception ex)

{

//Log the error here

}

finally

{

//Dispose the web and site

}

}

 

So far so good. The above code perfectly uploads the document to the document library and also maintains the metadata such as Created by/Modified By fields.

The challenge starts later. Now the same requirement had a provision that user should be able to create folders in the document library. Creating folder is simple

SPFolder destFolder = myWeb.GetFolder(foldername);

destFolder.SubFolders.Add(folderName);

Now the problem starts. The above SubFolders.Add method does not have any overload where I can pass on SPUser. Apparently what would happen is that the document gets uploaded with some other userid (or system account which was impersonated) but not with the specific userid I wanted.

Then I again searched on the web and somewhere down the line I got a hint which worked for me like anything. The below is the way to go about it.

SPFolder newFolder = destFolder.SubFolders.Add(folderName);

newFolder.Item["Modified By"] = user.ID;

newFolder.Item["Created By"] = user.ID;

newFolder.Item.Update();

 

Wow… This was so simple but still so important to know.. I am very thankful to the guy who floated this on the web. (Unfortunately I did not bookmarked the url, but I commented down there)..

Go ahead and try this out if this works for you.. if it does, keep the work spreading…

Enjoy Life .. Enjoy Programming.. ..

Let me know if you need any assistance on such issues…