Adding Chrome to browser identification list for Microsoft Ajax

Microsoft Ajax library can identify browser type and version for Firefox, Internet Explorer and Safari. Chrome is missing from this list and it seems Microsoft added Chrome to “init.js” but did not extend Microsoft Ajax. However, as Microsoft Ajax is not limited to SharePoint, you can add Chrome by adding the following code in another JavaScript file (be sure you load the file after Microsoft Ajax is loaded).

(function () {

    function execute() {
        Sys.Browser.Chrome = {};
        var a = navigator.userAgent.toLowerCase();
        if (a.toLowerCase().indexOf("chrome") != -1) {
            Sys.Browser.agent = Sys.Browser.Chrome;
            Sys.Browser.version = parseInt(a.substring(a.indexOf("chrome/") + 7));

    if (typeof (Sys) !== "undefined" && typeof (Sys.Browser) !== "undefined") {


And now you can easily identify is browser is Chrome and what version is used.

// Check if browser is Chrome
if (Sys.Browser.agent === Sys.Browser.Chrome) {
    // Alert current Chrome version

Date type extensions in Microsoft Ajax library

I have to admit, Microsoft Ajax JavaScript library is not so well known and popular these days. But still if offers a lot of functionalities a developer might use doing some client side work. I believe can be very useful for SharePoint developers, as SharePoint, being a web forms based technology, use it.

For this article, my objective is to explain a little bit more about Ajax Date type extensions, which are a set of methods designed to extend JavaScript built-in Date object.

Format date as string

Converting a date object to a formatted string has never been easier. Ajax is adding a extension method to Date object called “format” which accepts a format string as parameter. As rule this string should contain the following placeholders to show different parts of the date:

  • yyyy – show full year
  • yy – show short format of year (only the last 2 numbers)
  • M – show short month number (doesn’t add leading zero if month number is under 10)
  • MM – show month
  • MMM – show month name, like Oct, Jan
  • MMM – show full month name, like October and January
  • d – show short day number (doesn’t add leading zero if day number is under 10)
  • dd – show day
  • ddd – show day name, like Sat, Sun….
  • dddd – show full day name, like Saturday or Sunday
  • hh- show hours
  • mm – show minutes
  • ss- show seconds
var dt = new Date();

// 2015-10-22T09:18:30Z

// 2015/10/22

// 22-Oct-15

// Output Thu

// Output Thursday

// Output October

You can play with this format string parameter and find more date formats. There is not rocket science here and it is straight format.

Convert a formatted string to Date

Let’s now discussed about the reverse process to convert a string to Date object. Microsoft implemented a static method for Date object called “parseInvariant”. It returns a date if string is a valid representation of it, otherwise returns null.

// @value = A string that represents a date.
// @format = A string provided the format information. For me an array of strings did not work, even  Microsoft says this is the correct parameter type
 var a = Date.parseInvariant(value, format);

You can see below some examples.

var dt;

// This is valid date and is 1st of december 2015. 
dt = "2015-12-01";
dt = "12/01/2015";
dt = "01 December 2015";
Date.parseInvariant(dt,"dd MMMM yyyy");

It seems for “parseInvariant” function we use the same placeholders like we used for “format” function.

The functions I have talked about do not depend on the culture, but there are also other two versions which are affected by it: “parseLocale” and “localeFormat”. Documentation about entire Date type extended methods can be found here. I have to admit Microsoft did not provide too much details about this, but I hope my findings will help you work smarter doing client side development on web forms based technology (not only, because Ajax is not depending on server side).

Autocomplete textbox with JavaScript CSOM

I have searched on the internet about how to create an auto-complete functionality in SharePoint. Of course, jQuery UI was the solution with an Ajax request to REST service. For some reason, I cannot understand it, the examples I have seen are based on synchronous Ajax request. So I simply said no way. I needed something asynchronous to avoid page freeze.

Normally to create an auto-complete is a simple thing.

        minLength: 3,
        source: function(request, response) {
         // At the and of the async operation call response with obtained results

Asynchronous operation will be placed inside source function, but instead using classic Ajax examples, I will use JavaScript CSOM. It is not better, but I like it more. So is more a personal choice.

For getting data from SharePoint, you can use classical example from MSDN website, but I prefer to reorganize the code a little bit. After all I still have Microsoft Ajax library available so I can put classes in namespaces or I can validate parameters type.

/// <Reference Name="MicrosoftAjax.js" />


Shp.Lists = function () {
    throw 'Cannot instantiate Shp.Lists static class';

Shp.Lists.GetItems = function (listName, query, web, success, fail) {
    /// <summary>Get list items based on provided CAML query</summary>
    /// <param name="listName" type="String" optional="false" mayBeNull="false">List name</param>
    /// <param name="query" type="String" optional="false" mayBeNull="false">Query</param>
    /// <param name="web" type="SP.Web" optional="false" mayBeNull="true">Web</param>
    /// <param name="success" type="Function" optional="false" mayBeNull="false">Success callback</param>
    /// <param name="fail" type="Function" optional="true" mayBeNull="false">Fail callback</param>

    var e = Function.validateParameters(arguments, [{ name: 'listName', type: String, mayBeNull: false, optional: false },
                                                   { name: 'query', type: String, mayBeNull: false, optional: false },
                                                   { name: 'web', type: SP.Web, mayBeNull: true, optional: false },
                                                   { name: 'success', type: Function, mayBeNull: false, optional: false },
                                                   { name: 'fail', type: Function, mayBeNull: false, optional: true }], true);
    if (e) throw e;

    var fail = fail || function (error) { alert(error); };
    var ctx = (web === null) ? SP.ClientContext.get_current() : web.get_context();
    var web = (web === null) ? ctx.get_web() : web;

    Shp.Lists._GetItems(listName, query, ctx, web, success, fail);

Shp.Lists._GetItems = function (listName, query, ctx, web, success, fail) {

    var oList = web.get_lists().getByTitle(listName);
    var camlQuery = new SP.CamlQuery();
    var oListItems = oList.getItems(camlQuery);
    ctx.executeQueryAsync(function () {
    }, function (sender, args) {


“Shp.Lists.GetItems” should be called with the following parameters:

  • List name, as string, not optional and cannot be null.
  • CAML query as string, not optional and cannot be null.
  • Web as SP.Web, not optional but can be null. In this case web associated with the current context is used.
  • Success as function, not optional and cannot be null. It is executed if operation is a success.
  • Fail function, optional and cannot be null. If not specified and operation fails, code will alert the error message.

Now as I created a reusable function for reading list items, everything should be much easier. I just need to call “Shp.Lists.GetItems” with correct parameters inside auto-complete source and, if operation is successfully, to add suggestions based on list items.

    minLength: 3,
    source: function (request, response) {
        var term = request.term;
        var query = '<View><Query><Where><Contains><FieldRef Name="Title" /><Value Type="Text">' + term + '</Value></Contains></Where></Query></View>';
        Shp.Lists.GetItems("list name", query, null, function (items) {
            var suggestions = [];
            var listItemEnumerator = items.getEnumerator();
            while (listItemEnumerator.moveNext()) {
            // Add suggestions



This was my approach of creating the auto-complete functionality. As I said, is a personal option to use JavaScript CSOM because looks for me more organized and structured. Of course code can be extended and you can even create an Ajax client side control to incorporate this functionality.

Thank you for reading my post!