Monday, May 13, 2013

SharePoint: exclude files from WSP

Publishing of WSPs in Visual Studio is very simple and straightforward. But this process includes absolutely everything into WSP (aspx, ascx, css, js, etc), even if we don’t need them. For example, if we have minified versions of javascript files, we definitely don’t need debug versions in production. SharePoint tooling doesn’t really help us when we need to customize building process for our solutions. But luckily we have MsBuild! I’ll come up with simple solution on how it’s possible to exclude some files from Layouts directory. Everything that will be described below has been tested in Visual Studio 2012 and SharePoint 2010.

When we click Publish button from VS menu, it executes the following MSBuild file:

c:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets

A closer look to this file shows that we can officially override two targets here: BeforeLayout and AfterLayout. We need to do two things - exclude file from WSP and exclude it from manifest.xml. Excluding from WSP is simple enough. According to Microsoft.VisualStudio.SharePoint.targets file there is EnumeratedFiles ItemGroup created before BeforeLayout target, so we can exclude some files in this target like that:

ExcludedFiles ItemGroup should be defined earlier. FindInEnumeration is simple task that finds files in MSBuild ItemGroup array. Using MSBuild 4.0 we can use little C# snippets in order to create tasks:

After we excluded items from WSP let’s exclude them from manifest.xml. We have to do it separately because manifest files are gathered before BeforeLayout target and we have no control over it. We need to define the following AfterLayout target:

ExcludeFromManifest task reads manifest.xml file from pkg folder and removes ExcludedFiles items from it:

Please note that I added Condition="'$(Configuration)'=='Release'" condition to both targets, so only Release version of WSP will not contain excluded files, Debug version should stay untouched for development mode of course.

Here is the link to the whole build file:

You can just include it in your project, import it through  <Import Project="build-release.targets" /> and modify ScriptsDir, ManifestPath, ExcludedFiles variables according to your project needs. 


  1. Thanks for sharing such informative guide on .Net technology. This post gives me detailed information about the .net technology. I am working as trainer in leading IT training academy offering Dot Net Training in Chennai and i use your guide to educate my students. .Net Training in Chennai

  2. I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.

  3. Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.
    selenium training in chennai

  4. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.

    DevOps Training in Bangalore

  5. Have you played gambling for a long time? Come to us and win right away. good luck on your side. best slots game online .Good luck does not turn away from you, take risks and win

  6. Good Article… I Like to read your articles because your writing style is too good, it is useful for all of us. Do check Matlab Training in Chennai | CCNA Training in Chennai & Get trained by an expert who will enrich you with the latest trends.

    Ethical Hacking Training in Chennai | Robotics Training in Chennai
    IOT Training in Chennai | Data Science Training in Chennai | Machine Learning training in Chennai | R programming Training in Chennai

  7. This comment has been removed by the author.

  8. Informative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post.

    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

  9. Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me

    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery