Question

I'm using TFS SDK and I have a method which allows to get latest versions of projects. But when I was call method, it always re-downloading files. This also takes a long time.

I tried that, I get changeSet and I compare particularly items. If item has a change, download it. But this way takes a long time too.

That is my first code without check changeSet

ItemSet items = sourceControl.GetItems(itemPath, VersionSpec.Latest, RecursionType.Full);
foreach (Item item in items.Items)
{                    
    localName = item.ServerItem.ToString();
    localName = localName.Substring(2, (localName.Length - 2)).Replace("/", "\\");

    switch (item.ItemType)
    {
        case ItemType.Any:
            throw new ArgumentOutOfRangeException("ItemType returned was Any; expected File or Folder.");
        case ItemType.File:
            item.DownloadFile("D:\\WORK\\Tries\\"+localName);
            break;
        case ItemType.Folder:
            Directory.CreateDirectory("D:\\WORK\\Tries\\"+localName);
            break;
    }
}  

And that is my new code that checking changesets

ItemSet items = sourceControl.GetItems(itemPath, VersionSpec.Latest, RecursionType.Full);

foreach (Item item in items.Items)
{                    
    localName = item.ServerItem.ToString();
    localName = localName.Substring(2, (localName.Length - 2)).Replace("/", "\\");

    var histories = sourceControl.QueryHistory(itemPath, VersionSpec.Latest, 0, RecursionType.OneLevel, null, null, null, Int32.MaxValue, true, false, true);
    bool check = false;

    foreach (Changeset history in histories)
    {
        foreach (Change change in history.Changes)
        {
            if (change.Item.Equals(item))
                check = true;
        }

    }

    switch (item.ItemType)
    {
        case ItemType.Any:
            throw new ArgumentOutOfRangeException("ItemType returned was Any; expected File or Folder.");
        case ItemType.File:
            if(check)
                item.DownloadFile("D:\\WORK\\Tries\\"+localName);
            break;
        case ItemType.Folder:
            if(!Directory.Exists("D:\\WORK\\Tries\\" + localName))
                Directory.CreateDirectory("D:\\WORK\\Tries\\"+localName);
            break;
    }
}

Does anybody have a suggestion? Thanks.

EDIT: I solved problem this way:

            String ServerFolder = itemPath; // start with "$/ + serverFolder path"
            itemPath = itemPath.Substring(2, (itemPath.Length - 2)).Replace("/", "\\");

            String LocalFolder = @"D:\WORK\"+itemPath;

            WorkingFolder workfolder = new WorkingFolder(ServerFolder, LocalFolder);
            workspace.CreateMapping(workfolder);

            workspace.Get(VersionSpec.Latest,GetOptions.Overwrite);
Was it helpful?

Solution

You are actually calling the wrong method if you are trying to update your workspace to the latest changes. VersionControlServer.GetItems() is a way to ask the server to list what items it has. It will always tell you the same answer.

If you are trying to update the items in your workspace to latest you will want to get the corresponding Workspace object and then call the "Get" method on that object. That will download the files that are out of date from the version you are requesting.

EDIT: Adding some sample code:

// Get a reference to our Team Foundation Server. 
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("http://<yourserver>:8080/tfs/<yourcollection> "));

// Get a reference to Version Control. 
VersionControlServer versionControl = tpc.GetService<VersionControlServer>();

Workspace workspace = versionControl.GetWorkspace("<local path to your workspace>");
workspace.Get();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top