July 23, 2024

Dear Sitecore Family,

This post is in continuation to the post Setup Sitecore Developer Box in less than 5 minutes.

Here, we will discuss another way of Setting up the Visual Studio project in such a way that we do not need to use Visual Studio Publish .

Challenge:

A Developer needs to Publish the Changes everytime he/she wants to sync it with the Sitecore Website. For some, this can be a useful point – say someone who builds the visual studio solution on a regular basis just to check that things are doing fine till that time, but doesn’t want the Sitecore Website to restart everytime, this would be an ideal scenario. At some point of time, even I do it that way, I just build to check hope there is no build error or warning till that point of time, but as there are some more things left to code, I don’t want to merge them to my Sitecore Website and unnecessary restart it.

But in case when I want to sync the changes to the Sitecore Website, I need to go and say “Publish” to my Visual Studio solution — indeed a step more, which we can save.

On discussing this with Mr. Brijesh PatelTech-It Pro Author, he told me, why do I require do an additional step everytime? And then even I felt what he said, had an important point!

Solution:

He asked me Varun, remember some Build related events? Yes, it is the Post-Build Event which is of our concern!

Then, we went and configured the Post Build Event in a project and it worked like charm! Here’s how we did it.

Steps:

  1. After Step 2 of the Previous Post, go to the Directory Structure of the Visual Studio Solution and open the main Web Project File (.csproj) — in Notepad or Notepad++.
  2. Go to the End of File, and we will see a commented section – after ProjectExtensions.

    [code language=”xml”] <!– To modify your build process, add your task inside one of the targets below and uncomment it.
    Other similar extension points exist, see Microsoft.Common.targets.
    <Target Name="BeforeBuild">
    </Target>
    <Target Name="AfterBuild">
    </Target>
    –> [/code]

  3. Uncomment the Target with name “AfterBuild” and add a few lines of configuration to it. Basically, we will add a few lines into the ItemGroup Section inside AfterBuild, and then we will add the Copy Tag to specify where should the specified files be copied! Here’s how we would do it.

    [code language=”xml”]  <Target Name="AfterBuild">
    <ItemGroup>
    <MyBinDllFiles Include="$(ProjectDir)\bin\**\*.*" />
    <MyConfigFiles Include="$(ProjectDir)\App_Config\Include\**\*.config" />
    <MyImageFiles Include="$(ProjectDir)\VisualStudioSampleProject\StaticResources\Images\**\*.*" />
    <MyFontFiles Include="$(ProjectDir)\VisualStudioSampleProject\StaticResources\Fonts\**\*.*" />
    <MyStyleFiles Include="$(ProjectDir)\VisualStudioSampleProject\StaticResources\Styles\**\*.css" />
    <MyScriptsFiles Include="$(ProjectDir)\VisualStudioSampleProject\StaticResources\Scripts\**\*.*" />
    <MyLayoutFiles Include="$(ProjectDir)\VisualStudioSampleProject\Layouts\**\*.aspx" />
    <MySubLayoutFiles Include="$(ProjectDir)\VisualStudioSampleProject\Sublayouts\**\*.ascx" />
    </ItemGroup>
    <Copy SourceFiles="@(MyBinDllFiles)" DestinationFiles="@(MyBinDllFiles->’C:\Websites\Sitecore\SitecoreSolution\Website\bin\%(Filename)%(Extension)’)">
    </Copy>
    <Copy SourceFiles="@(MyConfigFiles)" DestinationFiles="@(MyConfigFiles->’C:\Websites\Sitecore\SitecoreSolution\Website\App_Config\Include\%(RecursiveDir)%(Filename)%(Extension)’)">
    </Copy>
    <Copy SourceFiles="@(MyImageFiles)" DestinationFiles="@(MyImageFiles->’C:\Websites\Sitecore\SitecoreSolution\Website\VisualStudioSampleProject\StaticResources\Images\%(RecursiveDir)%(Filename)%(Extension)’) ">
    </Copy>
    <Copy SourceFiles="@(MyFontFiles)" DestinationFiles="@(MyFontFiles->’C:\Websites\Sitecore\SitecoreSolution\Website\VisualStudioSampleProject\StaticResources\Fonts\%(RecursiveDir)%(Filename)%(Extension)’)">
    </Copy>
    <Copy SourceFiles="@(MyStyleFiles)" DestinationFiles="@(MyStyleFiles->’C:\Websites\Sitecore\SitecoreSolution\Website\VisualStudioSampleProject\StaticResources\Styles\%(RecursiveDir)%(Filename)%(Extension)’)">
    </Copy>
    <Copy SourceFiles="@(MyScriptsFiles)" DestinationFiles="@(MyScriptsFiles->’C:\Websites\Sitecore\SitecoreSolution\Website\VisualStudioSampleProject\StaticResources\Scripts\%(RecursiveDir)%(Filename)%(Extension)’)">
    </Copy>
    <Copy SourceFiles="@(MyLayoutFiles)" DestinationFiles="@(MyLayoutFiles->’C:\Websites\Sitecore\SitecoreSolution\Website\VisualStudioSampleProject\Layouts\%(RecursiveDir)%(Filename)%(Extension)’)">
    </Copy>
    <Copy SourceFiles="@(MySubLayoutFiles)" DestinationFiles="@(MySubLayoutFiles->’C:\Websites\Sitecore\SitecoreSolution\Website\VisualStudioSampleProject\Sublayouts\%(RecursiveDir)%(Filename)%(Extension)’)">
    </Copy>

    </Target> [/code]

  4. Go to Visual Studio with the Project open – it will ask to Reload the Web Project as it has undergone some changes, say yes and we are done!
  5. Make some changes and say Build Solution – and our changes are visible in the Sitecore Website!

Conclusion:

In a Developer Box, it is always useful to configure the Post-Build Event rather than using the Visual Studio Publish for merging changes with the Sitecore Website, as suggested in Step 3 of the previous post! This is to avoid performing an additional step everytime we need to sync the changes! And as far as saving code changes on a regular basis, we can anytime use Ctrl+Shift+S — to save all the changed files.

The Step 3 – of Visual Studio publish is good when we want to deploy the Project changes to Sitecore Website on different environments, say we are merging the changes from our Local Box to the Internal Integration Testing Environment or the UAT Server.

Hope you enjoyed the post!

Happy Sitecoring! 🙂

2 thoughts on “Setup Visual Studio Project with Post Build Script

  1. Did need to point out something: “but doesn’t want the Sitecore Website to restart everytime”; Once you hit that bin directory you’re in for a domain recycle/warmup.

    And, not that I’m advocating, but just something to share–TDS performs this step just by simply giving it the web project.

    Finally, I don’t know if I need to have every file I’ve generated between in my Sitecore directory. If I build 3 times, but switched nuget packages (maybe one worked at first but found another with a better API) now I’m left with artifacts in my instance because I hit build.

    1. Hi Brad Christie,
      Firstly, Thanks for checking the blog post and I appreciate you commenting on it with your point of view.
      Its always good to discuss things and I loved your point.
      Yes, I completely agree, that TDS performs this step as we give the web project, which I have shown to do manually, the main reason being keeping it a general Sitecore Website — without TDS.
      TDS is a great tool and a must have for an Ideal Sitecore Development Environment — but in case we don’t have one, shouldn’t stop us from getting to something which makes a developer task easy — and hence the post!

      Keep reading, and keep sharing your thoughts, because in the end, that’s how we increase our Knowledge!

      Regards,
      Varun Shringarpure

Leave a Reply

Your email address will not be published. Required fields are marked *