Branch TFS Directory and Check-in - c#

Been looking all day for a solution for this, with little to no luck. I have this Windows Forms app in C# - My goal is to branch a directory that is in my TFS repository, make changes to filenames/folder names within the newly created branch if possible, and check code back into TFS with updated folder name/filename structure.
Is there a package or a class that I'm not seeing that would allow me to create and manipulate branches, as well as checking in the code all from a c# winforms app? If so, an example would be much appreciated.

If you are using Client Object Model Reference to manage the Version Control programmatically.
To create a branch, you need to use the "CreateBranch()" method in Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer class.
Creates a branch on the server and checks it in without downloading
the branch to the client.
A sample for you reference:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Client;
using System.Net;
namespace Model.versionControl
{
public static class CreateBranch
{
public static void CreateBranchWithComment()
{
NetworkCredential cre = new NetworkCredential("userName", "password", "domain");
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://TFSServerName:8080/tfs/CollectionName"), cre);
VersionControlServer vcServer = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
int changesetId = vcServer.CreateBranch(#"$/SourceControl/WebSites", "$/SourceControl/WebSites_Branch", VersionSpec.Latest);
new WorkspaceVersionSpec("machineName","domain\userName");
Changeset changeset = vcServer.GetChangeset(changesetId);
changeset.Update();
}
}
}
You could also take a look at this similar question: How to create a new source code branch using TFS API?
As for renaming a file or directory, you could use Workspace.PendRename Method, sample code please refer: How do I move a TFS file with c# API?

Related

what reference I need to add in C# to get LaunchPadApi?

I try to connect to the API (Using C#) that client provided, but it is using LaunchPadApi() I am not sure what assembly I need to add to get this, these are 2 references that were added:
using IO.Swagger.Api;
using IO.Swagger.Client;
I was able to find using IO.Swagger.Client; but I am not able to find using IO.Swagger.Api;
This is a sample code I am not sure how I can get LaunchPadApi defined so "responsePost" method is not recognized as well,
using System;
using System.Diagnostics;
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;
public class responsePostExample
{
public void main()
{
var apiInstance = new LaunchPadApi();
....
try
{
apiInstance.responsePost(body, xAPIKey);
}
....
Install Launchpad in your nuget package manager for the project
Add using Launchpad; to the top of your file

Unable to get Source Generator to do anything

I've been wrestling with Source Generators but there's a lack of tutorials and information that are hurting.
I want to generate some C# classes from a database. Using a T4 template to do this is difficult and problematic, because of issues I'm having with using SQL in T4 templates and similar.
The description of Source Generator is that "The generator can create new C# source files on the fly that are added to the user's compilation. In this way, you have code that runs during compilation. It inspects your program to produce additional source files that are compiled together with the rest of your code." which seems to match what I want.
This seems significantly more promising.
I've created a project, and put a [Generator] into it using this tutorial.
Full source:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace x
{
[Generator]
internal class TestGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
File.Create(#"C:\temp\ITRUNS.TXT");
var sourceBuilder = new StringBuilder(#"
using System;
namespace HelloWorldGenerated
{
public static class HelloWorld
{
public static void SayHello()
{
Console.WriteLine(""Hello from generated code!"");
Console.WriteLine(""The following syntax trees existed in the compilation that created this program:"");
");
Debugger.Launch();
// using the context, get a list of syntax trees in the users compilation
var syntaxTrees = context.Compilation.SyntaxTrees;
// add the filepath of each tree to the class we're building
foreach (SyntaxTree tree in syntaxTrees)
{
sourceBuilder.AppendLine($#"Console.WriteLine(#"" - {tree.FilePath}"");");
}
// finish creating the source to inject
sourceBuilder.Append(#"
}
}
}");
// inject the created source into the users compilation
context.AddSource("helloWorldGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
}
public void Initialize(GeneratorInitializationContext context)
{
}
}
}
I want this to run when I build the solution and create some .cs files
However, it does not run. I put some code in the execute method to create a file, and it does not create the file.
The tutorial says:
Add the source generator from a project as an analyzer and add preview to the LangVersion to the project file like this:
I don't really know what this means. Which project? I tried to download the samples as suggested, but I can't get them to build.
When I examine the code, it's quite hard to understand what they've done that is different.
And what they say in the tutorial about adding the analyzer, doesn't seem to present anywhere in the sample code!
I tried adding the project reference in the csproj that it said to add, however that didn't work - it did create an item within Analyzers but it just has a red - and says 'Ignored' on the tooltip.
I honestly don't know what else I can do to figure out how to get this to work.
I also don't know for sure if it will do what I want - autogenerate a bunch of .cs files with code in that I can use.
I'm going to give it one more go then I'll just write a console application to do it manually instead. Any ideas are welcome.

Visual Studio / RestSharp - Cannot navigate to the symbol under the caret (CS0118)

I have only been coding for about two years now (off and on) but I'm well acquainted with this cryptic error across multiple versions of VS. I know this issue isn't related to just RestSharp.
This 'caret error' has been the bane of my VS experience so that is why I'm here.
Environment:
Microsoft Visual Studio Community 2019 Version 16.8.1
RestClient v4.0.30319
Windows 10 Professional
What I'm trying to do:
I'm following the very simple directions on the RestClient site. I have installed RestClient, added references but when I try to get started I get that dreaded caret error.
None of this is my code aside from the reference to RestClient, which may or may not be appropriate (I don't know).
Screen Snip
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using RestSharp.Authenticators;
using RestClient;
namespace newDawn2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var client = new RestClient("https://api.twitter.com/1.1");
client.Authenticator = new HttpBasicAuthenticator("username", "password");
var request = new RestRequest("statuses/home_timeline.json", DataFormat.Json);
var response = client.Get(request);
}
}
}
Thanks for reading.
using RestClient;
Here you are declaring that you are using the namespace RestClient, not the object.
var client = new RestClient(...
The error is raised here because you are trying to create an instance of the namespace you just declared you were using.
It's uncommon and confusing for an object to share a name with part of its namespace, so you should generally not expect to see a using statement with the same name you use to create an object.
Namespaces are used to separate, organize classes and limit what classes are loaded at compile time.
Classes of the same name can exist in different namespaces.
You could create your own class named RestClient and use it in this project as well(maybe for testing) as long as they existed in different namespaces. The full declaration of the class you are using now is RestSharp.RestClient. You could create a class with the full declaration Facundo.Test.RestClient and use them both in the same project. However, in this case, you could not use using statements and everytime you wanted to instantiate one or the other you would need to fully declare the namespace
RestSharp.RestClient client = new RestSharp.RestClient(...);
or
Facundo.Test.RestClient testClient = new Facundo.Test.RestClient(...);
I would not recommend naming them the same in practice, just trying to illustrate namespaces.

Visual Studio: 'using namespace' not being recognized

I have a Selenium framework that has been smooth sailing ever since it's creation. I implemented the Visual Studio Team Explorer today and after pulling from my remote branch, Intellisense started yelling at me, saying that one of my namespaces does not exist. The lines that it doesn't like are
using PageObjectModel.PageObjects.Maintenance;
and
var SettlementAccountReconciliations = new SettlementAccountReconciliation(_driver);
The SettlementAccountReconciliation is found within the Maintenance directory. Here is my full code for the test class where it does not like the directory:
using NLog.Internal;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using PageObjectModel.ControlObjects;
using PageObjectModel.PageObjects;
using PageObjectModel.PageObjects.Process;
using PageObjectModel.PageObjects.Status;
using System;
using PageObjectModel.PageObjects.Maintenance; // **LINE WITH MAINTENANCE UNDERLINED**
namespace PageObjectModel.Tests.TaskTesting
{
[TestFixture]
class JiraTaskTesting
{
private IWebDriver _driver;
[OneTimeSetUp]
public void SetUp()
{
_driver = new ChromeDriver();
DriverContext.Driver = _driver;
_driver.Manage().Window.Maximize();
var ConfigManager = new ConfigurationManager();
var Login = new Login(_driver);
Login.SignIn(ConfigManager.AppSettings["Username"], ConfigManager.AppSettings["Password"]);
}
[Test]
public void ReportNameStandardizationSettlementAccountReconciliations()
{
// **LINE WITH UNDERLINE**
var SettlementAccountReconciliations = new SettlementAccountReconciliation(_driver);
var Utilities = new Utilities(_driver);
var ReportPopup = SettlementAccountReconciliations.ExportReconciliationReport();
ReportPopup.StartDate.SetValue(DateTime.Now.ToString("MM/dd/yyyy"));
ReportPopup.EndDate.SetValue(DateTime.Now.ToString("MM/dd/yyyy"));
ReportPopup.ExportButton.Click();
Assert.IsTrue(Utilities.ValidateFilePresent("Settlement Account Reconciliation"));
Utilities.DeleteFile("Settlement Account Reconciliation");
}
Also, here is the SettlementAccountReconciliation found within the Maintenance namespace:
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using OpenQA.Selenium.Support.UI;
using System;
namespace PageObjectModel.PageObjects.Maintenance
{
public class SettlementAccountReconciliation
{
private readonly IWebDriver _driver;
public SettlementAccountReconciliation(IWebDriver driver)
{
_driver = driver;
PageFactory.InitElements(driver, this);
var Utilities = new Utilities(driver);
string TradingUrl = Utilities.RefactorUrl("/SettlementAccountReconciliation");
driver.Navigate().GoToUrl(TradingUrl);
WebDriverWait Wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
try
{
Wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[text()='Settlement Account Reconciliations']")));
Console.WriteLine("SettlementAccountReconciliation Page Label Found");
}
catch
{
Console.WriteLine("SettlementAccountReconciliation Page Label Not Found, timing out");
}
}
This is an image of what is shown in my test class for the Directives:
And for the SettlementAccountReconciliation constructor:
I have all of my code in the Page Object Model format, and this is how my file structure is lined out:
I know this question is long winded but to continue, the tests run just fine, and the solution builds like a charm. I just need to figure out how to get the text editor to not think there are issues with my code.
Visual Studio tells me that "The type or namespace name 'Maintenance' does not exist in the namespace PageObjectModel.PageObjects'", but it does.
Any help on this would be greatly appreciated.
EDIT:
I fixed it.
I don't why it worked, but it did.
All I did was renamed the Maintenance folder to Maintenance1, recreated the Maintenance folder, and dragged and dropped the SettlementAccountReconciliation class into the Maintenance folder.
I am guessing there was some random property or setting stored in a temp folder somewhere in Timbuktu that was stored for the existing folder that was reset or deleted when the new folder was created.
Thanks to everyone who put time into helping me out!
I've had this issue before myself. Check the file and folder names in your project directory, the namespaces and assembly name within the properties of your project, and check that, if you've added the project as a reference, that the path to the reference is consistent with the actual path. I have a strong feeling that there is some inconsistency somewhere if you've refactored this at any point and the code you think you are referencing isn't actually what is being referenced.
I renamed the Maintenance folder to Maintenance1, recreated the Maintenance folder, and dragged and dropped the SettlementAccountReconciliation class into the Maintenance folder, and it seems to have fixed the issue.
I am guessing there was some random property or setting stored in a temp folder somewhere in Timbuktu that was stored for the existing folder that was reset or deleted when the new folder was created.

Kentico UserInfoProvider not working as expected in a console app

This code works fine within a Kentico website:
var users = UserInfoProvider.GetUsers();
for (int x = 0; x < users.Count(); x++
{
UserInfo currentUser = users.ElementAt(x);
currentUser.SetValue("AcceptsAlerts", equivalentSubscriber.Status != SubscriberStatus.Unsubscribed);
UserInfoProvider.SetUserInfo(currentUser);
}
When I move the code to a console app, any calls to UserInfoProvider result in the error: "Object type 'cms.usersettings' not found"
For the initial call to get the users, I can do it like this in the console app:
DataSet usersds = new CMS.DataEngine.DataQuery("cms.user.selectall").Execute();
then loop through Table1 of the dataset using the user data:
UserInfo currentUser = new UserInfo(dtUsers.Rows[x]);
All is fine and working, until I come to write the updated user back to the database. I cannot find another way of writing the data apart from calling:
UserInfoProvider.SetUserInfo(currentUser);
Does anyone know another way to save the user data? or to resolve the error. The error is a runtime error and as far as I know, I have referenced everything I need to. The field I am editing is a custom field added to the cmsUser table.
using statements for info:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using CMS;
using CMS.CustomTables;
using CMS.DataEngine;
using CMS.Membership;
Before you start working with Kentico CMS API from an external application make sure you call the following lines:
CMS.DataEngine.ConnectionHelper.ConnectionString = "your connection string";
CMS.Base.SystemContext.WebApplicationPhysicalPath = Application.StartupPath;
CMS.DataEngine.CMSApplication.Init();
Then, you'll be also able to use UserInfoProvider.GetUsers() object query instead of using DataQuery.Execute().
Are you sure you are referencing all necessary assemblies?
Following scenario works on my machine with configuration: Kentico 8.x, Web Application project
Reference in your Console application these assemblies from lib folder
CMS.Base
CMS.DataEngine
CMS.DataProviderSQL
CMS.Membership
Then copy your Connection String from Web Application's web.config to Console Application's App.config.
After that you can use this code to set custom user properties
static void Main(string[] args)
{
var users = UserInfoProvider.GetUsers();
foreach (var user in users)
{
user.SetValue("myTestString", "test");
user.Generalized.SetObject();
}
}
For anyone looking to get a SiteID to use in API calls from an external app such as getting an email template, this might help you. In Kentico 8.1 you can go to Sites > General and get the code name for your site. Then you can do this:
int siteID = CMS.SiteProvider.SiteInfoProvider.GetSiteID("<your site code name>");
Hope it helps!

Categories

Resources