Build Customization using Visual Studio 2010 (TFS 2010) - Creating Custom Activity

Posted by: Gouri Sohoni , on 6/10/2010, in Category VSTS & TFS (Azure DevOps)
Views: 104571
Abstract: In this article we will discuss how to create a custom activity using code and make it available in the toolbox with the help of activity library.
In last 2 articles Build Customization using Visual Studio 2010 and Build Customization using Visual Studio 2010 – Part II  we discussed the new features provided for Build Management in Visual Studio 2010, how to customize the default template and use it in build definition and how to create a new activity using activity library (with xaml).
In this article we will discuss how to create a custom activity using code and make it available in the toolbox with the help of activity library.



Follow these steps:
Step 1: Create a project for activity library


NewProject
Step 2: Add new item named Code Activity
AddNewItem
Step 3: Add references to the following assemblies –
Microsoft.TeamFoundation.Build.Client.dll from <drive name>\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferencedAssemblies\V2.0
and
Microsoft.TeamFoundation.Team.WorkFlow.dll from <drive name>\Program Files\Microsoft Visual Studio 20.0\Common7\IDE\PrivateAssemblies (Take care of processor related Program Files folder)
Step 4: Provide the attributes of BuildActivity(HostEnvironmentOption.All)  and BuildExtension(HostEnvironmentOption.All) to the class.
Step 5: Change the target framework from .NET Framework 4 Client Profile to .NET Framework 4. By default the target framework is set to client profile which does not support the classes we are using.
Step 6: Now we will add the actual code. For this code we will use the following namespaces

-Microsoft.TeamFoundation.Build.Workflow.Tracking
-Microsoft.TeamFoundation.Build.Workflow.Activities
-Microsoft.TeamFoundation.Build.Client
The code looks as follows:
 
C#
 
[BuildActivity(HostEnvironmentOption.All)]
[BuildExtension(HostEnvironmentOption.All)]
public sealed class CodeActivity1 : CodeActivity
{
    public InArgument<string> Text { get; set; }
 
 
    public InArgument<IBuildDetail> currentBuild { get; set; }
    protected override void Execute(CodeActivityContext context)
    {
        string buildNumber = "Build Number: " + this.currentBuild.Get(context).BuildNumber;
        context.Track(new BuildInformationRecord<BuildMessage>()
        {
            Value = new BuildMessage()
            {
                Importance = BuildMessageImportance.Normal,
                Message = "This is a custom information node",
            },
        });
    }
}
 
 
Step 7: Build activity library and Create a custom definition for Build by using Team Explorer.
Step 8: Create a copy of the existing build process template for doing changes
NewBuildProcess
Step 9: Create a branch for the custom template and map it to the location where the activity library exists

BrowseForFolder
Step 10: Check in pending changes for branch. Check out the branch for edit. It will be available in the folder for activity library.
Step 11: Add the xaml as existing item to activity library project (make sure to view xaml files). We require this step to view the newly created activity in the toolbox.
 
Step 12: Now you can drag and drop the custom code activity onto the designer
Sequence
Step 13: Open the code and add namespace xmlns:<ns>="clr-namespace:<ActivityNamespace>;assembly=<AssemblyName>" to build process root element. Add the activity <ns>:<ActivityName> > to build process. (Use the same namespace provided in previous step)
Step 14: Save the file in location which is mapped to the branch. Check in activity library by including dll for it in the project. This makes the dll available on server side which is required for the custom build definition.
Step 15: Merge the branch to the original template and check in. Change the build controller to specify the path for custom assembly. It should be the same where the activity library dll is check-in. (If there are multiple custom assemblies it is better to create a folder in BuildProcessTemplate and provide it as path)
Step 16: Queue the build and check if the code activity is working.
CustomBuild

The build number can be seen when the build gets executed.
Conclusion: This article was the last part of the Build Customization Series. In this article we saw how to create a custom activity using code and make it available in the toolbox with the help of activity library.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Gouri is a Trainer and Consultant on Azure DevOps and Azure Development. She has an experience of three decades in software training and consulting. She is a graduate from Pune University and PGDCA from Pune University. Gouri is a Microsoft Most Valuable Professional (MVP)  - Developer Technologies (Azure DevOps), Microsoft Certified Trainer (MCT) and a Microsoft Certified Azure DevOps Engineer Expert. She has conducted over 150 corporate trainings on various Microsoft technologies. She is a speaker with Pune User Group and has conducted sessions on Azure DevOps, SQL Server Business Intelligence and Mobile Application Development. Gouri has written more than 75 articles on Azure DevOps, TFS, SQL Server Business Intelligence and SQL Azure which are published on www.sqlservercurry.com and www.dotnetcurry.com. You can connect with her on LinkedIn.


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Rahul on Friday, August 13, 2010 11:35 AM
How do you perform step 13.
where to add the namespace and activity.
Comment posted by Daniel Walker on Tuesday, September 14, 2010 8:07 AM
I echo the question above:

"Step 13: Open the code and add namespace xmlns:<ns>="clr-namespace:<ActivityNamespace>;assembly=<AssemblyName>" to build process root element. Add the activity <ns>:<ActivityName> > to build process. (Use the same namespace provided in previous step)"

How do you do this? What are <ns>, <ActivityNamespace> and <AssemblyName>? This is very confusing.

Good tutorial up to this point though...
Comment posted by Gouri Sohoni on Wednesday, December 15, 2010 10:11 PM
Hi Rahul and Daniwel ,
Sorry for late reply. You open the process template which we had mapped to local folder in code view and do the necessary changes.
Comment posted by Koefi on Thursday, December 16, 2010 12:08 AM
I have the same question of Daniel. I am not follow step 13 and i not have Microsoft.TeamFoundation.Team.WorkFlow.dll on system. Whats missing'/
Comment posted by John Doe on Wednesday, March 9, 2011 12:25 PM
[Comment edited due to its abusive nature**********], you can't even copy-paste without errors!

Comment posted by David on Friday, March 25, 2011 8:35 AM
To answer some of the questions:
Microsoft.TeamFoundation.Team.WorkFlow.dll is located here: "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\Microsoft.TeamFoundation.Build.Workflow.dll"
(remove the " ( x86)" from program files folder for non 64 bit systems)

For step 13, I right clicked on the xaml file in the solution, and clicked view code.
Then on the first line I added this to the end of the tag (inside of the tag):
xmlns:CustomWorkflowActivities="clr-namespace:CustomWorkflowActivities;assembly=CustomWorkflowActivities"
Where "CustomWorkflowActivities" is my assembly name, and namespace.
the "xmlns:CustomWorkflowActivities" could be xmlns:anythingatall, as long as you use the anythingatall when you replace the namespace on your task below.

After that, find your task and update the namepsace (the part to the left of the colon ":") to be the anythingatall you did up top. So my line is:
<CustomWorkflowActivities:RenameFileActivity DestFileName="[Path.Combine(CopyToIISPath, RenameFileTo)]" sap:VirtualizedContainerService.HintSize="200,22" SourceFileName="[Path.Combine(CopyToIISPath, RenameFileFrom)]" />

Where my activity name is "RenameFileActivity" (I used this to rename a web.release.config file to web.config file when the build runs)

Hope this helps.
David
Comment posted by LuWa on Wednesday, May 18, 2011 2:48 AM
Hi.

I got following error: Ein Teil des Pfades "C:\Windows\system32\$(SourceDir)\" konnte nicht gefunden werden.
What can i do?
Comment posted by Lautaro on Wednesday, April 18, 2012 11:11 AM
Hi, I don't understand step 14. How can I check in the activity library?
Comment posted by Gouri Sohoni on Thursday, April 19, 2012 12:59 AM
Hi Lautaro,
You can right click on the .dll file (in solution explorer) and select the option of "Include in Project" and later check in to the source control
Comment posted by Rishi on Wednesday, October 10, 2012 10:10 AM
Hello

i have tried everything you said. but, i'm getting a strange error. it is compiling.

NewBuildProcessTemplate.xaml (529): Could not find type 'Microsoft.TeamFoundation.TestImpact.BuildIntegration.BuildActivities.GetImpactedTests' in assembly 'Microsoft.TeamFoundation.TestImpact.BuildIntegration'.

BuildActivities is my library project which has NewBuildProcessTemplate.xaml.

i'm also getting the below error as other errors

TF270015: 'MSBuild.exe' returned an unexpected exit code. Expected '0'; actual '1'.


could you please help me.
Comment posted by Rishi on Wednesday, October 10, 2012 10:17 AM
Hello

i have tried everything you said. but, i'm getting a strange error. it is compiling.

NewBuildProcessTemplate.xaml (529): Could not find type 'Microsoft.TeamFoundation.TestImpact.BuildIntegration.BuildActivities.GetImpactedTests' in assembly 'Microsoft.TeamFoundation.TestImpact.BuildIntegration'.

BuildActivities is my library project which has NewBuildProcessTemplate.xaml.

i'm also getting the below error as other errors

TF270015: 'MSBuild.exe' returned an unexpected exit code. Expected '0'; actual '1'.


could you please help me.
Comment posted by Rishi on Wednesday, October 10, 2012 11:43 AM
Hello

i have tried everything you said. but, i'm getting a strange error. it is compiling.

NewBuildProcessTemplate.xaml (529): Could not find type 'Microsoft.TeamFoundation.TestImpact.BuildIntegration.BuildActivities.GetImpactedTests' in assembly 'Microsoft.TeamFoundation.TestImpact.BuildIntegration'.

BuildActivities is my library project which has NewBuildProcessTemplate.xaml.

i'm also getting the below error as other errors

TF270015: 'MSBuild.exe' returned an unexpected exit code. Expected '0'; actual '1'.


could you please help me.
Comment posted by JohnDr on Wednesday, January 23, 2013 5:20 AM
In response to Rishi: The assembly should be in C:\[Program Files]\Microsoft Visual Studio 11.0\Common7\IDE\PrivateAssemblies
Comment posted by Saumya Srivastava on Tuesday, May 14, 2013 12:07 AM
HI,
Can we call default template in main method(program.cs) in workflow?
actually we want to customize the default template. We have created
one code activity and we dropped that code activity on the default
template and just tell me how to invoke this customized default
template so that my customized code activity will execute with the
default template.
Comment posted by Gouri Sohoni on Friday, May 24, 2013 4:43 AM
Hi Saumya,

Please follow the steps in this and previous article to user your custom activity as a part of default template.
Comment posted by saumya srivastava on Friday, May 31, 2013 5:58 AM
how to create bug work item using c# via an xml file in TFS ie via an external source?
Comment posted by saumya srivastava on Friday, May 31, 2013 5:58 AM
how to create bug work item using c# via an xml file in TFS ie via an external source?
Comment posted by saumya srivastava on Friday, May 31, 2013 5:58 AM
how to create bug work item using c# via an xml file in TFS ie via an external source?
Comment posted by Gouri Sohoni on Monday, June 3, 2013 12:03 AM
Please follow the link http://www.dotnetcurry.com/ShowArticle.aspx?ID=753 and other work item related links
Comment posted by saumya srivastava on Wednesday, June 12, 2013 12:28 AM
how to feed values in the TFS from a wcf service??
Comment posted by saumya srivastava on Monday, July 8, 2013 1:14 AM
Hi i want to know that how i can subscribe to workitem changed event using ISubscriber interface? i have gone through this link but i didn't get it where i have to write this code..

http://nakedalm.com/team-foundation-server-2010-event-handling-with-subscribers/