Sunday, 26 June 2011

SharePoint 2010 - Unable to login through windows prompt (loopback issue)

Recently I was attempting to log into an extended SharePoint web application that had been configured for Claims Based Authentication.  The problem being that I was unable to login when prompted for credentials.

Turns out it was a loop back problem.  This can be fixed through a change to the registry. Which can be done manually or through some PowerShell.

New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -value "1" -PropertyType dword

 

Wednesday, 15 June 2011

SharePoint 2010 - Lookup Field + Content Type

A useful feature within SharePoint is the Lookup field.  This allows you to retrieve information from one list and display it as drop down list within another field.

Within SharePoint 2010 you can also pull through additional information about the field selected within the lookup dropdown.

Content Type

Within the content type declare the field reference for the Lookup.  Also declare an additional field references for the extra information you want to retrieve for the other list.

<FieldRef ID="{5955A1CD-3EF5-4D58-9DA6-628D0C997CD2}" Name="Employee" />
<FieldRef ID="{8BFAD894-D22C-4AED-91A2-2A75732899EC}" Name="EmployeeID" />

Schema

Within the schema for the content type you need to identify the list that the information is going to be retrieved from and the fields that you wish to display.

<Field ID="{5955A1CD-3EF5-4D58-9DA6-628D0C997CD2}" Type="Lookup" DisplayName="Employee" List="Lists/EmployeeDetails" ShowField="FullName" Name="Employee"/>
<Field ID="{8BFAD894-D22C-4AED-91A2-2A75732899EC}" Type="Lookup" DisplayName="ClockNo"List="Lists/EmployeeDetails" ShowField="ID" FieldRef="5955A1CD-3EF5-4D58-9DA6-628D0C997CD2" Name="EmployeeID" />

Key Items

Type="Lookup"

The type attribute determines the SharePoint field type.  For other field type see table bellow.

SharePoint Field

Type

Addition Info

Single Line of Text Text  
Multiple Lines of Text Note  
Date and Time DateTime Use Format tag to determine within is DateOnly or full DateTime
Choice Choice Use Format tag to determine display type: Dropdown, RadioButtons
Lookup Lookup  

List="Lists/EmployeeDetails"

The List attribute is used within a lookup field to determine which list the information is going to come from.  You can also specify the unique Guid for the list in here but be aware that it will change each time a list is deployed.

ShowField="FullName"
ShowField="ID"

The ShowField attribute is used within a lookup field to determine which pieces of information are to be retrieved for the specified list.

FieldRef="5955A1CD-3EF5-4D58-9DA6-628D0C997CD2"

Within any Lookups fields that have been created to pull back additional information you need to reference the main lookup field.  I have found that this works best if you omit any curly brackets from this reference.

Friday, 10 June 2011

Create custom SharePoint theme .thmx using PowerPoint

Within SharePoint 2010 there is the ability to set themes against sites.  This provides a quick way of changing the colours of certain areas of the site like the quick lunch,  navigation bar and hyperlinks.

It turns out that SharePoint can use the same file format for its theme as PowerPoint uses to brand slides.  Therefore we can use PowerPoint to create the .thmx file and use the resulting file within SharePoint.

Step 1

Open up PowerPoint.  Select the Design Tab then Colors.

PowerPoint - Theme

Step 2

Within Colors click “Create New Theme Colors”

PowerPoint - Colors

Step 3

You can now start creating you own custom theme.  Notice that the Theme options are the same as provided within SharePoint.

PowerPoint Create Theme

Give your newly created theme and name and click save.

Step 4

You now need to save the theme as a .thmx file which you can use within SharePoint.

Save Theme

Step 5

Now that you have a .thmx file containing your theme you can import in into your SharePoint site.

Within Site Settings you should see a Theme link within the Galleries grouping.

SharePoint Theme

Within here you can upload your .thmx file to SharePoint.

Step 6

Now that the theme is within SharePoint you can make use of it through the UI or you can reference it from code.

using (SPSite curSite = new SPSite("http://sharepoint"))
{
    using (SPSite curWeb = curSite.OpenWeb())
    {
        ReadOnlyCollection<ThmxTheme> siteThemes
            = ThmxTheme.GetManagedThemes(curSite);
        foreach (ThmxTheme theme in siteThemes)
        {
            if (theme.Name == "SharePointCustomTheme")
            {
                theme.ApplyTo(curWeb, true);
                break;
            }
        }
    }
}

Thursday, 9 June 2011

SharePoint 2010 Comments within Content Types

Recently I came across a content type which had comments within the field references. 

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ContentType ID="0x01005B46F6F81C7447289D88DC03731379DF" Name="Content Type Name" Description="My Content Type" Group="My Content Types">
    <FieldRefs>
      <FieldRef ID="{81d3f215-b0e8-47d0-b9fc-42687f849c85}" Name="Field1" DisplayName="Field 1" />
      <!--<FieldRef ID="{ca10fe52-eee8-4e0a-bfc5-7b77db491dc5}" Name="Field2" DisplayName="Field 2" />-->
      <FieldRef ID="{798458ED-612F-45BE-AF77-5AB09A769270}" Name="Field2" DisplayName="Field 2" />
    </FieldRefs>
  </ContentType>
</Elements>

The content type deployed as expected, however, when you created a content type which inherited from this content type only the fields above the comment are pulled through into the inherited content type.

Monday, 6 June 2011

Visual Studio - Debugging SharePoint

In order to debug SharePoint within Visual Studio you need to attach visual studio debugger to the appropriate process.

You can attach the debugger to the process through:


The process you need to attach visual studio to depends on the type of solution you are developing.

Solution Type Process
Web Part w3wp.exe
Visual Web Part w3wp.exe
Application Pages w3wp.exe
Timer Jobs owstimer.exe
Sandbox solution spucworkerprocess.exe

SharePoint Timer Jobs
If you want to debug a SharePoint Timer Job ensure that the SharePoint Timer Service is running then attach the debugger to the owstimer,exe process within Visual Studio.
Within Central Administration navigate to:


Run the desired timer job and after a second or two the debugger should enter the break point within the timer job.

Saturday, 4 June 2011

SharePoint 2010 - Changing Authentication Method

Recently I was tasked with setting up claims based authentication on a pre existing web application that was created using classic mode authentication. 

When I checked the authentication settings through the UI I discovered that their was no way of changing the authentication method to claims based authentication.

AuthenticationType

When editing the authentication type the Forms Authentication type was disabled.

On Microsoft's TechNet site I found the following article which outlines instruction on how to change the authentication method through PowerShell.

The PowerShell outlined in the TechNet article is detailed bellow:

$WebAppName = "http://sharepoint"
$account = "<domain>\<username>"
$wa = get-SPWebApplication $WebAppName

Set-SPwebApplication $wa -AuthenticationProvider (New-SPAuthenticationProvider) -Zone Default

$wa = get-SPWebApplication $WebAppName
$account = (New-SPClaimsPrincipal -identity $account -identitytype 1).ToEncodedString()


$zp = $wa.ZonePolicies("Default")
$p = $zp.Add($account,"PSPolicy")
$fc=$wa.PolicyRoles.GetSpecialRole("FullControl")
$p.PolicyRoleBindings.Add($fc)
$wa.Update()


$wa = get-SPWebApplication $WebAppName
$wa.MigrateUsers($true)



Important



  • I would recommend taking a backup of the site before applying this PowerShell script.

  • It should also be noted that this process is one way and you cannot revert back to using classic authentication

  • Read the TechNet article

Configuring Claims Based Authentication

While developing a recent SharePoint web application it became apparent that there would be two different user groups.  Users with existing windows accounts and external users.  Time to configure Claims Based Authentication.

ASP.NET Membership Database

Run the following command line statements to launch the ASP.Net SQL Server Setup Wizard:

cd %WinDir%\Microsoft.NET\Framework\v2.0.50727
aspnet_regsql.exe



aspmembershipwizard



Web Configuration


In order to configure claims based authentication you need to make changes to three web config files.



  • Web Application
  • SharePoint Central Administration v4
  • Security Token Service

The web config file for the Security Token Service can be found at:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\WebServices\SecurityToken
Web Application : Web Config
Role Manager



<roleManager enabled="true" defaultProvider="c" cacheRolesInCookie="false">

    <providers>

        <clear />

        <add name="c" type="Microsoft.SharePoint.Administration.Claims.SPClaimsAuthRoleProvider, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />

        <add connectionStringName="aspmembershipConn" applicationName="/" name="ClaimsBasedRoleManagerName" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

    </providers>

</roleManager>


Membership



<membership defaultProvider="i">

    <providers>

        <clear />

        <add name="i" type="Microsoft.SharePoint.Administration.Claims.SPClaimsAuthMembershipProvider, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />

        <add name="ClaimsBasedMembershipProviderName" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" applicationName="/" connectionStringName="aspmembershipConn" enablePasswordReset="true" enablePasswordRetrieval="false" passwordFormat="Hashed" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" />

    </providers>

</membership>



Connection String





<connectionStrings>

    <clear />

    <add name="aspmembershipConn" connectionString="Data Source=servername;Integrated Security=SSPI;Initial Catalog=databasename;" />

</connectionStrings>




Add <Clear/> tags to connection string, membership and role providers or you might get conflicts with machine.config


Central Administration : Web Config





<roleManager>

    <providers>

        <add name="ClaimsBasedRoleManagerName" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" applicationName="/" connectionStringName="aspmembershipConn" />

    </providers>

</roleManager>

<membership>

    <providers>

        <add name="ClaimsBasedMembershipProviderName" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" applicationName="/" connectionStringName="aspmembershipConn" passwordFormat="Hashed" />

    </providers>

</membership>




Security Token Service : Web Config




<roleManager enabled="true" defaultProvider="c">

    <providers>

        <add name="c" type="Microsoft.SharePoint.Administration.Claims.SPClaimsAuthRoleProvider, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />

        <add connectionStringName="aspmembershipConn" applicationName="/" name="ClaimsBasedRoleManagerName" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

    </providers>

</roleManager>

<membership defaultProvider="i">

    <providers>

        <add name="i" type="Microsoft.SharePoint.Administration.Claims.SPClaimsAuthMembershipProvider, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />

        <add name="ClaimsBasedMembershipProviderName" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" applicationName="/" connectionStringName="aspmembershipConn" enablePasswordReset="true" enablePasswordRetrieval="false" passwordFormat="Hashed" requiresQuestionAndAnswer="true" requiresUniqueEmail="true" />

    </providers>

</membership>



After you have configured the web config files remember to set permission on the asp membership database for the account the app pool of SharePoint is using so it can access the database.

Friday, 3 June 2011

SharePoint 2007- ASP:Chart

In a recent project one of the requirements was to produce a chart from 
data held within a SharePoint list.  
 
To fulfil this requirement I used the asp:Chart control.  The following steps outline how to set this up:
 
Setup
 
Namespace:


<%
@ Register Assembly="System.Web.DataVisualization,

                      Version=3.5.0.0, Culture=neutral,

                      PublicKeyToken=31bf3856ad364e35"

             Namespace="System.Web.UI.DataVisualization.Charting"

             TagPrefix="asp" %>


Control:
 

  1. <asp:Chart ID="Chart1" runat="server" />


Web Config:
Add the following tag within the <pages><controls>


<
add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />


Add the following tag within <httpHandlers>

<add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />

Useful Methods

Set Point:
private void SetPoint(string Series, int pointX, int pointY)
{
Chart1.Series[Series].Points.AddXY(pointX, pointY);
}

Set Legend:

private void SetLegend(string Series, string LegendText) 
{
Chart1.Series[Series].LegendText = LegendText;
}






Wednesday, 1 June 2011

SharePoint - asp:ChangePassword control

While using the asp:ChangePassword control within a SharePoint application page I was getting the following exception.

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

Using Fiddler I tracked this error down to the .ASPXAUTH cookie.   Setting this cookie back to null fixed this issue for me.

  1. Call code behind method onChangedPassword

 <asp:ChangePassword ID="cpUser"
MembershipProvider="MembershipProviderName"
OnChangedPassword="cpUser_PasswordChanged"
DisplayUserName="true" runat="server">
</asp:ChangePassword>




    2.  Clear .ASPXAUTH cookie




protected void cpUser_PasswordChanged(object sender, EventArgs e){
if (Request.Cookies[".ASPXAUTH"] != null)
{
HttpCookie myCookie = new HttpCookie(".ASPXAUTH");
myCookie.Expires = DateTime.Now.AddDays(-1d);
myCookie.Path = ApplicationName();
Response.Cookies.Add(myCookie);
}
}


Powershell - Security Token Timeout

A requirement of a recent project was to modify the time out of the security token for claims based authentication users.
  • Open SharePoint Powershell window
  • Run cmdlet:  Get-SPSecurityTokenServiceConfig

  • Run cmdlet:  Set-SPSecurityTokenServiceConfig -FormsTokenLifetime 5

Parameter FormsTokenLifetime specifies in minutes the length of time the security token for a forms based authentication user will remain active.


WindowsTokenLifetime performs a similar job for windows authentication users.