Tuesday, 1 April 2014

Restore Content Types in Sharepoint Foundation 2013

If you find yourself in the situation where you've messed up with the Content Types in a Sharepoint Site Collection then this will help restore everything back to the default settings:

Open - Start - All Programs - Microsoft Sharepoint Products - Sharepoint Management Shell and run this:

stsadm -o deactivatefeature -name Fields -url http://sitecoll -force
stsadm -o activatefeature -name Fields -url http://sitecoll -force

Source: http://sharepoint.stackexchange.com/questions/8517/site-content-types-screwed-up-how-to-restore-to-default/26225#26225?newreg=0c4f241b46ab466ea0d265069efdca5f

Wednesday, 26 March 2014

SharePoint Foundation 2013 Forms Based Authentication (FBA)

Scenario

You need to have individual Forms Based Authentication (FBA) on several different Sharepoint Foundation 2013 Site Collections.

Solution

  1. Run c:\windows\Microsoft.NET\Framework64\v4.0.30319\ aspnet_regsql.exe to set up Membership Database
  2. Give Identity Running Sharepoint "db_owner" priveleges in MSSMS for the aspnetdb database.
  3. Create a Dummy Website in IIS "MembershipConfig"
  4. Install FBA for SharePoint 2013 - This allows management of users inside sharepoint
  5. Edit machine.Config File
  6. Configure Forms Auth in Sharepoint for Site Collection

Caveats

  • Use SQL Authentication for ALL Connection Strings and DB User Accounts.

Resources

Here's a list of the resources I found which helped get the job done:

Friday, 21 February 2014

Searching Model Data based on string and integer field types

Scenario

You need to add a search function based on model data based on searching both string and int datatypes. If you try the obvious method in your LINQ query as just converting your int field ToString you end up with an error similar to this:

ToString is not supported in linq to entities

Or even this bad boy:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

After trawling around the forums for a good hour or so I found the following solution. Hope it helps someone else too.

 public ActionResult Search(string search = null)  
     {  
       int intSearch;  
       if (!Int32.TryParse(search, out intSearch))  
       {  
       }  
       IEnumerable<Car> cars = context.Cars.Where(  
            c => search == null  
              || c.IntegerField == intSearch  
              || c.Manufacturer.Contains(search)  
              || c.Model.Contains(search)             
             );  
       cars = cars.OrderBy(c => c.Id).ToList();  
       ViewBag.Search = search;  
       return View(cars);  
     }  

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