Knockout.js: first experience

For a project, I have started using Knockout.js.
This is a library to build UIs starting from a data model that is binded to html templates.

To start using the library, there is a list of articles that must be read:

And finally a few notes I have understood:
  • Visible controls only the html style, but you have always the binding.
    If you don't want to bind something (for example because you have null objects), use the if construct
Other links:

JavaScript: console.log error

When working in JavaScript I have done output with console.log, but after that my page was working correctly only in debug mode.

I have found the solution in the page What is console.log and how do I use it?: the reason is that in Internet Explorer up to version 8, console.log is defined only in debug mode.

The solution is to add in your page the following JavaScript code:

   if(!window.console){ window.console = {log: function(){} }; }

JavaScript: how call cross-domain Ajax services

For one big company I have developed a project where I had to call JSON services from client html pages.

The particularity was that the services where hosted in a domain different from the domain of the hosted pages.

I have found some difficulties, and here is a list of tips:

  • Internet Explorer requires the use of the object XDomainRequest
  • if the calling page is served in https, the JSON service has to called in https too
  • the receiving service must accept cross-domain calls and must declare this adding the following header:
          Access-Control-Allow-Origin: *


Some (but not all) of this guidelines is in the article Cross Domain Ajax Request with JSON response for IE,Firefox,Chrome, Safari – jQuery.

SharePoint 2010: How check if a folder exists

In SharePoint client object model, I needed to check if a folder exists.
In the post How to check if folder subfolder already exists in SharePoint document library using SharePoint web service I have found the (almost) correct query to be used. It is missing and close and open <Eq> tag.

The correct version is:
<Query><Where>
<And>
<Eq>
<FieldRef Name='FileLeafRef' />
<Value Type='Text'>[your Folder name]</Value>
</Eq>
<Eq>
<FieldRef Name='FSObjType'/>
<Value Type='Text'>1</Value>
</Eq>
</And>
</Where></Query>

SharePoint: integrate the Lync presence control in your pages and web parts

For one organizational chart page I needed to incorporate the Lync presence control next to each name. Practically I wanted to replicate what you get when you show a list containing a people picker field, but without using a field of this type.

 

I have found the article Integrate Lync into your intranet sites using the NameCtrl plug-in.

Only a few notes on the article:

  • 'case' is a JavaScript keyword, so when it is used as a field in the DIV, it has to be enclosed between quotes;
  • when creating the id of the control, in the getId function, it's needed to use a replaceAll function for SIDs like name.surname@domain.com

SharePoint and JavaScript CSOM

I'm relizing that I'm spending too much time googling the same topics over and over (on JavaScript programming), so I list here a few solution I need in a recurrent way.

 

JavaScript: how understand if page is opened in iFrame, and get its url

 

Javascript: how get url parameters

  • function to get query string parameters:
    function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
    }
  • source: How to get URL parameters with Javascript?

 

Javascript: function to format numbers

 

Javascript: function to format date-times

 

Javascript: how html encode strings

 

jQuery: how write a custom filter method

SharePoint: how add submenus in the top link bar

In SharePoint, without the Publishing Feature enabled, it's not possible to add sub-menus in the top link bar (except for sub-sites and related pages).

This is quite limiting, because it doesn't allow to organize menus in sites with many pages.

Luckily, I have found the solution in the article How can I add a dropdown menu to the Sharepoint Foundation top navigation bar?

Essentially, it's enough to import the file spf-dropdown2010.js or spf-dropdown2013.js after jQuery.

 

I have tried the solution above, and it worked, but it had the following:

  • PRO: possibility for the user to customize the sub-menus via SharePoint UI
  • CON: each time the page is loaded, the sub-menu items are seen for a short time
  • CON: for internal pages without the script, the sub-menus are not generated and so this doesn't give a nice UI (this point is valid in case it's not possible to modify the master page)

 

So I have changed implementation of the solution with the code below:

   $("span.menu-item-text:contains('TopMenu1')")

      .closest('li.static')

      .addClass('dynamic-children')

      .append('<UL class=dynamic></UL>')

      .find('ul')

      .append('<LI class="spfDropdowns-submenuitem dynamic"><A class="menu-item dynamic" href="Url1"><SPAN class=additional-background><SPAN class=menu-item-text>Descr1</SPAN></SPAN></A></LI>')

      .append('<LI class="spfDropdowns-submenuitem dynamic"><A class="menu-item dynamic" href="Url2"><SPAN class=additional-background><SPAN class=menu-item-text>Descr2</SPAN></SPAN></A></LI>');