Skip to content

Instantly share code, notes, and snippets.

@Serivy
Created May 10, 2017 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Serivy/74fff320c93176e6ca7f76e1cc139367 to your computer and use it in GitHub Desktop.
Save Serivy/74fff320c93176e6ca7f76e1cc139367 to your computer and use it in GitHub Desktop.
Gets the first item from an item group.
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="GetFirstItem" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
<ParameterGroup>
<Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<FirstItem ParameterType="System.String" Output="true" Required="false" />
</ParameterGroup>
<Task>
<Using Namespace="System.Linq"/>
<Code Type="Fragment" Language="cs">FirstItem = Items.First().ItemSpec;</Code>
</Task>
</UsingTask>
<Target Name="UseCase">
<ItemGroup>
<ItemList Include="first;second;third"/>
</ItemGroup>
<GetFirstItem Items="@(ItemList)">
<Output TaskParameter="FirstItem" PropertyName="FirstItemProperty"/>
</GetFirstItem>
<Message Text="First item is: $(FirstItemProperty)" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment