I am using Visual Studio 2013 and wrote a TFS 2015 server side plugin. Created a local TFS 2015 environment and checked-in files to test it, I found that it works as expected.
I want to deploy my plugin: Following the instructions on internet I changed my plugin code's output path to : ..............\Program Files\Microsoft Team Foundation Server 14.0\Application Tier\Web Services\bin\Plugins. So, my plugin.dll and plugin.pdb files are in this location.
After this step; I am stuck, I tried to go to Team Explorer -> Settings -> Source Control(under Team Project)-> Check-in Policy -> Add but I wasn't able to find my file.
I need help to deploy my plug-in.
Your server side plugin will not show up in the Check-in Policy Add dialog. It will however execute when the Check In button is hit for every client that connects to the TFS server where the plugin is deployed. Based on the plugin code it will either approve or deny the check-in. In case it denies the check-in you can supply a message for the user about what to fix.
Here is an example that just rejects if the code reviewer is claimed to be GOD. You can also check the comment section and look for required elements if you like.
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.TeamFoundation.Build.Server;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Server;
using System.Collections.Generic;
using Microsoft.TeamFoundation.VersionControl.Server;
namespace TFSPlugin
{
public class FittingSoftwarePlugin : ISubscriber
{
public string Name { get { return this.GetType().Name; } }
public SubscriberPriority Priority { get { return SubscriberPriority.Normal; } }
public Type[] SubscribedTypes() { return new[] { typeof(CheckinNotification) }; }
public EventNotificationStatus ProcessEvent(IVssRequestContext requestContext, NotificationType notificationType, object notificationEventArgs,
out int statusCode, out string statusMessage, out ExceptionPropertyCollection properties)
{
statusCode = 0;
properties = null;
statusMessage = String.Empty;
try
{
var checkinNotificationArgs = notificationEventArgs as CheckinNotification;
if (notificationType == NotificationType.DecisionPoint && checkinNotificationArgs != null)
{
var codeReviewer = checkinNotificationArgs.CheckinNote.Values.FirstOrDefault(v => v.Name.Equals("Code Reviewer"));
if (codeReviewer!=null && codeReviewer.Value.Equals("GOD", StringComparison.InvariantCultureIgnoreCase))
{
statusMessage = "GOD cannot be used as a code reviewer as he is not trustworthy!";
return EventNotificationStatus.ActionDenied;
}
}
}
catch (Exception e)
{
// Log error
}
return EventNotificationStatus.ActionPermitted;
}
}
}
The check-in policy has to be deployed to the local machines of anyone who is going to use it.
Check-in policies are not the same thing as server-side plugins.
Related
I'm working on a project team and our application is in TFS. I'm attempting to determine how many lines of code each team member is responsible. In TFS, I'm aware of the Annotate feature in the Visual Studio interface which allows you to see who last modified each line of code so I know TFS has this information.
I've written a small console app which accesses my TFS project and all its files, but I now need to programmatically access annotations so I can see who the owner of each line is. Here is my existing code:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
public class Program
{
static void Main(string[] args)
{
var credentials = new NetworkCredential(username, password, domain);
var server = new TfsTeamProjectCollection(new Uri(serverUrl), credentials);
var version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;
var items = version.GetItems(projectPath, RecursionType.Full);
var fileItems = items.Items.Where(x => x.ItemType == ItemType.File);
foreach (var fileItem in fileItems)
{
var serverItem = fileItem.ServerItem;
//TODO: retrieve and parse annotations
}
}
}
I can't seem to figure out how to retrieve annotations once I have the TFS item. This link explains how to do it by calling TFPT, but after implementing it (tfpt annotate /noprompt <filename>), you are only give the last changeset and code per line, not the owner.
I also found a Microsoft.TeamFoundation.VersionControl.Server namespace that has an Annotation class. I installed TFS on my machine to have access to that DLL, but it doesn't seem like it is of any help to this problem.
How can you programmatically access TFS annotations to determine the owner of a line of code for a file?
You may have to query the branch when a Item's change type is Branch.
For a simple example, there is a scenario
$/Project
/Main`
/a.txt
/Develop
/a.txt (branched from main)
When you query the history of $/project/Develop/a.txt, you can also get the history of $/project/Main/a.txt using following code
void GetAllHistory(string serverItem)
{
var changesets=vcs.QueryHistory(serverItem,
Microsoft.TeamFoundation.VersionControl.Client.VersionSpec.Latest,
0,
Microsoft.TeamFoundation.VersionControl.Client.RecursionType.None,
null,
new Microsoft.TeamFoundation.VersionControl.Client.ChangesetVersionSpec(1),
Microsoft.TeamFoundation.VersionControl.Client.VersionSpec.Latest,
int.MaxValue,
true,
false);
foreach (var obj in changesets)
{
Microsoft.TeamFoundation.VersionControl.Client.Changeset cs = obj as Microsoft.TeamFoundation.VersionControl.Client.Changeset;
if (cs == null)
{
return;
}
foreach (var change in cs.Changes)
{
if (change.Item.ServerItem != serverItem)
{
return;
}
Console.WriteLine(string.Format("ChangeSetID:{0}\tFile:{1}\tChangeType:{2}", cs.ChangesetId,change.Item.ServerItem, change.ChangeType));
if ((change.ChangeType & Microsoft.TeamFoundation.VersionControl.Client.ChangeType.Branch) == Microsoft.TeamFoundation.VersionControl.Client.ChangeType.Branch)
{
var items=vcs.GetBranchHistory(new Microsoft.TeamFoundation.VersionControl.Client.ItemSpec[]{new Microsoft.TeamFoundation.VersionControl.Client.ItemSpec(serverItem, Microsoft.TeamFoundation.VersionControl.Client.RecursionType.None)},
Microsoft.TeamFoundation.VersionControl.Client.VersionSpec.Latest);
GetAllHistory(items[0][0].Relative.BranchToItem.ServerItem);
}
}
}
}
I am trying to work out how, in my Visual Studio Extension, I can work out which of the currently loaded projects are in TFS and then the TFS server uri and location for each of these projects.
I would like to be able to do something like this:
foreach (EnvDTE.Project project in dte.Solution.Projects)
{
if (project.IsInTFS) {
var uri = project.TFSUri; // http://tfsserver:8080/tfs
var location = project.TFSLocation; // $\TeamProject\Project
}
}
Can anyone help me?
Thank you.
You can use the SourceControl2 interface to accomplish this in the following way:
foreach (Project project in dte.Solution.Projects)
{
if (dte.SourceControl.IsItemUnderSCC(project.FileName))
{
var bindings = project.GetSourceControlBindings();
var uri = bindings.ServerName;
var location = bindings.ServerBinding;
// your logic goes here
}
}
Where GetSourceControlBindings is an extension method:
public static SourceControlBindings GetSourceControlBindings(this Project project)
{
SourceControl2 sourceControl = (SourceControl2)project.DTE.SourceControl;
return sourceControl.GetBindings(project.FullName);
}
Note: you will have to a add reference to the EnvDTE80.dll assembly.
If you're interested exclusively in TFS source control, then you should add one more condition:
if (bindings.ProviderName == "{4CA58AB2-18FA-4F8D-95D4-32DDF27D184C}")
Where that magic guid specifies the Team Foundation Source Control Provider.
I am trying to customize some lists for SharePoint Online and since I am new to the subject I do not know how to connect to the service.
When I use NAPA and from the cloud use the option "Edit in Visual Studio", I am prompted for credentials automatically when the project opens.
However, when I start from bottom-up, i.e. open a new project in Visual Studio, add all necessary dlls, this part of code throws an error (it is an authentication issue):
ClientContext context = new ClientContext("https://MYURL.sharepoint.com/n/");
context.ExecuteQuery();
I am using Microsoft.SharePoint.Client;
The error message:
An unhandled exception of type 'System.Net.WebException' occurred in Microsoft.SharePoint.Client.dll
Additional information: The remote server returned an error: (403) Forbidden.
I think I am missing part of the code which is responsible for authentication and which in case of NAPA app is hard-coded.
How can I authenticate to SharePoint Online? (it is enough if my code runs just once, it's not an app, I don't want to package it and publish)
I am guessing it has something to do with http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.sharepoint.remote.authentication.aspx, but that's as far as I got.
How to authenticate against SharePoint Online using the managed CSOM
The CSOM for SharePoint 2013 introduces the SharePointOnlineCredentials class that allows to perform an active authentication to SharePoint Online.
Example
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the URL of the SharePoint Online site:");
string webUrl = Console.ReadLine();
Console.WriteLine("Enter your user name (format: username#tenant.onmicrosoft.com)");
string userName = Console.ReadLine();
Console.WriteLine("Enter your password.");
SecureString password = GetPasswordFromConsoleInput();
using (var context = new ClientContext(webUrl))
{
context.Credentials = new SharePointOnlineCredentials(userName,password);
context.Load(context.Web, w => w.Title);
context.ExecuteQuery();
Console.WriteLine("Your site title is: " + context.Web.Title);
}
}
private static SecureString GetPasswordFromConsoleInput()
{
ConsoleKeyInfo info;
//Get the user's password as a SecureString
SecureString securePassword = new SecureString();
do
{
info = Console.ReadKey(true);
if (info.Key != ConsoleKey.Enter)
{
securePassword.AppendChar(info.KeyChar);
}
}
while (info.Key != ConsoleKey.Enter);
return securePassword;
}
}
I am trying to create a custom workflow in Visual Studio 2010 for SharePoint 2010 and have run into a problem. I have figured out how to deploy the workflow to the SharePoint site, but executing it results in an error. However, the error message is completely non-descriptive, so I want to find out if there is a way to execute it from Visual Studio so I can see where it fails, and possibly why.
I'm trying to simply create a new subsite based on a given ListItem.Title information.
How is it you go about debugging?
For reference, here is my code
class CreateSubsite : System.Workflow.ComponentModel.Activity
{
protected override System.Workflow.ComponentModel.ActivityExecutionStatus
Execute(System.Workflow.ComponentModel.ActivityExecutionContext executionContext)
{
createSite();
return System.Workflow.ComponentModel.ActivityExecutionStatus.Closed;
}
public void createSite()
{
using (SPSite currentSite = SPContext.Current.Site)
{
using (SPWeb currentWeb = SPContext.Current.Web)
{
SPList currentList = SPContext.Current.List;
SPListItem currentListItem = SPContext.Current.ListItem;
WorkflowContext workflow = new WorkflowContext();
SPSite parentSite = new SPSite(workflow.CurrentWebUrl);
SPWeb newSite = currentSite.AllWebs.Add(
currentListItem.Title.Replace(" ", "_"),
currentListItem.Title,
String.Empty, currentWeb.Language, "CI Template", false, false
);
}
}
}
}
Try to remove Using keyword from your code .You should not dispose your SPSite and SPWeb when you use SPContext because disposing of that object might actually break the workflow as it may still need a reference to that object for later use.
just rewrite your code without use using
public void createSite() {
SPSite currentSite = SPContext.Current.Site
SPWeb currentWeb = SPContext.Current.Web
//.... Rest of your code
Hope that help
Regards.
What is the alternative for DomainProjectPicker if I want to select a server plus its projects? I am aware of a new class called TeamProjectPicker, but that doesn't help me. Anyone know how to select the server from this type of dialog?
Thanks,TS.
As far as I can figure it out it's more or less the same as the DomainProjectPicker.
Here's a code sample of how I was working with it:
if (tpp.ShowDialog() == DialogResult.OK)
{
try
{
//here you get the TfsTeamProjectCollection (the TeamFoundationServer class is also obsolete)
TfsTeamProjectCollection tfsProj = tpp.SelectedTeamProjectCollection;
//here you authenticate
tfsProj.Authenticate();
}
etc...
You can use the TeamProjectPicker class from Microsoft.TeamFoundation.Client.dll. There is a great blog post that describes how to wrangle the dialog: Using the TeamProjectPicker API in TFS 2010
Here's the code sample for selecting multiple team projects:
Application.EnableVisualStyles(); // Makes it look nicer from a console app.
//"using" pattern is recommended as the picker needs to be disposed of
using (TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.MultiProject, false))
{
DialogResult result = tpp.ShowDialog();
if (result == DialogResult.OK)
{
System.Console.WriteLine("Selected Team Project Collection Uri: " + tpp.SelectedTeamProjectCollection.Uri);
System.Console.WriteLine("Selected Projects:");
foreach(ProjectInfo projectInfo in tpp.SelectedProjects)
{
System.Console.WriteLine(projectInfo.Name);
}
}
}
If you don't care about the project and only want the user to be able to select a server and collection, use TeamProjectPickerMode.NoProject in the constructor.