SharePoint – Timer Job Executing Multiple Times

March 13, 2014 1 comment

Custom timer jobs are created by inheriting SPJobDefinition class. This class is available under Microsoft.SharePoint.Administration namespace.

Recently I was working on a timer job which was scheduled to run every 5 minutes. After deployment I found the job is invoked multiple times during each 5 minute cycle. In my case this was executing 10 times. This happened because I have 10 content database. To avoid this we should make use of the SPJobLockType enumeration.

public MyCustomTimerJob(string jobName, SPWebApplication webapp) : 
base(jobName, webapp, null, SPJobLockType.Job) {
	this.Title = "My Custom Timer Job";
}

Hope this helps!

PHP – MySQL Insert

January 16, 2014 1 comment

After long wait I got my hands dirty with PHP and MySQL.

$dbServer = 'localhost';
$dbUserName = 'username';
$dbPassword = 'password';
$connection = mysql_connect($dbServer, $dbUserName, $dbPassword);
if(!$connection) {
		  die('Connection Failed : ' . mysql_error());
}

$insertStatement = 'INSERT INTO TableName(Column) VALUES("Value")';
mysql_select_db('databaseName');
$returnValue = mysql_query( $insertStatement, $connection );
if(!$returnValue){
		  die('Insertion Failed : ' . mysql_error());
}
echo "Data Inserted.";
mysql_close($conn);

Categories: Misc Tags: ,

VB.Net – Keydown event not firing with WinForm application

December 4, 2013 Leave a comment

As part of my recent project I was handed with a job to create a windows form application. In this application I had to capture arrow key events. For my surprise the key event where not firing for arrow keys. I tried setting “KeyPreview” but that didn’t helped. Then I realized these are special keys and controls in the form will intercept these.

The solution was to override ProcessCmdKeys function as below

For demonstration, add a PictureBox control and change its background color. Pressing arrow key will move it around the form

Public Class Form1
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keydata As Keys) As Boolean
        If keydata = Keys.Right Then
            PictureBox1.Left += 10
        End If

        If keydata = Keys.Left Then
            PictureBox1.Left -= 10
        End If

        If keydata = Keys.Up Then
            PictureBox1.Top -= 10
        End If

        If keydata = Keys.Down Then
            PictureBox1.Top += 10
        End If

        If keydata = Keys.Right Or keydata = Keys.Left Or keydata = Keys.Up Or keydata = Keys.Down Then
            OnKeyDown(New KeyEventArgs(keydata))
            ProcessCmdKey = True
        Else
            ProcessCmdKey = MyBase.ProcessCmdKey(msg, keydata)
        End If
    End Function
End Class
Categories: VB.Net

SharePoint 2013 – Hide page title

December 4, 2013 7 comments

Here is a snippet which can be used to hide page title. All you need to do is edit the master page and place the script inside head tag

<script type="text/javascript">
	function hidePageTitle() {
		var elem = document.getElementById('pageTitle');
		elem.style.display = 'block';
	}

	_spBodyOnLoadFunctionNames.push("hidePageTitle"); 
</script>

SharePoint 2013 – Open folder in new window

December 3, 2013 Leave a comment

I found a question related to opening folder in new window.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/6fb8e8e7-c3f8-4df9-9ecf-cf48851dc1c0/open-links-from-folders-in-document-libraries-from-a-new-window?forum=sharepointgeneral

The solution is to override HandleFolder method and use the script below

function HandleFolder(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) {
	var parts = new Array();
        parts = c.toLowerCase().split('%2f');
        c = c.replace('SitePages/Home.aspx',parts[1]);
	window.open(c);
	return false;
}

Edit page, add new script editor webpart and use the script.

JavaScript – Object Oriented Programming

December 2, 2013 Leave a comment

The use of JavaScript is increasing heavily. It’s the right time to learn about writing good, clean and efficient JavaScript code rather than simply writing in an unorganized fashion. In this post I am trying to bring up some of the concepts we should be aware while using JavaScript.

JavaScript is a prototype based language. Its a style of object-oriented programming in which classes are not present.

In JavaScript defining a class is equal to creating a function. For example

function Employee() {
}

To create instances of this we use

var e = new Employee();

Creating an object will executes the statements inside the function, thus it acts as a constructor. For example

function Employee() {
    alert("I am an employee");
}
var e1 = new Employee();

This will bring up the alert window
JS1

Properties & Methods

JavaScript object properties are varibales declared inside the class. We can refer properties inside the class using the this keyword (similar to C#).

Example

function Employee(name) {
     this.name = name;
}
var e1 = new Employee("Amal Hashim");
alert("I am an employee with name = " + e1.name);

JS2

Methods are defined as functions, and they are invoked similar to how properties are used

Example

function Employee(name) {
	this.name = name;
}
Employee.prototype.getName = function() {
	alert(this.name);
};
var e1 = new Employee("Amal Hashim");
e1.getName();

JS3

Inheritance & Encapsulation

JavaScript supports only single class inheritance. Inheritance enables the creation of parent child relationships.

Example

//Define Parent Class
function Employee(name) { 
	this.name = name;
}
Employee.prototype.whoAmI = function() {
	alert("Employee Object");
}
Employee.prototype.getName = function() {
	return this.name;
}

//Define Child Class
function Manager() {
	//Invoke parent constructor
	Employee.call(this);
}
//Inheritance - Manager inherits Employee
Manager.prototype = new Employee();
Manager.prototype.constructor = Manager;
//Over ride whoAmI
Manager.prototype.whoAmI = function() {
	alert("Manager Object");
}

var manager = new Manager();
manager.whoAmI();
manager.name = "Amal Hashim";
alert(manager.getName());
//checking
alert(manager instanceof Manager);
alert(manager instanceof Employee);

JS4
JS6
JS4
JS4

In the above example, Manager object doesn’t need to define getName method. Its encapsulated in the parent Employee class.

Categories: jQuery Tags: ,