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.



Tuesday, August 26, 2014

SharePoint 2013 search for SharePoint 2010 content

Many customers are excited about the new features that SharePoint 2013 brings to the table. Small or large organizations who have implemented any SharePoint implementation project size hesitate to upgrade for many reasons, but they want to take advantage of some the new features of 2013.
As we all know, F4SP is part of the 2013 platform now and it is not a standalone product anymore. For this reason, the vision was to better off use SharePoint 2013 for search, rather than F4SP then go through the headaches of migration F4SP to 2013 or any future release of the product.
The introduction of the Service Applications in SharePoint 2010, made life easier to implement scalable architecture and to create large multi-tenants farms, where you can share and publish service applications across different SharePoint farms. The same architecture is carried to 2013, and now we have the ability to publish service applications from 2013 to 2010, allowing customers to take advantage of some new features of the 2013 platform.
Note that 2010 can consume 2013 service applications and not the other way around.
Here is a list of the service applications that you can publish in 2013 and consume in 2010:
1.     User Profile Service
2.     Search Service
3.     Managed Metadata Service
4.     Business Connectivity Services
5.     Secure Store Service
Details on how to publish the Search Service Application in 2013, and consume it in 2010 using the Search Center.
First Step: You need to establish a trust relationship between the two farms:
1.    Export the Farm and STS certificates from the SharePoint 2010 farm:
$rootCertificate = (Get-SPCertificateAuthority).RootCertificate
$rootCertificate.Export("Cert") | Set-Content C:\Certificates\2010FarmRoot.cer -Encoding byte
$stsCertificate = (Get-SPSecurityTokenServiceConfig).LocalLoginProvider.SigningCertificate
$stsCertificate.Export("Cert") | Set-Content C:\Certificates\2010FarmSTS.cer -Encoding byte
 
2.    Export the Farm certificate from the SharePoint 2013 farm:
$rootCertificate = (Get-SPCertificateAuthority).RootCertificate
$rootCertificate.Export("Cert") | Set-Content C:\Certificates\2013FarmRoot.cer -Encoding byte
 
3.    Import the SharePoint 2013 certificate into the SharePoint 2010 farm:
$trustCertificate = Get-PfxCertificate C:\Certificates\2013FarmRoot.cer
New-SPTrustedRootAuthority "2013 Trust"-Certificate $trustCertificate
 
4.    Import the SharePoint 2010 into the SharePoint 2013 farm:
$trustCertificate = Get-PfxCertificate C:\Certificates\2010FarmRoot.cer
New-SPTrustedRootAuthority "2013 Trust" -Certificate $trustCertificate
$stsCertificate = Get-PfxCertificate C:\Certificates\2010FarmSTS.cer
New-SPTrustedServiceTokenIssuer "2013 Trust" -Certificate $stsCertificate
 
Second Step: You need to publish the Search Service Application and set the permissions:
1.    Go to Central Admin à Manage Service Applications
2.    Click on your Search Service Application
3.    Click Publish; make sure you select the checkbox next to "Publish this Service Application to other farms"
4.    From the SharePoint 2010 farm, run the following command to get the Farm ID:
$farmID= Get-SPFarm
$farmID.Id
5.    From the SharePoint 2013 farm, run the following commands:
$security=Get-SPTopologyServiceApplication | Get-SPServiceApplicationSecurity
$claimprovider=(Get-SPClaimProvider System).ClaimProvider
$principal=New-SPClaimsPrincipal -ClaimType "http://schemas.microsoft.com/sharepoint/2009/08/claims/farmid" -ClaimProvider$claimprovider -ClaimValue [FarmID]
Grant-SPObjectSecurity -Identity $security -Principal $principal -Rights "Full Control"
Get-SPTopologyServiceApplication | Set-SPServiceApplicationSecurity -ObjectSecurity $security
 
6.    From the SharePoint 2013 SSA, give the SharePoint 2010 Farm ID “Full Control” permissions
 
From 2010, you can connect to the 2013 SSA by providing the 2013 SSA published servive URL. 
Now, go into your 2013 SSA, add a SharePoint 2010 content source and run a full crawl. Once the crawl is completed, you will be able to search the content using your 2010 Search Center.
Note: If you need to take advantage of the results preview feature, you will need to install and configure Office Web Apps 2013 against your SharePoint 2013 farm.

Thursday, January 30, 2014

How to color code SharePoint 2010 calendar with Jquery

It is very easy to achieve this with Jquery, a little tricky, as the calendar items loads after the page is ready, hence jquery document.ready() function will still not have all of the html.
<script type="text/javascript">
if (typeof jQuery == "undefined") {
    var jQPath = "_layouts/MFS/";
    document.write("<script src='", jQPath, "jquery-1.8.3.min.js' type='text/javascript'><\/script>");
}
_spBodyOnLoadFunctionNames.push('colorCalendarEventLinkIntercept')
function colorCalendarEventLinkIntercept()
{
  if (SP.UI.ApplicationPages.CalendarNotify.$4a)
  {
    var OldCalendarNotify = SP.UI.ApplicationPages.CalendarNotify.$4a;
    alert(OldCalendarNotify);
    SP.UI.ApplicationPages.CalendarNotify.$4a = function ()
    {
        OldCalendarNotify();
        colorCalendarEventLinks();
    }
  }
  if (SP.UI.ApplicationPages.CalendarNotify.$4b)
  {
    var OldCalendarNotify =SP.UI.ApplicationPages.CalendarNotify.$4b;
     alert(OldCalendarNotify);
    SP.UI.ApplicationPages.CalendarNotify.$4b = function ()
    {
        OldCalendarNotify();
        colorCalendarEventLinks();
    }
  }
}
function colorCalendarEventLinks() {
 $('div.ms-acal-item[title*="usa"]').css('background-color', '#FF0000');
 $('div.ms-acal-item[title*="china"]').css('background-color', '#F00987');
 $('div.ms-acal-item[title*="india"]').css('background-color', '#F00987');
}
</script>