Monday 23 December 2013

Creating Custom HTML Reports using jQuery, SPServices, JSON, the Script Editor App and some elbow grease

I'm typing these notes up here because I really pushed the boundaries of my, if I'm honest limited, knowledge of Sharepoint development.

The Problem

The client I was dealing with at work was looking to have a Year Planner view of a Tasks List. This isn't something you can do out of the box, so I was stumped!

The Solution

SPServices To The Rescue!

The jQuery Library for SharePoint Web Services is a great way to access Sharepoint Data in JSON format without having to worry about Visual Studio or Sharepoint Designer.

Ok down to the nitty-gritty. To reference the SPServices Library you can do the following:

<!-- Reference jQuery on the Google CDN -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<!-- Reference SPServices on cdnjs (Cloudflare) -->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.2/jquery.SPServices-0.7.2.min.js"></script>

You basically edit your Sharepoint Page and add a Script Editor App. Then click Edit Snippet with the Script Editor in Edit Mode and add your JavaScript code in there

As with most JQuery malarchy we start with document.ready


<script language="javascript" type="text/javascript">
$(document).ready(function() {
var tasks = GetTasks();
});

function GetTasks(){
// JSON ObjectArray container
var tasks = [];
//The Web Service method we are calling, to read list items we use 'GetListItems'
var method = "GetListItems";
//The display name of the list we are reading data from
var list = "{YOUR LIST NAME HERE e.g. Tasks}";
var fieldsToRead = "<ViewFields><FieldRef Name='Title' /><FieldRef Name='AssignedTo' /></ViewFields>";

// CAML Query var query = "<Query>" +
"<Where>>" +
"<Neq>" +
"<FieldRef Name='ID'/><Value Type='Number'>0</Value>" +
"</Neq>" +
"</Where>" +
"<OrderBy>" +
"<FieldRef Name='Title'/>" +
"</OrderBy>" +
"</Query>";


//Here is our SPServices Call where we pass in the variables that we set above
$().SPServices({
operation: method,
async: false, //if you set this to true, you may get faster performance, but your order may not be accurate.
listName: list,
CAMLViewFields: fieldsToRead,
CAMLQuery: query,
//this basically means "do the following code when the call is complete"
completefunc: function (xData, Status) {
//this code iterates through every row of data returned from the web service call
$(xData.responseXML).SPFilterNode("z:row").each(function() {
//get the title field
var name = ($(this).attr("ows_Title"));
// which site?
var site = ($(this).attr("ows_Site"));
//who's it assinged to?
var person = ($(this).attr("ows_AssignedTo"));
person = person.replace(/^\d+;#/,"");
// week number ?
var completed = ( $(this).attr("ows_Checkmark") );
completed = completed.replace(/boolean;#/,"");
// overdue ?
var overdue = ( $(this).attr("ows_Overdue") );
tasksJSON.push({
name: name,
person: person,
site: site,
completed: completed,
overdue: overdue
});
});
}
});
return tasksJSON;
}

References

Friday 20 December 2013

MVC Not running on IIS6 on windows 7 - Have you registered asp.net?

Just because you've installed the .NET Framework on your local machine for testing it doesn't necessarily mean that when you deploy your ASP.NET app to your localhost IIS that it will work. You actually need to register asp.net and here's how:

c:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -i

Login failed for user 'IIS APPPOOL\DefaultAppPool'

if you get this error:
Login failed for user 'IIS APPPOOL\DefaultAppPool'
Here's a few things to watch out for
  • If you have the setting: Integrated Security=true in your web.config file , set it to false or remove it.
  • In IIS Manager go to Application Pools and select the one running your web app. Go to Advanced Settings, Process model and make sure Identity is set to NetworkService
  • Restart the website
That should sort it.

Update: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'

Let's say you follow the previously outlined steps and you still get this error: "Login failed for user 'NT AUTHORITY\NETWORK SERVICE'". You can try the following solution:

  • In SQL Server Management Studio go to Security -> Logins
  • Right Click on NT AUTHORITY\NETWORK SERVICE -> Properties -> User Mappings.
  • Select the Database in question and make sure the User has the db_owner and public checkboxes ticked.

Wednesday 18 December 2013

ASP.NET MVC 4 Entity Framework (EF) Code First

I'm finding this a very fast and efficient way of creating .NET MVC Web Applications but still find I have to keep looking up the initial stages of setting up Entity Framework Migrations. With that in mind I'll be updating this post periodically with summary notes on what's involved.

The best article I've found on this subject so far is on dominikgorecki.com entitled "Code First Entity Framework with MVC4 and Visual Studio 2012".

Friday 6 December 2013

Sharepoint Notes - Calculated Fields - Displaying HTML in Calculated Fiels, Displaying Status Indicator Icons, Display Week Number, Calculate Overdue Status

Display HTML in a Calculated Column

Make sure you select Number rather than single line of text for the format of your calculated column.

Use Calculated Column to Display if a Task is Overdue

This formula displays True or False: =AND([Due Date]<NOW(),Status<>"Completed",[Due Date]<>"") To make your Overdue column display a KPI Icon you can do this: ="<div><img src='/_layouts/images/KPIDefault-"&IF(AND([Due Date]<NOW(),[Task Status]<>"Completed",[Due Date]<>"")=FALSE,0,2)&".gif'/></div>"

Display KPI Icon for Yes / No column

This formula worked for me: ="<div><img src='/_layouts/images/KPIDefault-"&IF(Approved=TRUE,0,2)&".gif'/></div>"

Display Week Number in Calculated Column

This formula worked for me: =INT(([Due Date]-DATE(YEAR([Due Date]),1,1)+7-WEEKDAY([Due Date],1))/7)+1

Resources

Here's a list of resources I used to get to grips with this stuff. Some of them I've ended up referring back to several times.

Sharepoint Notes

Week Numbers in Calendar Color Coding: More Examples Using calculated columns to add color coding to your SharePoint lists Using calculated columns to write HTML SharePoint Calculated Column Formulas Sharepoint converting a date to a week number