Monday, August 24, 2015

MSSQL Maintenance Plans


Daily Maintenance:

Back Up Database (Differential)
Check Database Integrity

Weekly Maintenance:

Rebuild Index
Back Up Database (Full)
Check Database Integrity
Clean Up History
Maintenance Cleanup Task




Sunday, January 25, 2015

HTTPS within iframe on IE11

Not sure which of the following codes here worked, but when added on the header the HTTPS url inside on iframe will work on IE 11.

<!-- START: needed to load HTTPS inside iframe on IE 11 -->

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1, IE=9">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta content="IE=8" http-equiv="X-UA-Compatible">

<!-- END: needed to load HTTPS inside iframe on IE 11 -->


Monday, November 17, 2014

Umbraco: Partial View VS Partial View Macro

The company I have worked for is a multinational event organizer which is hosting one of the biggest shows on earth. With this nature of the business, an event website should be as flexible as a rubber where you can twist and make changes anytime and anywhere, not just on contents but on the design aspect as well. So compiling the Umbraco CMS is not an option,and besides, only 2 developers are employed to maintain their websites and in-house enterprise applications.

Umbraco is a great CMS, the best one and friendliest for developers that I have used so far. However, with the business needs, we faced a challenge of applying quick changes on code logic and view logic. If only we have time to compile Umbraco and make custom Surface Controllers, it would be a lot better, but because there's only 2 of us, we should be able to apply the changes as quickly as possible even when we're out of the office and lessen the compilation.

Another challenge is to let the designers have total freedom on their layout and not limited to any specific HTML standards but should not be able to see the macro codes because they don't have access to the Developers section and just to be sure they don't accidentally remove any syntax on the macros. The developers should also be able to change or modify the macros without the fear of breaking any HTML codes. They should also be able to create multiple macros but using the same HTML markup from the designers without copy-pasting the html markup on every macros. 

I have searched the internet about the good practice on how to achieve these goals, but to no avail. So, here's what I came up so far for the business needs.
  1. Partial View = where designers can implement their HTML markup. Minimal razor syntax.
  2. Partial View Macro = where developers will code the business logic and call the necessary partial views. No HTML markup.
  3. View Models = classes used to communicate or pass data between Partial View and Partial View Macro. This is the only part where we have to compile on VS. I don't think this will change that much.
The first thing I did is create a new VS project as Class Library (name it anything you like). I then created the view model classes, but using the namespace "Umbraco.Web". Be careful you don't create the same class names with Umbraco. We have added prefix on our classes just to be sure. The reason for this is so that you don't have to modify your existing web.config or including the references for each macros or partial views. At first, I have tried adding the references on the web.config and macros and it worked fine, until I have made the same changes on the Azure website which was not able to find the references. (couldn't find the answers to this yet). Here's a sample view model that I have created on my custom class library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Umbraco.Web
{
    public class OurHeader
    {
        public string Name{ get; set; }
        public string Logo{ get; set; }
    }
}
Compile this project and copy your dll on the bin folder of your Umbraco installation.


Then, I have created a partial view called "_ourHeader". To make use of the properties available on a normal Umbraco views and helpers, the partial view must inherit from UmbracoViewPage.
@inherits Umbraco.Web.Mvc.UmbracoViewPage<OurHeader>

<div class="header-info">
 <span>@Model.Name</span>
 <img src="/ImageGen.ashx?width=389&heigth=94&constrain=true&image=@Model.Logo" />
</div>

I then created a partial view macro called "header" with parameter which sets the name of the partial view to use.


@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{

      string partialView = (string)Model.MacroParameters["partialView"];

      OurHeader _header = new OurHeader ();
     //add your logic here how to populate the data
 
     //render your partial view
     @Html.Partial(partialView,_header)
}

On the template, the macro is then called by supplying the partial view name to use.


@Umbraco.RenderMacro("Header", new {partialView="_ourHeader"})

With this approach, the designers can have both access on the templates as well as the partial views, and they can have total control on their HTML markup without changing the codes inside the macro. They can define numerous versions of the partial views for a macro, and the developers can create multiple versions of macros using the same partial view. 

Basically, that's it. If you have a better approach to this, please let me know. 

Monday, October 27, 2014

Javascript to get hash value from URL

<script type="text/javascript">
            var hashId = window.location.hash;
            hashId = hashId.match(/#[^?&\/]*/g);
<script>

sample input: www.domain.com/#anchorlink?firstname=mark

result: #anchorlink

Tuesday, July 22, 2014

Umbraco + TweetInvi + JQuery Social Stream Wall

I just spent days to figure out the best Twitter library using OAuth that we can use on our Umbraco websites and a JQuery plugin that could display various social media widgets on a "wall".

Here's the major problem:







  • We don't want to do any custom codes and recompile an Umbraco installation  nor create a package / plugin because we are new to Umbraco and we don't have the time yet to learn how to create Umbraco packages.

  • By searching through the internet forums and doing some code testings, we found out that TweetInvi and JQuery Social Stream could help us achieve our goals. These two libraries are just awesome and it did saved our day!

    But here are more problems:

    • JQuery Social Stream is using a PHP class for the Twitter OAuth as an example and we are using Umbraco (C#)
    • We don't want to create another site just to host the PHP class.
    • TweetInvi returns an IEnumerable rather than a JSON object that can be easily passed to any Javascript library, which means we might have to use another library to convert these objects to JSON
    • I couldn't find any samples on the web that have used both TweetInvi and JQuery Social Stream under an Umbraco website.

    These are all simple problems but not for me as I am not an expert in any of these libraries and Umbraco. So I have to deal with this especially that my company have already purchased a license for JQuery Social Stream.

    Fortunately, Lee Chestnutt (JQuery Social Stream) and Linvi (TweetInvi) have quickly responded to my queries and Linvi's source code is available on GitHub for download in which I could just browse and check if there's any functions available that I could use.

    So far, here are the solutions to these simple problems:

    1. Copy all TweetInvi dlls into the Umbraco installation bin folder
    2. Create a new template under Umbraco
    3. Create a new macro (partial view macro)
    4. Use TweetInvi to authenticate your site to Twitter. You can include this on the Umbraco template or on a macro
    5. Use TweetInvi's function "Tweetinvi.Json.TimelineJson.GetUserTimeline" to retrieve the twitter timeline as json
    6. Render the result as JSON on the Umbraco template
    7. Create a page with the template we just created above
    8. Use JQuery Social Stream on the macro and pass the url (generated from #7) to the twitter option

    Simple isn't it?

    To help other beginners just like me, here's how it was done (through codes):

    Umbraco Template:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using System
    @using System.Collections.Generic
    @using System.Diagnostics
    @using System.IO
    @using System.Linq
    @using Tweetinvi
    @using Tweetinvi.Core.Enum
    @using Tweetinvi.Core.Extensions
    @using Tweetinvi.Core.Interfaces
    @using Tweetinvi.Core.Interfaces.Controllers
    @using Tweetinvi.Core.Interfaces.DTO
    @using Tweetinvi.Core.Interfaces.Models
    @using Tweetinvi.Core.Interfaces.Models.Parameters
    @using Tweetinvi.Core.Interfaces.oAuth
    @using Tweetinvi.Core.Interfaces.Streaminvi
    @using Tweetinvi.Json
    @using Stream = Tweetinvi.Stream
     
    @{
        Layout = null;
     
     string accessToken = (string)CurrentPage.twitterAccessToken;
     string accessTokenSecret = (string)CurrentPage.twitterAccessTokenSecret;
     string consumerKey = (string)CurrentPage.twitterConsumerKey;
     string consumerSecret = (string)CurrentPage.twitterConsumerSecret;
     string screenName = (string)CurrentPage.twitterScreenName;
     int maxTweets;
     Tweetinvi.Core.Interfaces.IUser user;
     
     //authenticate your site to twitter OAuth
     TwitterCredentials.SetCredentials(accessToken, accessTokenSecret, consumerKey, consumerSecret);
        
     if(!Int32.TryParse(CurrentPage.twitterMaxLimit.ToString(),out maxTweets))
     {
      maxTweets = 20;
     }
     
     if(String.IsNullOrEmpty(screenName))
     {
      //get user
      user = Tweetinvi.User.GetLoggedUser();
     }
     else
     {
      //get user from screen name
      user = Tweetinvi.User.GetUserFromScreenName(screenName);
     }
     
     //retrieve the timeline as json
     var tweets = Tweetinvi.Json.TimelineJson.GetUserTimeline(user,maxTweets);
     
     //convert template to return as json
     Response.ContentType = "application/json";
     
     //write result as json
     @Html.Raw(tweets); 
     
    }

    Umbraco Macro:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using ClientDependency.Core.Mvc
    @using ClientDependency.Core
    @{
     Html.RequiresJs("~/scripts/jquery/plugins/socialstream/js/jquery.social.stream.1.5.4.min.js", 3);
     Html.RequiresJs("~/scripts/jquery/plugins/socialstream/js/jquery.social.stream.wall.1.3.js", 4);
     Html.RequiresCss("~/scripts/jquery/plugins/socialstream/css/dcsns_wall.css");
     
     var startNodeId = (string)Model.MacroParameters["startNodeId"];
     var iconPath = (string)Model.MacroParameters["iconPath"];
     var imagePath = (string)Model.MacroParameters["imagePath"];
     var htmlId = (string)Model.MacroParameters["htmlId"];
     
     var node = Umbraco.Content(startNodeId);
     
     if(node.DocumentTypeAlias == "SocialWall")
     {
      List<string> feeds = new List<string>();
      List<string> options = new List<string>();
      
      if(String.IsNullOrEmpty(htmlId) || htmlId == "null")
      {
       htmlId = "social-stream";
      }
      if(String.IsNullOrEmpty(iconPath) || iconPath == "null")
      {
       iconPath = "/scripts/jquery/plugins/socialstream/images/dcsns-dark-1/";
      }
      if(String.IsNullOrEmpty(imagePath) || imagePath == "null")
      {
       imagePath = "/scripts/jquery/plugins/socialstream/images/dcsns-dark-1/";
      }
      
      //this is where you have to insert the template url
      if(!String.IsNullOrEmpty(node.socialWallTwitter.ToString()))
      {
       var twitterNode = Umbraco.Content(node.socialWallTwitter.ToString());
       
       if(twitterNode != null && !String.IsNullOrEmpty(twitterNode.Name))
       {
        feeds.Add("twitter: { id: '" + twitterNode.twitterScreenName + "', url: '" + twitterNode.Url + "' }");
       }
       
      }
      
       
      if(!String.IsNullOrEmpty(node.socialWallFacebook))
      {
       feeds.Add("facebook: { id: '" + node.socialWallFacebook +"' }");
      }
      
      //do your other stuffs here...
      
      //render the social stream
      <div id="@htmlId"></div>
      <script type="text/javascript">
       $(document).ready(function($){
       $('#@htmlId').dcSocialStream({
         @Html.Raw(String.Join(",",options.ToArray()))
        });
       });
      </script>
     }
    }
    
    
    

    I know this isn't the best solution, but so far this was the fastest and easiest way for us to implement. So if you have a better solution or suggestions, please don't hesitate to comment below or send me an email. Would be really glad if you could help us out. :)



    Tuesday, December 4, 2012

    Delete files / folders older than X days on windows (bat file)

    This script will delete all MS SQL database backups (*.bak files) older than 30 days on the current directory and all its subdirectories:

    forfiles /s /m *.bak /d -30 /c "cmd /c del @path"
    


    This script will delete all MS SQL database backups (*.bak files) older than 30 days on a specified directory and all its subdirectories:

    forfiles /p "c:\database\backups" /s /m *.bak /d -30 /c "cmd /c del @path"



    This script will delete all folders older than 30 days on a specified directory:


    forfiles /p "C:\todelete" /m * /d -30 /c "cmd /c rd /s /q @path"  
    


    More from here: ForFiles

    If you don't have the forfiles.exe, you can download it from here: ForFiles.zip