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

Wednesday 25 January 2012

SharePoint 2010 - SPCalendarView Issues

When using the SPCalendarView within a web part I encountered an issue where the calendar was incorrectly displaying the calendar events when moving between the current, previous and next month.  The control correctly displayed the current month ,however, the previous and next month were not displaying as expected.

I had tested the SPCalendarView control within a RTM version of SharePoint and it worked as expected.  However,  when I updated my development environment with the June 2011 cumulative updated the behaviour described above was encountered. 

I noticed that if I disabled the V4Rendering on the control it functioned as expected, but that resulted in a lost of the Ajax functionality.

I subsequently updated my environment with the October 2011 cumulative updated but the issue was still present.

Recently I applied the December 2011 cumulative update and I am glad to report that this issue has now been fixed.

“Applying a cumulative update to SharePoint is not a process to take lightly.  It is recommended that you taking a backup of your SharePoint farm before undertaking any update as you cannot uninstall them. Click here for more information about the contents of the relevant cumulative updates.”

SharePoint 2010 - Debug Timer Job

In order to debug a timer job or a code section within a workflow that occurs after a delay you need to attach the visual studio debugger to the OWSTIMER.exe process.

This can be done using the following steps:

STEP 1

Click On Debug then Attach To Process

Debug

STEP 2

Within the Attach to Process window ensure the Show process from all users checkbox is selected.  Find The OWSTIMER.exe process within the available processes then click Attach.

Attach to Process

After these steps you should now be able to debug your timer job code.

SharePoint 2010 - Debug Workflow After A Delay

The debugging features within Visual Studio are extremely useful when trying to track down the cause of a failure within your workflow code.  

To debug your workflow you usually attach the debugger to the relevant w3wp.exe process.  However, if your workflow contains a “Delay Activity” then SharePoint no longer uses the w3wp.exe process to execute the code after the delay but the OWSTIMER.exe (Time Job) process.

Workflow Diagram

Using the workflow diagram above as an example:

CodeBeforeDelay

This code block executes within the w3wp.exe process.

CodeAfterDelay

This code executes within the OWSTIMER.exe process.

“Click here for details on how to attach the visual studio’s debugger to the OWSTIMER.exe process.”

Thursday 5 January 2012

SharePoint 2010 - Get Web Template Name

Site templates provides a simple and quick way of provisioning new sites with common lists and library's.

However, in order to programmatically create a site from a template it is necessary to know the internal name used to reference the web template. 

The format of the internal name for a custom web template is a Guid followed by a hash followed by the template name.  You can find the name for the template using either PowerShell or C#.

PowerShell

The following PowerShell can be used to output the full template name for a given template.

$site = Get-SPSite -Identity http://sharepoint
$site.GetWebTemplates(1033) |  Where-Object {$_.name -like "*[TemplateName]*"} | Select Name

The following PowerShell can be used to output all the full templates within the site collection.

Get-SPWebTemplate | SELECT name

 

C#

The following C# can be used to find the desired web template given the templates name.

private string getWebTemplate(string SiteUrl, string WebTemplateName)
{
    string TemplateName = string.Empty;
    using (var site = new SPSite(SiteUrl))
    {
        using (var web = site.OpenWeb())
        {
            SPWebTemplateCollection templates = web.Site.GetWebTemplates(1033);
            foreach (SPWebTemplate template in templates)
            {
                string[] templateInfo = template.Name.Split('#');
                string CustomWebTemplateName = templateInfo[1];
                if (CustomWebTemplateName.ToLower().Equals(WebTemplateName.ToLower()))
                {
                    TemplateName = template.Name;
                    break;
                }
            }
        }
    }
    return TemplateName;
}

Once you have the full template name for the template you can then using it to programmatically create a site. Click here to see how to programmatically create a site using a web template.

SharePoint 2010 - Save Site Template

A site template provides a simple and quick way of provisioning new sites with common lists and library's.

The following steps detail how to save a site as a site template.

Step 1

Within the site you wish to save as a template click Site Actions then Site Settings.

Site Settings

Step 2

Within the Site Actions section click on Save site as a template

Site Actions

STEP 3

Enter the filename for the template and then name of the template.  If you wish to include any existing list items within the lists then select the include content checkbox.  Then click OK.

Save as template

STEP 4

Once the template has finished saving a conformation message will be displayed with a link to the solution gallery where the template has been saved.

Operation Successful

STEP 5

Within the solution gallery the new template should now be visible.  Now when new sites are creation within SharePoint the template will one of the options to choose from.

Solution Gallary

SharePoint 2010 - Create Site From Template

Creating a SharePoint site from a site template provides a simple and quick way of provisioning new sites with common lists and library's.

Click here to see how to save a site as a site template.

Once a site has been saved as a site template you can use it as a base to create new sites.  This can either be done through the standard SharePoint interface or through code.

C# – Create Site From Template

private void CreateSiteFromTemplate(string strWebUrl,string strTitle,string strDescription)
        {
            using (var curSite = new SPSite("http://SharePoint"))
            {
                using (var curWeb = curSite.OpenWeb())
                {
                    uint nLCID = 1033;

                    SPWebTemplate WebTemplate = curWeb.Site.GetWebTemplates(1033)["Guid#TemplateName"];

                    curWeb.Webs.Add(strWebUrl, strTitle, strDescription, nLCID, WebTemplate, false, false);                     
                }
            }
        }

 

Key Line

The code block below is used to specify which template is to be used to create the new site.  When using a custom template the format of the template name is a Guid followed by a hash then the name of the template name.

Click here to see how to find out the internal name for a custom site template.

SPWebTemplate WebTemplate = curWeb.Site.GetWebTemplates(1033)["Guid#TemplateName"];