Thursday, March 24, 2011

Get current SharePoint User with JavaScript

During the SharePoint form customization in many cases it is important to know the current user. So with JavaScript and SharePoint Designer this is how it's done:

var loginElement = document.getElementById("zz7_Menu").innerHTML;
var end = loginElement.indexOf("<");
var nameOnly = loginElement.substring(8, end);

note: I use this script in MOSS2007, but if you use this in WSS3 change "zz7_Menu" to "zz6_Menu".

My whole point using this script was to get the current username (in my case organization username format is: username14, username57 etc.) And based on that I need to get only numbers from this username. So there is my complete solution:

var loginElement = document.getElementById("zz7_Menu").innerHTML;
var end = loginElement.indexOf("<");
var nameOnly = loginElement.substring(8, end);
var number = nameOnly.replace (/[^\d]/g, "");
var k;

if (number != "") {
var elements = document.getElementsByTagName('select');
for (var i = 0; i < elements.length; i++) {
if (elements[i].title == 'YOUR DROPDOWN TITLE'){
var sel = elements[i].options.selectedIndex = number;
for (k=elements[i].options.length-1;k>=0;k--) {
if (k != sel) {
elements[i].remove(k);
        }
      }
    }
  }

}

Tuesday, March 22, 2011

How to get CRM grid View Id

If we add this piece of code to ISV.config we get the current View Id currently displayed in the CRM grid:

if (top.stage.crmGrid != null)
{
    var ViewID = top.stage.crmGrid.GetParameter('viewid');    

    alert(ViewID);
}

Train Tracking with GPS

Last Saturday I took a trip on a train from Riga to Saulkrasti (45km). Weather was pretty cloudy but I thought maybe I could get interesting info from this trip with my gps. This is my gps data report

Train model: ER2T

Friday, March 18, 2011

CRM Save Events - you can do more

    When user saves the form we can check which save event was executed and regarding to that event make some actions. Two most popular save events are Save and SaveAndClose.


Javascript functions to call from  your script:
crmForm.Save(); 
crmForm.SaveAndClose();


To check which event was executed  we use "event.Mode"
Output for Save event = 1;
Output for SaveAndClose event = 2;


example:   
if  (event.Mode == 1) {
alert("You click Save button");
} else if (event.Mode == 2) {
alert("You click SaveAndClose button");

Thursday, March 17, 2011

JavaScript in CRM ISV.config

Writing JavaScript code in ISV.config can be painful. There is one important thing you need to know when you write JS code in ISV - Some JavaScript characters here are not supported and that's why you need to encode them:

Symbol                 Encoded value
   <                       &lt;    
   >                       &gt;
   "                       &quot;
   &                       &amp;