Saturday, December 27, 2014

JSOM Basics

JSOM required Libraries:
MicrosoftAjax.js - Loaded via ScriptResource.axd
SP.runtime.js - Loaded directly from layouts folder
SP.js - Loaded from Layouts folder

Only in the context of a Site Collection and not beyond it. Can only run in the context of current user.

Also use _context = new SP.ClientContext("subsite url");
Also use _context = new SP.ClientContext.get_current()

ERROR HANDLING in JSOM

Interesting Program that creates a list in the catch block if it does not exist yet. Also see the use of Javascript Parser


Tuesday, December 23, 2014

Some important Javascript functions which are available OOTB in Sharepoint

Some OOTB Sharepoint Javascripts (SP 2010 & 2013) which are very handy

IndexOfIllegalCharInUrlPath(string) - Used to check the validity of the URL. This takes the URL in string format and returns the index of the first available illegal character and returns negative when no illegal characters are present, this is particularly useful when a piece of the URL is taken as an input from an user.
Important thing to know about this is you need to strip certain part of the URL before checking.
http://, &, #, ? will be identified as illegal..so replace them before checking.

IndexOfIllegalCharInUrlLeafName(string) - Used to check the validity of the File name or Leaf Name in SharePoint lingo. Identifies the index of first available illegal character or negative in none are present.

Technically you should use both the functions to check the validity of the URL from user input.


ReplaceURLTokens(urlWithTokens, ctx)


SP.Utilities.UrlBuilder.urlCombine(string1, string2) - only works when the second string doesn't begin with a slash.

SP.Utilities.UrlBuilder.removeQueryString(url,key)

SP.Utilities.Utility.getLayoutsPageUrl("Settings.aspx")) - this simple little function help sreturn the server relative URL for the specific layouts page for the  current context. This is really helpful when you environment is a mix of SP2010 and SP2013 where you have upgraded few and not all.

SP.Utilities.HTTPUtility.navigateTo(URL) - this simply navigates the current page to the URL specified and nothing much. The important thing that it does is when the current page is loaded on a SP Modal dialog, the specified URL will also load in the modal dialog.

SP.ScriptUtility: Provides various general purpose functions for examining values, out of which the most useful are as follows

isNullOrEmptyString - Gets a value that indicates the specified text is null or empty or undefined
isNullOrUndefined - Gets a value that indicates the specified text is null or undefined
isUndefined - Gets a value that indicates the specified text is null
truncateToInt - Gets the largest integer value that is less than or equal to the specified number if the number is greater than 0. Otherwise, gets the smallest integer value that is greater than or equal to the number. var value = SP.ScriptUtility.truncateToInt(12.76);

parsing URL:
For Query string values:
GetURLKeyValue(NameOfTheKey, BoolNoDecode, url, BoolCaseInsensitive)

URL: http://sharepointsite/lists/mylist/editForm.aspx??ID=5&Source=%2FLists%2FCountries

GetURLKeyValue('ID', true)

SetURLKeyValue(NameOfTheKey, KeyValue, BoolDecode, url)




Wednesday, December 10, 2014

Variables (Ctx, _spPageContextInfo) available on SharePoint pages OOTB

There are two javascript variables which are very helpful and are entirely free to use - I found  this available on both SP 2010 and SP 2013, one all the time(_spPageContextInfo) and the other, most of the time (Ctx).

Each of these variables represents an object with lot of information related to the current context of the page.

Ctx Object

The image shows various objects available with it. It is extremely useful on List view apps on pages. 
Not available everywhere, not available on form pages(new, edit and display) and not available on layouts pages(site settings, admistration pages). not available on apps, inside lists. This object is read-only

Eg: alert(ctx.ListData.Row[2].Title);
will display the title of the third row of the list view.




_spPageContextInfo Object: 

Very similar to Ctx object with minimal information than Ctx and important information about the page.

The main difference is _spPageContextInfo is available everywhere.

var _spPageContextInfo = 
{
           webServerRelativeUrl: "\u002f", 
           webLanguage: 1033, 
           currentLanguage: 1033, 
           webUIVersion:4,
           pageListId:"{dc0262dd-b8fe-4d76-bd28-9125d2e115a6}",
           userId:1, 
           alertsEnabled:true, 
           siteServerRelativeUrl: "\u002f", 
           allowSilverlightPrompt:'True'
}; 

The above is the variable info that was available for my page that I was on, I got it using the developer toolbar.