Need a Thumbnail Field in the Document library for the uploaded picture… Then read this post.

One of my customer came up with a requirement that he would like to add the “Thumbnail” computed field to other lists as well, where it is by default only available with picture library. Customer checked the site column gallery for “Thumbnail” field but he couldn’t able to find it to add it to the list. Now his requirement is to know how to add the “Thumbnail” field to other list.

Before getting into the work around in detail, First I would like to give some insight about Thumbnail field and then the description of the solution.

We are already aware that “Thumbnail” is the registered COMPUTED site column and the reason why they are available in the “Picture” library is because the “thumbnail” field has been included in the “Picture Library” schema definition. So whenever you creates a picture library from OOB “Picture Library” template then the thumbnail field gets created along with the picture library instance. The reason why the Thumbnail field is not available in the site column is because it is placed under the Hidden group and it’s hidden by default. With this said and explained, now we already knows that “Thumbnail” is a computed field and hence it has several other field references defined inside it. They are “ ImageWidth”,”ImageHeight”,”FSObjType”,”EncodedAbsThumbnailUrl” and “Description”. So if you want to include the “Thumbnail” field into a list then you need to ensure that these field references should be already existing in the list. So in short, before adding the Thumbnail field one need to add these fields to the list else the Thumbnail field won’t work and will throw an error.

Again if you would like to add these fields individually from site column gallery, None of these fields which used as field references for thumbnail field will be available in the Site column gallery to add. If you are thinking about making these hidden fields to visible through SharePoint API’s using script then there is another hurdle that you can’t add “EncodedAbsThumbnailUrl” to other lists as it is very specific to picture library and it holds the thumbnail images in a web folder which will be only available with picture library. So only adding these fields individually to the list and then adding OOB thumbnail field will not help to achieve your requirement.

So the work around comes in the following format, From SharePoint architectural point of view instead of adding the individual field references required for “Thumbnail” field you can add the OOB “Picture” content type to the list thus we can get the required field references for “Thumbnail” field. One can think that adding the “Picture” content type would be sufficient enough as it contains all the required field and as well the “Thumbnail” field. But as I already told, the “EncodedAbsThumbnailUrl”,”Thumbnail” and some other computed fields in the picture content type are very specific to Picture library and they won’t be included to other lists. But it’s mandatory to add the “Picture” content type to get the picture schema and some other supported fields.

With the so far details, we understand that to add the “thumbnail” computed field to the list we need following 5 field references to be included first in the list :

       1. “ImageWidth” – Gets included when you add the picture content type

       2. “ImageHeight” - Gets included when you add the picture content type

       3. “FSObjType” – This field is available in the BASE TYPE and will be available to all the lists so no need to include.

       4. “EncodedAbsThumbnailUrl” – This field is very specific to Picture library and will not be included to other list just by adding the Picture content type.

       5. “Description” – Gets included when you add the picture content type.

So now we have been without “EncodedAbsThumbnailUrl” field which is very important to compute the “Thumbnail” field as it holds the “URL” of the image. So we need to find a way to render the thumbnail image… We have a Global field called “EncodedAbsUrl” which is available as a BASE TYPE field and will be included to all the lists by default. We can use this field instead of “EncodedAbsThumbnailUrl” which is only specific to picture library.

So we need to create a “Thumbnail” field in the list which is going to be computed from “ ImageWidth”,”ImageHeight”,”FSObjType”,”EncodedAbsUrl” and “Description”. If you add “Picture” content type to the list then “ImageWidth”, “ImageHeight” and “Description” will be available. “FSObjType” and “ EncodedAbsUrl” fields will be available to all the lists by default as they are coming from Base type.

Now, how to create the “thumbnail” field in the list. We can’t simply include the OOB “Thumbnail” Site column through code as the OOB “Thumbnail” column is going to be computed using “EncodedAbsThumbnailUrl” which will not work except Picture library. So we need to define our “Thumbnail” field in such a way that it can be calculated using “EncodedAbsUrl”. Thus we can make use of “AddFieldAsXml” method of SPList object to create the Thumbnail field from the definition.

Following are the steps for the work around :

1. Enable the content type in the list and add the “Picture” content type to the list. Make the picture content type as default content type to the list, If you don’t want the picture content type as default content type then you can change it once you added the Thumbnail field to the list. (Note : The picture content type cannot be added to all the list types)

2. Now define the “Thumbnail” field definition as follows (Note : The only change from the OOB Thumbnail Site column definition and the given definition is that we replaced the “EncodedAbsThumbnailUrl” with “EncodedAbsUrl” ). Create this field definition and save it as a .txt file.

<Field ID="{ac7bb138-02dc-40eb-b07a-84c15575b6e9}" ReadOnly="TRUE" Type="Computed" Name="Thumbnail" ShowInNewForm="FALSE" ShowInFileDlg="FALSE" ShowInEditForm="FALSE" DisplayName="Thumbnail;" Sealed="TRUE" Sortable="FALSE" Filterable="FALSE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Thumbnail">

            <FieldRefs>

                  <FieldRef Name="ImageWidth" />

                  <FieldRef Name="ImageHeight" />

                  <FieldRef Name="FSObjType" />

                  <FieldRef Name="EncodedAbsUrl" />

                  <FieldRef Name="Description" />

            </FieldRefs>

            <DisplayPattern>

                  <IfEqual>

                        <Expr1>

                              <LookupColumn Name="FSObjType" />

                        </Expr1>

                        <Expr2>0</Expr2>

                        <Then>

                              <IfEqual>

                                    <Expr1>

                                          <LookupColumn Name="ImageWidth" />

                                    </Expr1>

                                    <Expr2>

                                    </Expr2>

                                    <Then>

                                    </Then>

                                    <Else>

                                          <IfEqual>

                                                <Expr1>

                                                      <LookupColumn Name="ImageWidth" />

                                                </Expr1>

                                                <Expr2>0</Expr2>

                                                <Then>

                                                </Then>

                                                <Else>

                                                      <HTML><![CDATA[<a href="]]></HTML>

                                                      <URL Cmd="Display" />

                                                      <HTML><![CDATA["><img border=0 ALT="]]></HTML>

                                                      <IfEqual>

                                                            <Expr1>

                                                                  <Column Name="Description" />

                                                            </Expr1>

                                                            <Expr2>

                                                            </Expr2>

                                                            <Then>

                                                                  <HTML><![CDATA[$Resources:core,Thumbnail;]]></HTML>

                                                            </Then>

                                                            <Else>

                                                                  <Column Name="Description" HTMLEncode="TRUE" />

                                                            </Else>

                                                      </IfEqual>

                                                      <HTML><![CDATA[" SRC="]]></HTML>

                                                      <Field Name="EncodedAbsUrl" />

                                                      <HTML>

                                                            <![CDATA[">

                                        </a>

                                        ]]>

                                                      </HTML>

                                                </Else>

                                          </IfEqual>

                                    </Else>

                              </IfEqual>

                        </Then>

                  </IfEqual>

            </DisplayPattern>

   </Field>

3. Create a Visual Studio Console or Windows application and compile the following code and execute it to add the “Thumbnail” field defined in the above step in the list.

using (SPSite osite = new SPSite("URL of your SharePoint Site”)

            {

                using (SPWeb oweb = osite.OpenWeb())

                {

                    SPList olist = oweb.Lists["YourListName"];

                    StreamReader rdr = new StreamReader("Definition text file path which has the field definition");

                    string fld = rdr.ReadToEnd();

                    olist.Fields.AddFieldAsXml(fld, true, SPAddFieldOptions.AddToDefaultContentType);

                    olist.Update();

                }

            }

4. Now upload a picture using “Picture” content type in the list and you can find the Thumbnail rendered in the view.

It’s SharePoint’s OOB by design behavior that the “Thumbnail” should be available only to the picture library and it makes sense too. So the work around provided is a sample steps to achieve the requirement of bringing the thumbnail field to other document library when the picture content type is enabled with it.

HAPPY CUSTOMIZING :)