Blogs

Blogs

SharePoint: Adding a Publishing Image Field Programmatically

16th July 2014

The Issue

Here at PSP we produce custom solutions for SharePoint 2010 and 2013 for many of our clients. These solutions range from themes to fully migrated solutions.

One thing that crops up in most of our solutions is the ability to add an image column to either a list or library.

Easy you say… just add a ‘Hyperlink or Picture’ column.

And in some sense you would be correct. But are we the only people that find this field type somewhat disappointing? Why should the user have to locate the URL? Why can’t they just pick the image from a selector? What if the image hasn’t been uploaded yet?



These are the type of questions that are raised during UX testing and it is often unacceptable to offer the standard ‘that’s how SharePoint works’ response. What adds more fuel to the fire is when users familiar with SharePoint have seen the ability to pick or upload images while editing list items.



This is where the Publishing Image Field comes in to play. This field type allows the user to insert a picture from SharePoint via a friendly UI and also allows the upload of images at the same time thus eliminating the hassle generated when using the ‘Hyperlink or Picture’ field.

But I can’t find a SPField type for this! How do you add the column? Why is it so difficult to do something so simple? I HATE SHAREPOINT!!!

All of us in the SharePoint dev industry have felt your pain at some point or another, but don’t despair, there is a solution and it is surprisingly simple and that you are ready to add your field to the Fields collection.

The Solution

The code below assumes that you have the SPList object and that you are ready to add your field to the Fields collection.

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing.Fields;
{}

ImageField field = null; 
 // Check to see if the list already contains the field.
 if (!list.Fields.ContainsField("My Image Field"))
 {
     // Create a new field using the Image field type. The field type is specified
     // by name as there is not a SPField object definition available.
     field = new ImageField(list.Fields, "Image", "MyImageField");    
     // Set the field to support Rich Text in Full HTML mode.
     field.RichText = true;
     field.RichTextMode = SPRichTextMode.FullHtml; 
     // Add the field to the fields collection.
     list.Fields.Add(field);
 } 
 // Pull back a fresh instance of the field from the list. This is done because of
 // the way in which objects become dirty.
 field = list.Fields.GetFieldByInternalName("MyImageField") as ImageField; 
 // Check to ensure that the field has been retrieved successfully.
 if (field != null)
 {
    // Set the normal field properties (Title, Description, Required, etc...).
     field.Title = "My Image Field";
     field.Description = "A publishing image field";
     field.Required = true; 
     // Update the field
     field.Update();
 }

If you have added Publishing Image Fields to lists before you may have used lighter code than this and at first we did the same until we hit an issue with Internal Names.

Many code examples just create the field and pass through the title when creating the new Image field and let SharePoint create the Internal and Static names. When the title doesn’t contain any spaces this method work fine. However when the title does contain spaces, SharePoint replaces the spaces with its own encoded space characters and can then make it confusing when trying to retrieve the field later down the line.

With the solution above, we create the field using the Internal name and then replace the title after. This causes SharePoint to create the Internal and Static names based on the internal name that you specify (we avoid using spaces in internal and static names with a view to eliminating issues when trying to retrieve fields at a later time). We then retreive a clean instance of the field from the list and then set the title as required.

The finished product is a list that uses the Publishing Image Field allowing users to select an existing image from SharePoint as well as upload an image if it is does not already exist.

And this wraps up this post. I hope this tutorial is helpful and as usual, constructive critisism and suggestions are welcome.

This website uses cookies to provide the best possible experience. By continuing to use our website you are agreeing to our use of cookies.