Search code examples
c#tfstfs-sdk

Error in checkout and checkin files in TFS programatically despite having permissions


I want to programatically connect to TFS and be able to checkout and checkin the files. For that purpose, I am using the following code (some private information omitted), however, I get the "not having sufficient permissions error", I have checked with the administrator and he has given me both read and write permissions, can anyone please help me. Here's the code:

using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace CodeGeneration
{
    public class CheckInTFS
    {
        public static void ProcessFile()
        {
            var tfs = new TfsTeamProjectCollection(new Uri("http://tfs"));
            var versionControlServer = tfs.GetService<VersionControlServer>(); 
            var workspace = versionControlServer.GetWorkspace(@"D:\Test");

            #region Checkout File

            var file = @"D:\EnumGeneration.cs";
            workspace.PendEdit(file);
            var pendingChange = workspace.GetPendingChanges();

            #endregion

            #region Checkin File

            workspace.CheckIn(pendingChange, "Test Comment!");
            #endregion
        }
    }
}

The error which I receive is this:

enter image description here

Also, I have looked at the permissions from This MS Page and I have GENERIC_READ and GENERIC_WRITE permissions.


Solution

  • I found this sample, try with this and let me know if you still have persmisson problems when adapting and running this

    using System;
    using System.Collections.ObjectModel;
    using Microsoft.TeamFoundation.Client; 
    using Microsoft.TeamFoundation.Framework.Common;
    using Microsoft.TeamFoundation.Framework.Client;
    
    namespace TfsApplication
    {
        class Program
        {
            static void Main(String[] args)
            {
                // Connect to Team Foundation Server
                //     Server is the name of the server that is running the application tier for Team Foundation.
                //     Port is the port that Team Foundation uses. The default port is 8080.
                //     VDir is the virtual path to the Team Foundation application. The default path is tfs.
                Uri tfsUri = (args.Length < 1) ? 
                    new Uri("http://Server:Port/VDir") : new Uri(args[0]);
    
                TfsConfigurationServer configurationServer =
                    TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);
    
                // Get the catalog of team project collections
                ReadOnlyCollection<CatalogNode> collectionNodes = configurationServer.CatalogNode.QueryChildren(
                    new[] { CatalogResourceTypes.ProjectCollection },
                    false, CatalogQueryOptions.None);
    
                // List the team project collections
                foreach (CatalogNode collectionNode in collectionNodes)
                {
                    // Use the InstanceId property to get the team project collection
                    Guid collectionId = new Guid(collectionNode.Resource.Properties["InstanceId"]);
                    TfsTeamProjectCollection teamProjectCollection = configurationServer.GetTeamProjectCollection(collectionId);
    
                    // Print the name of the team project collection
                    Console.WriteLine("Collection: " + teamProjectCollection.Name);
    
                    // Get a catalog of team projects for the collection
                    ReadOnlyCollection<CatalogNode> projectNodes = collectionNode.QueryChildren(
                        new[] { CatalogResourceTypes.TeamProject },
                        false, CatalogQueryOptions.None);
    
                    // List the team projects in the collection
                    foreach (CatalogNode projectNode in projectNodes)
                    {
                        Console.WriteLine(" Team Project: " + projectNode.Resource.DisplayName);
                    }
                }
            }
        }
    }
    

    taken from here

    http://msdn.microsoft.com/en-us/library/bb286958.aspx