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: ,

SharePoint 2013 – Document Library – Download a copy button

November 29, 2013 8 comments

I found a question in technet forum, the user was asking on How to add “DOWNLOAD A COPY” button in document library COLUMN?

And to answer this question, I did a bit of research and came up with below solution

First step is to add a new column to the document library. I named it as “Download a copy”

D1

Ensure to add the new column as the last column in the View.

Now go back to the view, then edit page. Now add a new Script Editor webpart and paste below code

<script type="text/javascript">
function runAfterEverythingElse(){
var elementArray = document.getElementsByClassName("ms-vb-lastCell");
var idArray = document.getElementsByClassName("ms-listlink");
for(var i = 0; i < elementArray.length; i++)
{
	elementArray[i].innerHTML = '<a unselectable="on" href="javascript:;" onclick="window.open(\''+ idArray[i]['href'] + '\')" class="ms-cui-ctl-large " aria-describedby="Ribbon.Documents.Copies.Download_ToolTip" mscui:controltype="Button" role="button" id="Ribbon.Documents.Copies.Download-Large"><span unselectable="on" class="ms-cui-ctl-largeIconContainer"><span unselectable="on" class=" ms-cui-img-32by32 ms-cui-img-cont-float"><img unselectable="on" alt="" src="/_layouts/15/1033/images/formatmap32x32.png?rev=31" style="top: -409px; left: -35px;"></span></span><span unselectable="on" class="ms-cui-ctl-largelabel">Download a<br>Copy</span></a>';
}  
}
_spBodyOnLoadFunctionNames.push("runAfterEverythingElse");  
</script>

Click Ok, and stop editing the page. Final result

D1

SharePoint 2013 – Specialized Search Experience

November 28, 2013 Leave a comment

SharePoint 2013 Enterprise search gives out a specialized search experience.

S1

For example, “Everything” will searches across all crawled contents while “People” searches only data related to people etc. In SharePoint 2013 we can customize this pretty quickly. For example, we can easily create a search experience for Microsoft Word documents.

Creating a specialized search experience is a two step process, the first step is to create a Result Source and the next step is to use Search Result webpart.

Creating Result Source

Open site settings and click on Result Sources
S2

Click on New Result Source
S3

Fill following details
S4
Now save and close the window.

Creating Search Results

First create a new Page and name is word-results.aspx

S5

Edit Search Results webpart using the context menu. And click on Change Query

S6

This will bring up the query builder window. Select “Word Results” from Select Query dropdown

S7

Click “Ok” and close the query builder tool. Apply and close the webpart properties pane. The page is ready which only returns search results containing word documents.

Next step is to edit the search experience links. For that, open site settings and click on search settings

S8

Click on Add Link and fill in the details

S9

Click OK and go to search center, now you can see your specialized search experience in action.

S91

C# 5.0 – Caller Information

November 27, 2013 Leave a comment

Caller information is introduced in C# 5. This feature is directly available in Visual Studio 2012. As the name suggests, caller information is a set of attributes which can be used to obtain information about caller to a method.

Attributes include, path of the source code, line number, and the member name of the caller. This information is vital in case of debugging or tracing application issues.

CallerFilePathAttribute – String type attribute, this will provide the file path at compile time.

CallerLineNumberAttribute – Integer type attribute, this will provide the line number at which the method is invoked.

CallerMemberNameAttribute – String type attribute, this will provide the Method/Property name of the caller.

Caller information attributes can be specified only to optional parameters. We should also specify a default value. Below example show a typical logging method

//...
Log("Log this");
//...
public void Log(string messageToLog,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string filePath = "",
        [CallerLineNumber] int lineNumber = 0)
{
    //Log the details
}

To make this working, we should include following using statements

using System.Runtime.CompilerServices 
using System.Diagnostics;
Categories: C# Tags: ,

SharePoint 2013 – List Validation

November 26, 2013 2 comments

One of my friend recently asked me how to apply list validation in SharePoint 2013. I explained him and thought of documenting it so others can use it for reference.

To start with I am trying to enforce the following requirement

There is a list with 3 choice columns “A”, “B”, and “C”. In any of these columns a value = “None” is not allowed.

Solution

Open list setting

ListValidation1

Click on Validation Settings

ListValidation2

Now you can apply formula and a message which will appear if validation fails

ListValidation3

Formula uses the IF condition for validation. The general syntax is IF(condition, true case, false case).
First we will check if column “A” is not equal to “None”, then only go and validate “B”. The same logic is applied for validating “B” and then “C”

ListValidation4

SharePoint 2013 – Directly open list item edit form

November 21, 2013 Leave a comment

Sometimes it will become a difficult job to edit a list item (not for everyone :D). You should first open the context menu, and then click edit item or you can open the item in view mode and then click edit item.

Using jQuery we can easily change this behavior by directly opening edit form instead of view. Below is a simple snippet which can achieve this. Just add a script editor webpart and paste below code

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() 
{
  // Change all display form links to edit form links
  $('.ms-vb a[href*="listform.aspx"]').each(function(){
      var url = $(this).attr('href');
      url = url.replace("PageType=4", "PageType=6");
      $(this).attr('href', url);
  });
});
</script>