public static void AddAfterFirstItemGroup() { var project = Project.Load("testproj.csproj"); var itemGroup = new ItemGroup(); var newFile = new Compile("testfile.cs"); itemGroup.Add(newFile); var firstItemGroup = project.ItemGroups.First(); firstItemGroup.AddAfterSelf(itemGroup); var referenceItemGroup = new ItemGroup(); var guid = Guid.NewGuid(); var reference = new ProjectReference("../classlibrary/classlibrary.csproj") { Project = $"{{{guid}}}", Name = "Some.Namespace", }; // Add a Condition attribute to an item Metadata reference.Metadata["Project"].Condition = " '${CustomProperty}' == 'CustomValue' "; referenceItemGroup.Add(reference); itemGroup.AddAfterSelf(referenceItemGroup); project.Save(); }