Uncategorized Posts

Adding Validation MetaData to Entity Framework Auto Generated Classes

Entity Framework’s Code First is one of my favorite tools, but in my experience it is somewhat uncommon to build a database from scratch. More often you are accessing and editing data in an existing database, or modifying its schema. Thankfully, Entity Framework 6.1 introduced “reverse code first”, which is more officially known as “Code First From Database”. This pattern allows you to choose an existing database, and Entity Framework will generate the appropriate entities and DbContext classes for it. This can also be a great option if you are a fan of code first syntax but still prefer to design your databases through SQL or Management Studio.

When you first start using Code First From Database, however, it won’t be long until you run into a problem. The framework generates correct entity classes for you based on your database tables, but what happens when you want to add additional meta data to these classes? Maybe you want to enforce certain model validation rules that don’t actually exist in the database. How do we add this meta data? Sure, you can add them directly to the classes Entity Framework generates, but if the database changes and you need to regenerate the classes, you’ll loose these updates. There must be a better way!

Creating a Publishing Field Declaratively and Programmatically in SharePoint

I recently came across a task in SharePoint that nearly drove me insane. I was creating a publishing layout template and I needed to add some “Publishing HTML” fields so the user would have full editing control inside that editable field. Doing this declaratively using a SharePoint feature in Visual Studio is simple enough – some example code is below:

<?xml version="1.0" encoding="utf-8"?>
  <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Field ID="{your-guid-here}" Type="HTML" RichText="TRUE" RichTextMode="FullHTML" Name="YourInternalName" DisplayName="Your Display Name" Group="YourGroupName"></Field>

Be sure to use the type “HTML”. This type is not included on many of the lists that describe all of the types in SharePoint, because a lot of those only cover SharePoint foundation and this is part of the Publishing Infrastructure of SharePoint Server. Remember, you can generate your own guids inside of Visual Studio under “Tools”. Also, remember to not include spaces in your internal name for cleanliness (spaces get converted to %20 behind the scenes).

The harder part is how to create this programmmatically. I haven’t tried this in SharePoint 2013, but 2010 seems to have a bug that does not let you instantiate the HTMLField class correctly, which limits your ability to use the straight server object model. Every time I attempted this I received the dreaded “column failed to install correctly error. Avoid this.

Instead, try creating the column programmatically using the AddFieldAsXML method of the SPFieldsCollection class, and then passing in the declarative XML that we know works. Make sure to escape your quotes, as seen below.

Adding an RSS Feed to Sharepoint Using a Web Part

Note: This tutorial assumes at least basic knowledge of creating and deploying SharePoint Solutions using Visual Studio

A common request I have seen crop up a number of times when working with SharePoint is to add an RSS feed to the site. There are a handful of SharePoint Apps out there on the SharePoint store that will do this for you, but I wanted to take a look at coding this myself and packaging it as a custom web part. After doing a little digging this turned out to be fairly straight forward. RSS feeds are obviously pretty easy to work with, so it was really just a matter of finding the most efficient methods.

Some of you may be familiar with the System.ServiceModel.Syndication Namespace that is part of the .NET library. This namespace contains some really useful classes designed to work with RSS feeds. For this example, we are going to take a look at the SyndicationFeed and SyndicationFeedItem classes with this namespace.