Archive for the ‘SharePoint’ Category

SharePoint – Creating a Rotating Image Banner using JavaScript

April 27, 2015 2 comments

In this post I will show you how easily we can create a Rotating Image banner in SharePoint 2013. (I saw the requirement from

Images are stored in an Images Library (Picture Library)

First you need to place anĀ img tag into the Page. This is the tag which loads Images from Library and cycles images every 5 seconds.

<img id="imageRotator" >

Once this is added, you can use below JavaScript code

	var arrayOfImageObjects = new Array();
	var currentImageIndex = 0;
	function GetImagesAndRotate() {
		var url = "http://siteurl/_api/web/lists/getbytitle('ImageLibrary')/items?$expand=File"
			url: url,
			type: "GET",
			headers: { 
				"accept": "application/json;odata=verbose",
				"content-type": "application/json;odata=verbose",
				"X-RequestDigest": $("#__REQUESTDIGEST").val(),				
			success: function(data){
				var result = data.d.results;
					var image = result[i];
					var imageObject = { };
					imageObject.Name = image.File.Name;
					imageObject.Url = image.File.ServerRelativeUrl;
					imageObject.Width = image.File.ImageWidth;
					imageObject.Height = image.File.ImageHeight;					
			error: function(){
				alert("request failed");
	function NextImage(){		
		if(currentImageIndex < arrayOfImageObjects.length) {
			currentImageIndex = 0;		

Finally you can invoke this using


SharePoint – Retrieve SharePoint Timer Job Schedule

Here is a utility function which can give you the Job Schedule

string GetTimerJobStatus(string timerJobName) 
    string schedule = string.Empty;

    SPSecurity.RunWithElevatedPrivileges(() => 
        SPWebApplication webApp = SPContext.Current.CurrentWeb.WebApplication;
        foreach (SPJobDefinition job in webApp.JobDefinitions)
            if (job.Name.Equals(timerJobName))
                schedule = job.Schedule.GetType().FullName;

    return schedule;

SharePoint – JSOM Sample for SP.AttachmentCollection.getByFileName Method

Today I found a question in StackExchange.

The OP was asking how to use this method using Client Side Object Model. Since this is not available in MSDN ( I thought of creating it.

Here is the sample – You should change the LISTNAME, ID & FILENAME

var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle("LISTNAME");
var listItem = list.getItemById(ID);
clientContext.executeQueryAsync(Function.createDelegate(this, onSuccess), Function.createDelegate(this, onFailure));

function onSuccess(sender, args) {
	var item = listItem;
	var total = item.get_attachmentFiles().get_count();
	if (total > 0) {
		console.log(total + " file attachments");

function onFailure(sender, args) {

SharePoint Online – Setting up PowerShell

SharePoint global administrator can use the SharePoint Online Management Shell to manage the site and content.

Following are the steps we should do for that

  1. Install Windows Management Framework 3.0 – You can download it from This require a system restart
  2. Install SharePoint Online Management Shell – You can download it from

Once this is done, you can start “SharePoint Online Management Shell” and use following command to connect

Connect-SPOService -Url -credential

After connection, you can execute SPO cmdlets and work remotely.



will retrieve all sites.

Take a look at this to find the list of supported cmdlets

Categories: Misc, SharePoint Online Tags:

SharePoint 2013 – Read List Items using REST API

December 23, 2014 1 comment

SharePoint 2013 introduced Rich REST endpoints, we can use this to query data asynchronously using jQuery/JavaScript.

Here is a sample code which can be used to read items from a SharePoint list

var siteUrl = window.location.protocol + "//" + + _spPageContextInfo.siteServerRelativeUrl;

	url: siteUrl + "/_api/lists/getbytitle('ListTitle')/Items?$select Title",
	type: "GET",    
	headers: { 
		"accept": "application/json;odata=verbose",
		"X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
	success: function(d) {
		var stringData = JSON.stringify(d);
		var jsonObject = JSON.parse(stringData);
		var results = jsonObject.d.results;
		for(i = 0; i < results.length;i++) {
	error: function() {

We are dynamically forming the site url using various tokens.

First we need to get the SharePoint list, for that we use REST method GetByTitle and pass the list title. Then we are selecting the item Title. You can add more fields by separating with comma.

The result is then converted to JSON object using JSON.Parse method.

Finally a loop is used to iterate through the items.

Categories: SharePoint 2013 Tags: , , ,

SharePoint – Updating External Lookup Field

November 21, 2014 Leave a comment

Updating an external look up is a bit different compared to updating other look up fields.

Below is a small snippet which can help you with this task

SPBusinessDataField dataField = item.Fields["Field Display Name"] as SPBusinessDataField;
item[dataField.RelatedField] = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { NEWVALUE }); ;
item["Field_x0020_Internal_x0020_Name"] = newCRMNumber;

For getting SPBusinessDataField you need to get the fields from item which is the listitem we are trying to modify.

item.Fields["Field Display Name"] – This require you to pass the Field Display name

SharePoint – Programatically Starting a Workflow

Here is a code snippet I recently developed for starting a sharepoint workflow from code behind.

public void StartWorkFlow(string listName, int itemID, string workflowName)
        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            using (SPWeb oWeb = oSite.OpenWeb())
                oWeb.AllowUnsafeUpdates = true;
                SPList oList = oWeb.Lists[listName];
                SPListItem oItem = list.GetItemById(itemID);
                SPWorkflowManager wfManager = oSite.WorkflowManager;

                foreach (SPWorkflowAssociation association in oList.WorkflowAssociations)
                    if (association.Name == workflowName)
                        string associationData = association.AssociationData;
                        wfManager.StartWorkflow(oItem, association, associationData);                                
                oWeb.AllowUnsafeUpdates = false;
Categories: SharePoint, SharePoint 2010 Tags:

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!

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'); = 'block';


SharePoint 2013 – Open folder in new window

December 3, 2013 Leave a comment

I found a question related to opening folder in new window.

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]);;
	return false;

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