Friday, May 12, 2017

Sitecore Admin Menus

Sitecore Admin Pages Explained, from the blog Fire Breaks Ice

Sitecore comes shipped with a plethora of admin pages that provide significant features to developers and administrators. Many of these pages aren’t exactly documented well or at all. I intend to identify and explain the admin pages that I know of and how they can be used to your advantage.

Read More:
http://firebreaksice.com/sitecore-admin-pages-explained/
Wednesday, May 10, 2017

Undoing a merge in Sourcetree/Git

Merging some changes into your master branch, then realizing you need an undo function, is stressful. Fortunately, whether or not you've pushed your merge, you can still reverse what you did. There's no actual "undo" in Git, but you can either reset the master branch, or reverse your changes.

If you haven't pushed to the origin master branch yet, you can reset the master branch to the previous commit before the merge. You'll want to reset using "Hard" mode so that your working copy of master will have the changes removed.

Sourcetree: 
Right-click the previous commit on the master branch, select "Reset current branch to this commit" and select "Hard" mode.

Git Command Line:
git reset --hard XXXXXXX (insert the Commit ID)

If your merge to master has been pushed, then you will need to do a reverse commit in the feature branch, which reverts everything in that commit, merge to master, then push master again. Later on, if you want to re-merge what was in the feature branch to master once again, you will have to reverse commit the reversion itself in the feature branch. This will reinstate the changes you had removed.
Monday, May 1, 2017

Using setInterval to run a script after element is finished loading from server

Sometimes I need to run a JS script on a set of HTML elements, but I run into problems when the script fires before elements or assets are finished loading from the server side.

For example, let's say I'm loading a grid of items, each of which has an image and some copy underneath. I want each item in the grid to be the same height, regardless of how much text it has. Generally I'd run a JS script to set the heights of all the items to the height of the tallest item.

However, since I'm loading the images through a CMS off the server, it sometimes takes a little bit before they are all loaded. If I run the script normally, it will fire before all the images finish loading, and it will resize the items to a height that is too short.

I've found that using the javascript function "setInterval" is a good solution; it runs at a frequency of your choosing, and each time it checks if the slower-loading element is loaded. If it isn't loaded, it will simply repeat the function; if the element is found to be finished loading, then it will run another JS script of your choosing, and clear the interval to stop checking.



// create your interval function as a named variable; having a name allows you to clear it later
var checkImages = setInterval(function () {

    // This checks the height of the last grid item's image, since it will be loaded last. 
    // Once it's loaded, the height will be more than 0.
    if ($('.grid:last img').height() > 0) {

        // if the image is done loading, clear out the interval
        clearInterval(checkImages); 
        
        // then run the script you want; this just happens to be the one I'm using
        equalizeHeights($('.grid__item')); 
    }
    else {
    }
}, 200); // run this every 200 ms
Monday, April 10, 2017

Reconnecting TeamCity build agent

We use TeamCity to build our projects when deploying to staging and production servers. Sometimes the Build Agent will get disconnected if the server gets restarted, and for some reason it won't reconnect.



If you are googling this issue frantically (like I have), you'll find a lot of people suggesting to open up Services in Windows and restarting the TeamCity Build Agent service. While this is needed, I found that doing so didn't reconnect the agent.

I finally started looking through the build agent configuration files, and found that the port change I had just done wasn't reflected in the build agent files. The file I was looking at was <TeamCity installation directory>\buildAgent\conf\buildAgent.properties and buildAgent.dist.properties. I had changed the port number for the web login, and while I saw in the buildAgent.dist.properties that it had the new port in the serverUrl config, the buildAgent.properties file still had the old port in the generated serverUrl.

I'm still not sure why the TC generation wasn't working, but I manually changed the port in the buildAgent.properties file. After restarting the TeamCity Build Agent service once more for good measure, I was able to see that the build agent was reconnected in the web interface.

Getting Grunt to run when deploying to a Windows server

I use Grunt to process my website css/js/etc files, which is quite handy. When deploying to a server, you do need to make sure that the files get processed as well. One way to do that is to install grunt on the server itself, and either run your grunt production task on deploy, or keep grunt running on the server with a watch task.

This is a short guide on how to run grunt as a Scheduled Task on a Windows server. (This assumes that you have already added a "watch" task on your grunt production task.)

  • Create a .bat file in the root website directory, with the only content being "grunt" or whichever grunt task you want to run.
  • In Windows Server Manager, click on Configuration > Task Scheduler > Task Scheduler Library > Microsoft > Windows
  • Click Create Basic Task
    • Enter the name and description, click Next
    • Select "When the computer stats" for when the task should start and click Next
    • Under What action do you want the task to perform, select "Start a program" and click Next
    • Under Action > Program/script type in the name of the .bat file (don't use Browse), and under Start in type in the full path to the website directory (if you don't do this and use Browse to path to the file it won't run). Click Next and Finish to finish creating have it auto open the properties.
  • In the Properties pop-up dialog, under Security options, select "Run whether user is logged on or not" and "run with highest privileges"


Friday, April 7, 2017

Shuffle an array of items in Javascript

This function will take an array, shuffle the items, and return a shuffled array.

function shuffleArray(array){

 var currentIndex = array.length, temporaryValue, randomIndex;

 // While there remain elements to shuffle...
 while (0 !== currentIndex) {

  // Pick a remaining element...
  randomIndex = Math.floor(Math.random() * currentIndex);
  currentIndex -= 1;

  // And swap it with the current element.
  temporaryValue = array[currentIndex];
  array[currentIndex] = array[randomIndex];
  array[randomIndex] = temporaryValue;
 }

 return array;
}
Wednesday, April 5, 2017

NuGet package errors in Visual Studio

If you're trying to install or manage NuGet packages in Visual Studio and you're getting error messages like "... end of Central Directory" or "Unable to read beyond the end of the stream," then you might have a corrupted .nupkg file.

To solve this, you will have to first see which packages are affected, and then manually download the .nupkg file for each package in question and replace them in your /packages directory.

To check which packages are corrupt, first open Visual Studio and the NuGet Package Manager in Tools > NuGet Package Manager > Manage NuGet Packages for Solutions.



Once you open it, click on the "Installed" tab and that is generally where you'll see the error messages.

To check which packages have corrupted files, open the /packages/packages.config file and one by one, comment out all but one of the <package> elements in <packages>, each time clicking back to the NuGet - Solution tab in VS to see if the error is still happening. If you don't get an error message for one, that means it's fine.

For the packages with errors, you will have to download the .nupkg files manually. Go to the NuGet site and type in the name of the package you want to download. Click "Download" in the left sidebar, making SURE that you have the same version that is in your Visual Studio! Then paste the file and overwrite your current corrupted .nupkg file in /packages/packagename. 

Once you do this for each of the corrupted packages, uncomment everything in packages.config and check in the Installed tab again. Hopefully your error messages will be all gone and you'll just have a list of the installed packages.