Thursday 27 September 2012

SharePoint 2010 - Custom Sign In Page: 401 Unauthorized

Recently when developing a custom sign in page I was encountering an “401 Unauthorized” error.  

The cause of this error was that my application page was inherited from LayoutsPageBase.  

In order to fix this I changed the application page to inherit from System.Web.UI.Page.

Throws 401 Unauthorized

public partial class Login : LayoutsPageBase

Resolution

public partial class Login : System.Web.UI.Page

Friday 31 August 2012

PowerShell - Event Receivers on SharePoint List

There are times when you want to check which event receivers are attached to a SharePoint list.   This can be easily achieved through use of a PowerShell script.

$spWeb = Get-SPWeb Identity http://sharepoint
$spList = $spWeb.Lists["My List"]  
$spList.EventReceivers | Select Name, Assembly, Type

There are also times when you might want to remove any event receivers from the list

$spWeb = Get-SPWeb Identity http://sharepoint
$spList = $spWeb.Lists["My List"]  
$spList.EventReceivers | Select Name, Assembly, Type

$eventsCount = $spList.EventReceivers.Count

for ($i = $eventsCount-1; $i -ge 0; $i--)
{
    $spList.EventReceivers[$i].Delete()
}

$spList.Update()

Wednesday 29 August 2012

SharePoint - REST : Feed Format not supported

With REST services looking like they are going to be playing a bigger part of the next version of SharePoint I felt it was time long over due for me to take a look.

I started with a basic query to return an item from a list based on its ID:

  • http://sharepoint/_vti_bin/listdata.svc/MyCustomList(1)

However,  when entered into IE I was presented with the following message:

Internet Explorer cannot display this feed

Internet Explorer does not support this feed format

Quite a simple fix:

Within IE click Tools –> Internet Options –> Content

Within the the Content Tab click on Settings button within the Feeds and Web Slices section:

FeedWebSlices

Within the Feed and Web Slice Settings window uncheck the “Turn on feed reading view” then Click OK.

TurnOnFeed

Now when I execute the query I get my results:

Results

First hurdle overcome lots more to learn!!!

Monday 27 August 2012

.NET Select Distinct Values From DataTable

To select distinct values from a DataTable you can make use of the DataView:

DataView view = new DataView(dt);
DataTable distinct = view.ToTable(true, "columnname");

Tuesday 21 August 2012

SharePoint 2010 - asp:CreateUserWizard

While using the asp:CreateUserWizard control to add forms based authentication users to my asp membership database I was encountering the following error:

Exception of type 'System.ArgumentException' was thrown.
Parameter name: encodedValue

The error above was being generated after I successfully created a new user.  The issue it seems is with the control creating a cookie named:  .ASPXAUTH

The solution expire the cookie.

private void ResetAuth()
{
  HttpCookie aspCookie = new HttpCookie(".ASPXAUTH");
  aspCookie.Expires = DateTime.Now.AddDays(-1);
  Response.Cookies.Add(aspCookie);            
}

This needs to be done on both the ContinueButtonClick and the onActiveStepChanged Event.

ActivateStepChanged

protected void ActiveStepChanged(object sender, EventArgs e)
{
   ResetAuth();   
}

ContinueButtonClick

protected void btnContinue_Clicked(object sender, EventArgs e)
{
   ResetAuth();
}

A more detailed explanation of this and other asp.net login control issues can be found here within Bernado Nguyen-Hoan's Blog