Monthly Archives: April 2014

SharePoint 2013 JSLink property

JS Link property is SharePoint 2013 new addition property where users can Control Rendering of Fields, Items and WebParts using a Java Script file.

I will use my existing students list from my previous post for this example. I also want to makes the background color of each student different based on the status field.

Results

DisplayTemplateList1
DisplayTemplateList2

Solution

1. Create Javascript as per below and Save it as DisplayTemplate.js in Root site > Style Library

(function () {
 var itemCtx = {};
 itemCtx.Templates = {};

 itemCtx.Templates.Header = "<div><b>Students</b></div><ul>";
 itemCtx.Templates.Item = StatusOverride;
 itemCtx.Templates.Footer = "</ul>";

 itemCtx.BaseViewID = 1;
 itemCtx.ListTemplateType = 100;

 SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);
})();

function StatusOverride(ctx) {
 if (ctx.CurrentItem.Status == 'Active')
 {
  return "<li style='background-color: green;width: 300px;height: 24px;'>" + ctx.CurrentItem.Title + " – " + ctx.CurrentItem.Status + "</li></span>";
 }
 if (ctx.CurrentItem.Status == 'Not Active')
 {
  return "<li style='background-color: red;width: 300px;height: 24px;'>" + ctx.CurrentItem.Title + " – " + ctx.CurrentItem.Status + "</li></span>";
 }
 if (ctx.CurrentItem.Status == 'Pending')
 {
  return "<li style='background-color: yellow;width: 300px;height: 24px;'>" + ctx.CurrentItem.Title + " – " + ctx.CurrentItem.Status + "</li></span>";
 }
}

2. Set Students List View Webpart JSLink property to ~sitecollection/Style Library/DisplayTemplate.js and Click Apply

DisplayTemplateList3

References

Advertisements

SharePoint 2013 New Delegate Controls

Using SharePoint delegate control, a developer can customize the SharePoint site controls without editing or even touching the master page.
Most of us already knows existing delegate controls from previous versions of SharePoint.

  • AdditionalPageHead
  • GlobalNavigation
  • GlobalSiteLink0
  • GlobalSiteLink2
  • GlobalSiteLink3
  • PublishingConsole
  • PageHeader
  • TopNavigationDataSource
  • TreeViewAndDataSource
  • PageFooter
  • QuickLaunchDataSource
  • SmallSearchInputBox

You could find more details on those delegate controls on Daniel’s blog. Developers could also create new delegate control of their own and include it in their custom master page like I found here.

Now with SharePoint 2013, they introduce new Delegate Controls below.

SuiteBarBrandingDelegate
SuiteBarBranding
SuiteLinksDelegate
SuiteLinksDelegate
PromotedActions
PromotedActions

I found excellent article with example that’s really helpful and used mostly with my client project in Tobias Zimmergren blog.

Retrieve List Items Using Knockout.js with SharePoint REST API

Recently, I’m trying to use Client side object model or java script whenever possible. I’m sure there are multiple ways to retrieve List items, but in this scenario I will use SharePoint REST API and utilizing Knockout view model to display the list of items.

Prerequisite

  • Includes jquery.min.js to Style Library
  • Includes knockout.js to Style Library
  • Includes ko-sp.js to Style Library
  • Create Majors list
    ko1
  • Create Courses list
    ko2
  • Create Students list
    ko3
Solution

  • Edit a Page and Insert Content Editor webpart
    ko4
  • Edit Content Editor Source and insert below code:
    <script src="/Style%20Library/jquery-1.11.0.min.js"></script>
    <script src="/Style%20Library/knockout-3.1.0.js"></script>
    <script src="/Style%20Library/ko.sp-1.0.min.js"></script>
    <div id="restStudentDiv">
    	<table width="900px">
    		<thead>
    			<tr>
    				<th>SID</th>
    				<th>Name</th>
    				<th>Major</th>
    				<th>Courses</th>
    				<th>Status</th>
    			</tr>
    		</thead>
    		<tbody data-bind="template: { name: 'templateStudent', foreach: Items }" />
    	</table>
    </div>
    <script type="text/html" id="templateStudent">
    	<tr>
    		<td data-bind="text:SID"></td>
    		<td data-bind="text:Title"></td>
    		<td data-bind="spLookup:Major,defaultValue:'NA'"></td>
    		<td data-bind="spLookup:Courses,multi:true,defaultValue:'NA',dataFormat:'/'"></td>
    		<td data-bind="spChoice:Status"></td>
    	</tr>
    </script>
    <script type="text/javascript">
    	function StudentModal() {
    		var self = this;
    		self.Items = ko.observableArray([]);
    		$.getJSON(_spPageContextInfo.webAbsoluteUrl + "/_vti_bin/listdata.svc/Students?$expand=Major,Courses",
    		function (data) {
    			if (data.d.results) {
    				self.Items(ko.toJS(data.d.results));
    			}
    		}
    		);
    	}
    	ko.applyBindings(new StudentModal(), restStudentDiv);
    </script>
    
Result
ko5

Fix: Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON’

I’ve recently faced an issue when setting up BCS connection from SQL Server with error message Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON’. Quick googling determine that this is a Double Hop issue when the SQL Server box is not the same server as the BCS Service Application running.

My Solution is to create new key in Secure Store Service Application (ensure user has access to the SQL Database) and update BCS connection to use the key ID along with Impersonate Windows Identity.

Reference

%d bloggers like this: