Sometimes we find it useful to create patch features which are used to add new elements to our existing SharePoint sites. Oftentimes, these patch features make use of some of the same files as the larger features that are used for provisioning new sites. To support the creation of these patch features without manually copying the files that are shared across features, use Visual Studio file links and a customized MSBUILD target to automate the copy process. Here’s how it works.
First, in the project that contains the patch feature, add file links to the files that are needed for the feature. To do this, add an existing item to the project just as if you wanted to add a physical copy of the file. However, instead of clicking the add button in the file dialog, click the little arrow next to the button instead and select Add As Link.
Visual Studio will add a reference to the selected file without creating a physical copy. Your project will act as though the file is part of the project, but the only physical copy will be in the original location. Any changes made to the original file will be reflected in the link.
So far so good. We can create a patch feature that duplicates the desired elements defined in the original feature without having to maintain the feature content in two places. The only problem is that automated WSP generators such as STSDEV require a physical copy of the file when creating the WSP to package up the feature. This is where a customized MSBUILD target comes into play.
Add the following copy task to the AfterBuild target in the csproj file of the project that defines the feature. The task copies all of the linked files from their physical locations to their linked locations. The copied files will be in the location where STSDEV expects them to be and the copying process will be completely automated as part of the build.
Make sure that the linked files are set up to be included as content when the project builds. You can do this by setting the Build Action property of the file in the properties window.