ASP.NET LinkedListBoxes UserControl

January 5, 2012 Leave a comment

I needed a simple Linked ListBox control for a project but couldnt find one that was available for free so I ended up developing one myself.

I have created a project for it on codeplex and published it. Please feel free to use/modify the control as you require. If you have any suggestions or feedback then please feel free to leave a comment.

The control uses two ListBoxes with two buttons (add, remove) in between that allows you to add and remove items by using some Javascript. The screen shot below shows the control in action:

LinkedListBox UserControl

You can download the complete source code from the link below:

LinkedListBox UserControl sourcecode.

You can also download a sample Visual Studio 2010 project that shows the control in action by going to the project home page and clicking on the Download button:

Project homepage

SharePoint 2010: List Fields not showing up in Edit, Display or New Forms

December 28, 2011 8 comments

I have seen a lot of people get stuck on this issue.

Look at the CAML markup in the list definition’s Schema.xml file below (this was auto-generated by the Visual Studio SharePoint ‘List Definition’ template selecting Custom List as the list type):

<List xmlns:ows="Microsoft SharePoint" Title="Custom List" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/ACustomList" BaseType="0" xmlns="">
      <ContentTypeRef ID="0x01">
        <Folder TargetName="Item" />
      <Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Type="Text" Name="Title" DisplayName="Title" Required="TRUE" StaticName="Title" FromBaseType="TRUE" />
      <Field ID="{AACF7B24-80FC-4EB6-8C73-381626D056C6}" Name="Value" StaticName="Value" Type="Note" DisplayName="Value" NumLines="6" RichText="TRUE" />

Looking at the list definition you would assume that the edit, display and new forms would display two fields (Title and Value) but in actual fact it only displays the Title field.

The reason for this is that in the markup we have specified that the list uses the Item (0x01) content type but the additional field is not part of that content type hence it does not appear in the forms.

To ensure they appear we either need to:

  • Remove the content type reference from the markup
  • Or we could create a new content type that inherits from the Item content type and include this additional field as part of its definition. We can then reference that content type in our list definition instead of the Item content type.

Other reasons that might cause your fields to not appear in edit, new and/or display forms of your list could be:

  • Field’s ShowInEditForm, ShowInDisplayForm, ShowInNewForm attributes are set to FALSE
  • Field is Hidden
  • Field is set as ReadOnly
    • Please note that this is not an exhaustive list. Hope this helps someone.

SharePoint 2010: List column that renders as a dynamic hyperlink

April 18, 2011 27 comments

A while back I worked on a project where we needed to add a column to a SharePoint List that would display a link based on values in other columns. For example: “/myWeb/pages/mycustomPage.aspx?Reference=[ValueToComeFromAnotherColumn] i.e. similar to the way the Title field link is rendered where the URL is dynamic and based on the ID of the row in question.

In MOSS 2007 we did this by adding a calculated column that constructed the HTML using our specified formula. However, the problem was that the link would appear as:

 <a href="myWeb/pages/mycustomPage.aspx?Reference=45">View</a>

To ensure it rendered as a nice and friendly hyperlink we used a script created by Christophe from (his blogs on the subject dont seem to exist anymore). It worked pretty well in MOSS 2007 but we found that in MOSS 2010 it didnt work if your list view had grouping by any column.

Initially I tried to get the script to work but then realised that there was a much better way to do this in SharePoint 2010. In 2010 you can control how any field is rendered by creating a custom XSLT stylesheet. In our scenario we created an XSL file which was deployed at “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL”. The XSL file must be named in the following format fldtypes_*.xsl, where * is any valid filename string value for example fldtypes_MyCustomDefinitions.xsl. Our XSL file looked something like this:

<xsl:stylesheet xmlns:x="" xmlns:d="" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="" xmlns:asp="" xmlns:__designer="" xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
  <xsl:template match="FieldRef[@Name='FormLink']" mode="Computed_body">
    <xsl:param name="thisNode" select="."/>
	    <a target="_blank" href="/appeal/pages/appealapplication.aspx?ApplicationRef={$thisNode/@ApplicationRef}">View Application</a>

The important bit is the match attribute in the xsl:template element where we specify the internal name of the Field we want this custom rendering applied to. What this basically does is that where ever a field with an internal name of FormLink is being rendered in a view it will render it as a hyperlink. Below is the definition for our SharePoint List Field:

<Field ReadOnly="TRUE" ID="{2DD3638A-56A0-4021-B2B9-7BBB19E191F1}" Name="FormLink" StaticName="FormLink" DisplayName="Application" Type="Computed" Group="My Custom Group" SourceID="" />

That is all there is to it, now when ever and where ever our field is rendered in a view it renders it in our custom way.

Update: For this to work, the column you are referencing must exist in the view. In my example above I am referencing a column called “ApplicationRef” therefore for it to work in my scenario the SharePoint List View I want it to work in must have “ApplicationRef” column otherwise the value wont be picked up. Hope this makes sense.

I have created a sample Visual Studio SharePoint Project that demonstrates this functionality. Please click on the link below to download it. Just deploy the solution from Visual Studio and it will create a list with a dynamic hyperlink column.


[Update: 08/10/2012] Depending on your scenario you might find this simpler solution more feasible:

0x80070057 Invalid data has been used to update the list item. The field you are trying to update may be read only.

March 22, 2011 2 comments

I came across this strange error today when I was trying to (programmatically) update a lookup column in a SharePoint list.

Below is the declaration for the lookup column in the schema.xml of the list:

<Field ID="{25b21cbb-c154-4dda-ac58-506a5853e5de}" Name="mlClientID" 
StaticName="mlClientID" Type="Lookup" DisplayName="Company" List="Lists/mcompany" FieldRef="ID" ShowField="Title" Group="Custom Columns" />

When I removed FieldRef=”ID” and then re-created the list it solved the problem.

I am not really sure why or what is going on in the background that causes this issue but I hope this helps someone incase you have the same problem.

SharePoint 2010: Set session timeout and setup sliding sessions on an FBA enabled site

January 10, 2011 48 comments

[Updated 12th of Sept 2012]

Please note that the below method is now redundant as long as you have SP 2010 April 2011 Cumulative Update installed. For more details please read this excellant blog post by Shawn Cicoria.

You can get the same to work now by running the PowerShell script below:

$sts = Get-SPSecurityTokenServiceConfig
$sts.FormsTokenLifetime = (New-TimeSpan -Minutes 60)
$sts.LogonTokenCacheExpirationWindow = (New-TimeSpan –Minutes 60)

This creates a sliding session effect whereby as soon as there is any activity before the expiry of the token it (the token) is renewed and it will usually only expire if there is 60 minutes of inactivity. If you do not have the SP 2010 April 2011 Cumulative Update installed then you can setup sliding session by following the original blog post below.

[Original Blog Post]

After struggling with this for a few days I finally figured out how to control this in SharePoint 2010.

In MOSS 2007 if you had a site that had windows authentication then you could specify the timeout by adding a timeout attribute in the sessionState element in your web.config. On the other hand if you had an FBA enabled site and wanted to control the session timeout you would do that by adding a timeout attribute in the forms element in the web.config.

The Problem

The problem is that in SharePoint 2010 the same thing does not work for a FBA enabled site. I spent a whole lot of time trying to search for a solution and ended up trying a lot of different things but nothing seemed to work. At one point I came across the powershell script below and thought I had finally found a solution:

$sts = Get-SPSecurityTokenServiceConfig
$sts.WindowsTokenLifetime = (New-TimeSpan -Minutes 60)
$sts.FormsTokenLifetime = (New-TimeSpan -Minutes 60)

However, soon I realised that this was causing the session to expire after 60 minutes regardless of the activity of the user. Eventually, I came across this very useful blog post.

The Solution

First of all we need to add some code behind to the Global.asax file of the SharePoint Web Application.

In your Visual Studio project add a new class file and call it Global.asax.cs (you can call it anything really) Then we need to add the following code to the Global.asax.cs file:

namespace MyNameSpace
    public class Global : SPHttpApplication
        /// <summary>
        /// Executes custom initialization code after all event handler modules have been added.
        /// </summary>
        public override void Init()
            FederatedAuthentication.SessionAuthenticationModule.SessionSecurityTokenReceived += new EventHandler<SessionSecurityTokenReceivedEventArgs>(SessionAuthenticationModule_SessionSecurityTokenReceived);

        /// <summary>
        /// Handles the Start event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void Application_Start(object sender, EventArgs e)

        /// <summary>
        /// Handles the SessionSecurityTokenReceived event of the SessionAuthenticationModule control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Microsoft.IdentityModel.Web.SessionSecurityTokenReceivedEventArgs"/> instance containing the event data.</param>
        void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e)
            // the timeout comes from the web.config
            int configuredTokenTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["SessionTokenTimeout"]);

            DateTime now = DateTime.UtcNow;
            DateTime validFrom = e.SessionToken.ValidFrom;
            DateTime validTo = e.SessionToken.ValidTo;

            long timeLeft = e.SessionToken.ValidTo.Subtract(now).Ticks;
            TimeSpan tokenLifeTime = validTo.Subtract(validFrom);
            TimeSpan configuredTokenTimeSpan = new TimeSpan(0, configuredTokenTimeout, 0);
            // 37200000000 = 62 minutes
            if ((tokenLifeTime.Ticks > configuredTokenTimeSpan.Ticks) || (timeLeft * 2) < tokenLifeTime.Ticks)
                SPSessionAuthenticationModule spsam = sender as SPSessionAuthenticationModule;
                e.SessionToken = spsam.CreateSessionSecurityToken(e.SessionToken.ClaimsPrincipal, e.SessionToken.Context,
                    now, now.AddMinutes(configuredTokenTimeout), true);
                e.ReissueCookie = true;

Next we need to link this code behind to the Global.asax file.

Browse to the root of your Web Application Folder and open up the Global.asax file and add the following (modify the application tag with your specific assembly details):

<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Application Language="C#" Inherits="MyNameSpace.Global,MyAssembly, Version=, Culture=neutral, PublicKeyToken=d4469032123b3e99a"%> 

Deploy your solution.

In the example above I am subscribing to the SessionSecurityTokenReceived event. In it I have some custom logic to implement a sliding session. Basically what I am doing is that I discard the original session token (which always seems to have a lifetime of 10 hours) and I create a new session token that has the lifetime I want. In my implementation I also added a key in the appsettings section of the web.config that controls the session timeout value:

    <add key="SessionTokenTimeout" value="60" />

Finally, I found that you need to run the following powershell script below as well to make the whole thing work:

$sts = Get-SPSecurityTokenServiceConfig
$sts.LogonTokenCacheExpirationWindow = (New-TimeSpan –seconds 1)
// set it to false for better client integration (thanks to Anirudh for pointing this out)
$sts.UseSessionCookies = $false

This is the only way I found that allowed me to control the Session Timeout in 2010. Please feel free to leave a comment if you have found a better and easier solution.

Custom Action to Trigger a SharePoint Timer Job

November 27, 2010 Leave a comment

In my last post I demonstrated how to create a SharePoint Custom Action to appear on a specific SharePoint List. When a user clicked the action it ran some of our custom code.

Today we will try to extend that example and run a SharePoint Timer Job on the click of our custom action.

I am making the following assumptions:

  • You have read my previous post and have created the custom action
  • You know how to create and deploy Custom Timer Job definitions

The Problem

SharePoint Timer Job instances are persisted in the configuration database. Typically the SharePoint Farm service account or other accounts that have been given permissions on this database explicitly are able to write to this database.

For this reason when you try to execute a SharePoint Timer Job from within a site collection it will generally fail even if you run it within a SPSecurity.RunWithElevatedPrivileges code block. Running with elevated priviledges results in the code being run in the context of the Application Pool account (referred to as the System Account by SharePoint). Unless you have deviated from the best practices guidelines this will result in the following error:

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Access denied.

You could always get around this by explicitly giving the Application Pool account permissions on the Configuration Database or by setting the Farm account as also being the Application Pool account for your Web Application. However, both these approaches are not recommended and are a deviation from the best practices guidelines.

The Solution

In my previous blog post, we created a custom action and deployed it. We used our custom action to send the user to a custom aspx page (with code-behind) that was located in the layouts folder. In the code behind we ran the custom code we wanted to execute. In this way we were able to create a custom action that ran some custom code on the click of our custom action.

To use our custom action to trigger a SharePoint Timer Job we need to add the following code in the aspx code-behind page:

namespace MyProject.CustomJobDefinitions
    public class JobInitiator: SPJobDefinition
        public JobInitiator()
            : base()
        public JobInitiator(string jobName, SPService service, SPServer server, SPJobLockType targetType)	 
	            : base(jobName, service, server, targetType)
        public JobInitiator(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.None)
            this.Title = "JobInitiator";
         public override void Execute(Guid targetInstanceId)
            using (SPSite site = this.WebApplication.Sites["/"])
                using (SPWeb web = site.RootWeb)
                    if (web.Properties["InitiatorJobFlag"] != null)
                        // Reset the flag 
                        Helper.SetSiteProperty(site, "InitiatorJobFlag", null);
                        // This is the job we want to run that does some stuff. It could be any job we want to run
                        CustomJobToRun process = new CustomJobToRun("My Job", WebApplication);

And below is the very useful ‘SetSiteProperty’ helper method:

        public static void SetSiteProperty(SPSite site, string propertyName, string value)
            bool unsafeUpdateValue = site.RootWeb.AllowUnsafeUpdates;
            site.RootWeb.AllowUnsafeUpdates = true;
            if (site.RootWeb.Properties.ContainsKey(propertyName))
                site.RootWeb.Properties[propertyName] = value;
                site.RootWeb.Properties.Add(propertyName, value);

            site.RootWeb.AllowUnsafeUpdates = unsafeUpdateValue;

Now that we have created our Custom Timer Job Definition, we can create a feature (scoped at Farm or WebApplication) and in the FeatureReciever feature activated event we add the following code:

JobInitiator job = new JobInitiator("JobInitiator", webApp)
                Schedule = new SPMinuteSchedule
                    BeginSecond = 0,
                    EndSecond = 59,
                    Interval = 1

The schedule above ensures that our Custom Timer Job will run every minute. So our timer job executes every single minute and checks a property on the RootWeb of our Site Collection. If this property is set then it runs the specified Job of our choice.

And then finally in the code-behind file of our aspx page we add the following code:

        protected override void OnLoad(EventArgs e)
            Helper.SetSiteProperty(SPContext.Current.Site, "InitiatorJobFlag", "RunNow");

So there we have it, by following this way we were able to create a Custom Action that, when clicked, triggers any SharePoint Timer Job we would like to run.

You might wonder why am I using a Job to execute another Job? Well there could be a scenario where you might want to run an OTB SharePoint Timer Job in which case this way would work.

In my specific scenario I had created a Job that was scheduled to run once every day. By using the above method I can easily trigger the execution of this Job on demand without interfering with its normal schedule.