How to: Customize a SharePoint Solution package by using MSBuild targets
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
By using MSBuild targets at a command prompt, you can customize how Visual Studio creates SharePoint package files (.wsp). For example, you can customize the MSBuild properties to change the packaging intermediate directory and the MSBuild item groups that specify the enumerated files.
Customize and run MSBuild targets
If you customize the BeforeLayout and AfterLayout targets, you can perform tasks before package layout, such as adding, removing, or modifying files that will be packaged.
To customize the BeforeLayout target
Open an editor, such as Notepad, and then add the following code.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Target Name="BeforeLayout"> <Message Importance="high" Text="In the BeforeLayout Target"></Message> </Target> </Project>
This example displays a message before the packaging of this target.
Name the file CustomLayout.SharePoint.targets, and then save it in the folder for the SharePoint project.
Open the project, open its shortcut menu, and then choose Unload Project.
In Solution Explorer, open the shortcut menu for the project, and then choose Edit <ProjectName>.vbproj or Edit <ProjectName>.csproj.
After the
Import
line near the end of the project file, add the following line.<Import Project="CustomLayout.SharePoint.targets" />
Save and close the project file.
In Solution Explorer, open the shortcut menu for the project, and then choose Reload Project.
When you publish the project, the message will appear in the output before packaging begins.
To customize the AfterLayout target
On the menu bar, choose File > Open > File.
In the Open File dialog box, navigate to the project folder, choose the CustomLayout.target file, and then choose the Open button.
Just before the
</Project>
tag, add the following code:<Target Name="AfterLayout"> <Message Importance="high" Text="In the AfterLayout Target"></Message> </Target>
This example displays a message after this target is packaged.
Save and close the targets file.
Restart Visual Studio, and then open the project.
When you publish the project, the BeforeLayout message appears before packaging starts, and the AfterLayout message appears after packaging finishes.