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

Wednesday 6 November 2013

ASP.NET MVC4 SimpleMembership Provider Notes

These are just notes for my benefit at the moment for implementing SimpleMembership Provider in an ASP.NET MVC4 Web Application.

Update 19-12-2013

Eureka! If you're trying to customise your User Profiles using SimpleMembership then read Introduction to Forms based authentication in .NET4.5 MVC4 with C# Part 1 and Introduction to Forms based authentication in .NET4.5 MVC4 with C# Part 2. This is a straightforward step-by-step explanation of how to achieve custom user profile fields, including how to handle the registration method.

Update 18-12-2013

Finally this stuff is starting to make sense!

I've used the techniques outlined in Long Le's Blog Article today and added additional fields to the SimpleMembershipProvider UserProfile table. Can't recommend this enough if you're struggling with this subject.

Resources

Some tools that also look useful.

This solution looks promising http://forums.asp.net/t/1877141.aspx as does this one http://forums.asp.net/t/1915443.aspx and this MVC4 role based controller's action access

SimpleRoleProvider Class

Tuesday 17 September 2013

Google Analytics Individual Qualified - Topics Covered

Revision Topics List

I'm going to break down my revision notes by topic as laid out on the Google Analytics Test website.

  • Accounts & Profiles
  • Google Analytics Tracking Code
  • Advanced Segments
  • Google Webmaster Tools
  • Browsers & Operating Systems
  • In-Page Analytics
  • Content Experiments *
  • Intelligence Events & Custom Alerts
  • Cookies & Sessions
  • Internal Site Search
  • Custom Reports
  • Mobile Devices
  • Custom Variables
  • Multi-Channel Funnels
  • Dashboards, Shortcuts & Sharing
  • Profile Filters & Profile Settings
  • Dimensions & Metrics
  • Real-Time Reports
  • Domains & Subdomains
  • Regular Expressions
  • E-commerce & Revenue
  • Search Engines & Search Engine Optimization
  • Events & Virtual Pageviews
  • Social *
  • Geography & Localization
  • Table Views & Table Filters
  • Goals & Funnels
  • Time & Annotations
  • Google AdSense **
  • Traffic Sources & Campaigns
  • Google AdWords & Search Engine Marketing
  • Visitors, Visits & Pageviews

Accounts & Profiles

Notes from Google Analytics Developers Guide

  • You may have up to 50 views (profiles) in any given Analytics account.
  • If you want to provide administrative access to other users of an account, those users will be able to see and modify all view (profile) data for all websites being tracked in the account.
  • You cannot migrate historical data from one account to another. Thus, if you set up an account for a web property and then later want to move tracking to a separate account, you cannot currently migrate the data from the old account to the new account
  • When you already have a functioning view (profile) for an existing website, and you add an additional view (profile) later on in time, the additional view (profile) will not contain the historical data that you see in the view (profile) created earlier.
  • You share your Analytics reports with other people who have Google Accounts.

Tuesday 10 September 2013

How to Get Top Search Queries Data from Webmaster Tools API

Here's how to get a .csv file of your latest Search Query report from the Google Webmaster Tools API.

Go download the gwdata.php api file here.

Here's sample code from the docs which grabs a .csv of your latest Search Query report and saves it in the same folder.

<?php
include 'gwtdata.php';
try {
$email = "XXXXX";
$password = "XXXXX";
# If hardcoded, don't forget trailing slash! $website = "http://nerdygoodness.wordpress.com/";
# Valid values are "TOP_PAGES", "TOP_QUERIES", "CRAWL_ERRORS",
# "CONTENT_ERRORS", "CONTENT_KEYWORDS", "INTERNAL_LINKS",
# "EXTERNAL_LINKS" and "SOCIAL_ACTIVITY".
$tables = array("TOP_QUERIES");
$gdata = new GWTdata();
if($gdata->LogIn($email, $password) === true)
{
$gdata->SetTables($tables);
$gdata->DownloadCSV($website);
}
} catch (Exception $e) {
die($e->getMessage());
}
?>

I've run this code to get the top search query report from my other blog nerdygoodness.wordpress.com. The results of which can be seen here.

Tuesday 3 September 2013

Google Analytics Individual Qualification - the journey begins

I've decided to prepare for the Google Analytics Individual Qualification.

I'll document my progress here in the hope that it helps someone else who wants to actually know what they're talking about.

I've listed some of the Revision Resources I'll be using here