Archive for the ‘C#’ Category

The accepted answer of this Stack Overflow post offers a pretty simple technique for extending IPrincipal to modify the display of the “Hello <username>” link in the _LoginPartial view.  In my case, username is an email address, so lengthy email addresses were taking up a lot of room on the top Navigation section, and I wanted something shorter (and sweeter).

The approach involves creating a custom claim during login and setting the desired value there, then retrieving the value via an extension method that reads the claim and returns the value. The partial view is updated to call the extension method instead of the default when creating the ActionLink for password modification.

This solution relies on having already extended the default ApplicationUser implementation to include additional properties such as FirstName, LastName, or FullName.

I’ve enabled tracing as part of an instrumentation effort in MVC 5 using Glimpse and ELMAH. I was surprised to find on one particular page that I was seeing traditional ASP.Net page lifecycle events being chronicled in my Trace output. (more…)

While extending the Identity 2.1 ApplicationUser class, I noticed that my newly-added string fields were being converted to nvarchar(max). I was OK with nvarchar type, but wanted to field length constraints.

Having earlier needed to force a field type to SQL DateTime2, I simply tried the same thing with [Column(TypeName=”nvarchar(##)”)], but that blew up the migrations with a System.InvalidOperationException with the message “Sequence contains no elements”.

It turns out that it was sufficient to simply add the MaxLength(##) annotation instead.

I also needed to add the AutomaticMigrationDataLossAllowed = true property in my Configuration() constructor to allow the migration to run, as it detected the potential for truncation of my existing data.

This post helped with the resolution.

Matt Randle shows you how, using the Watch window.

I’ve been working with WebAPI to implement a simple REST data service over http: as part of prototyping a client-side connection to an external system from SharePoint 2010.

I was able to get the data service running and accessible in Azure very easily due to some excellent tutorials from MSDN, and was able to bind the JSON data from my service to a simple chart from AmCharts that worked well in a test page. (more…)

If you are using the UpdateListItems() method of the Lists web service (Lists.asmx) to create or update large numbers of ListItems from your InfoPath code-behind project, you may potentially exceed the 160 item update limit that is documented for this method. 

To accommodate this limit, you will have to implement some sort of batching technique to break up your items into acceptably-sized update chunks. In my case, I am writing “Time Records” for individual employees from a “Daily Attendance” form into a SharePoint list that accumulates the daily records for each employee when the InfoPath form is submitted. (more…)

In order to delete every item from a SharePoint list using the Client Side Object Model (CSOM), it will likely be necessary to implement paging using the ListItemCollectionPosition property to avoid exceptions related to the size of the query/response.

While some of the old MSDN documentation recommends retrieving sets of up to 2000 items, I ran into problems with numbers anywhere near that large, before finally settling on a row limit of 250. Also, it doesn’t seem possible to optimize the Load method via lambda expression to retrieve just the ID field and ListItemCollectionPosition property. Maybe it is possible, but I gave up when it returned a MethodNotSupported (or something like that) exception. (more…)

This post could also be called “BizSupportOnline Saves The Day Once Again”, as the benefits of and technique for converting a button to a Submit button came from there.

But first a little background. I have a form that used code-behind triggered by rules on submit to create new items in two lists and to update existing data in another list. The rules would toggle values in a secondary data source, which would trigger Change events to execute my data creation/update code. (more…)

I ran into this exception today when trying to reconstruct a published form with a backup of its code-behind project. The exception and message was as follows:

“A first chance exception of type ‘System.Collections.Generic.KeyNotFoundException’ occurred in mscorlib.dll.The given key was not present in the dictionary.”

The project backup was a little out-of-date compared to the form, and  one of the secondary data connections that the form uses to write data back to a list had been renamed.

As a result, when it hit the line:

DataConnections[“<Name of Data Connection>”].Execute();

it blew up and threw the exception. After a little bit of head-scratching, I noticed that the name of the data connection had changed, so simply renaming it in code took care of the problem.

EPPlus is a handy library on CodePlex that makes it easier to work with the Open Office XML format for Excel, especially without relying on the installation of Excel on a server environment.

I have been using it to write out site audits of SharePoint sites to a spreadsheet, then using some of the formatting capabilities to group, highlight, and improve viewability of the data.  (more…)