skip to Main Content

I have the following method that creates a "hyperlinkOrPicture" column as it is referred to by the Graph API, which is essentially just a URL. The Sharepoint API refers to the column type by the FieldTypeKind value.

async function createHyperlinkOrImage(sharePointAPIaccessToken, listId, columnName, isImage) {
    const sharePointApiUrl = `${process.env.SITE_URL}/_api/web/lists(guid'${listId}')/fields`;

    // FieldTypeKind = 11 => URL type
    // DisplayFormat = 0 => Hyperlink, 1 => Image
    const sharePointPayload = {
        "__metadata": { "type": "SP.FieldUrl" },
        "Title": columnName,
        "FieldTypeKind": 11,
        "DisplayFormat": isImage ? 1 : 0,
    };

    try {
        const response = await fetch(sharePointApiUrl, {
            method: "POST",
            headers: {
                Authorization: `Bearer ${sharePointAPIaccessToken}`,
                Accept: "application/json;odata=verbose",
                "Content-Type": "application/json;odata=verbose",
            },
            body: JSON.stringify(sharePointPayload),
        });
        // error handling 
    }
       
}

The columns are created properly. It handles when the column is purely a hyperlink (with isImage set as 0), the problem is with Image columns (isImage set as 1): uploading an image, it is not rendered in the cell, which does happen when creating a column manually: "Add Column" -> "Image". I’ve included a side by side comparison of the two below:

enter image description here

To the left, is the column generated via the REST API, and to the right is through the UI.

One big difference, which I believe is the reason for why this is happening is when viewing the properties of the columns, we see that the REST API generated images column does not specify the ‘type’.

When opening Column Settings I see the following:

enter image description here

And for the manually set column:

enter image description here

Lastly, I noticed that I am unable to add the images directly when on the Add new item page:
enter image description here
enter image description here

I cannot tell what Sharepoint is recognizing the column as, clearly from the image icon next to the column name it should be rendered as an Image Column. However, it is behaving as a hyperlink column.

Thank you!

2

Answers


  1. Chosen as BEST ANSWER

    The following is from the results of a GET request for the list I have created above to the following endpoint: ${SITE_URL}/_api/web/lists(guid'${listId}')/fields

    Comparing a working Hyperlink Column with our "Image Column"

    Firstly, comparing a regular hyperlink column to the image column (not working). I will refer to these as A and B respectively from now.

    The SchemaXML field for column A:

    "SchemaXml": "<Field DisplayName="Hyperlinks to stuff" Type="URL"
    Required="FALSE" ID="{d5a65922-6472-4e3f-8262-1ecae1c8f157}" SourceID="
    {ca3a557c-cbe8-44be-9f86-04c68884d222}"
    StaticName="Hyperlinks_x0020_to_x0020_stuff"
    Name="Hyperlinks_x0020_to_x0020_stuff" ColName="nvarchar7"
    RowOrdinal="0" ColName2="nvarchar8" RowOrdinal2="0"
    Format="Hyperlink" Hidden="FALSE" CanToggleHidden="TRUE" Version="1"
    />"
    

    The SchemaXML for column B (The Image Column not rendering properly):

    "SchemaXml": "<Field DisplayName="Files (Image Files Only)" Type="URL"
    Required="FALSE" ID="{c3645596-cd5a-455c-8489-7ad084fdbe4d}" SourceID="
    {ca3a557c-cbe8-44be-9f86-04c68884d222}"
    StaticName="Files_x0020__x0028_Image_x0020_F"
    Name="Files_x0020__x0028_Image_x0020_F" ColName="nvarchar9"
    RowOrdinal="0" ColName2="nvarchar10" RowOrdinal2="0" Version="2"
    Format="Image" Hidden="FALSE" CanToggleHidden="TRUE"
    />"
    

    Key differences:

    A: SchemaXML.Format appears as Hyperlink

    B: SchemaXML.Format appears as Image

    Other relevant differences:

    // Column A data:
    "FieldTypeKind": 11,
    "TypeAsString": "URL",
    "TypeDisplayName": "Hyperlink or Picture",
    "TypeShortDescription": "Hyperlink or Picture",
    "ValidationFormula": null,
    "ValidationMessage": null,
    "DisplayFormat": 0
    
    // Column B Data: (Same as above with the exception):
    "DisplayFormat": 1
    

    Comparing Column B to Column C (Manually Added Image Column). I will provide the entire payload for each column...

    Column B

    {
        "__metadata": {
          "id": "https://greymatterconcepts.sharepoint.com/sites/DataStrategy/_api/Web/Lists(guid'ca3a557c-cbe8-44be-9f86-04c68884d222')/Fields(guid'c3645596-cd5a-455c-8489-7ad084fdbe4d')",
          "uri": "https://greymatterconcepts.sharepoint.com/sites/DataStrategy/_api/Web/Lists(guid'ca3a557c-cbe8-44be-9f86-04c68884d222')/Fields(guid'c3645596-cd5a-455c-8489-7ad084fdbe4d')",
          "type": "SP.FieldUrl"
        },
        "DescriptionResource": {
          "__deferred": {
            "uri": "https://greymatterconcepts.sharepoint.com/sites/DataStrategy/_api/Web/Lists(guid'ca3a557c-cbe8-44be-9f86-04c68884d222')/Fields(guid'c3645596-cd5a-455c-8489-7ad084fdbe4d')/DescriptionResource"
          }
        },
        "TitleResource": {
          "__deferred": {
            "uri": "https://greymatterconcepts.sharepoint.com/sites/DataStrategy/_api/Web/Lists(guid'ca3a557c-cbe8-44be-9f86-04c68884d222')/Fields(guid'c3645596-cd5a-455c-8489-7ad084fdbe4d')/TitleResource"
          }
        },
        "AutofillInfo": null,
        "AutoIndexed": false,
        "CanBeDeleted": true,
        "ClientSideComponentId": "00000000-0000-0000-0000-000000000000",
        "ClientSideComponentProperties": null,
        "ClientValidationFormula": null,
        "ClientValidationMessage": null,
        "CustomFormatter": null,
        "DefaultFormula": null,
        "DefaultValue": null,
        "Description": "",
        "Direction": "none",
        "EnforceUniqueValues": false,
        "EntityPropertyName": "Files_x0020__x0028_Image_x0020_F",
        "Filterable": false,
        "FromBaseType": false,
        "Group": "Custom Columns",
        "Hidden": false,
        "Id": "c3645596-cd5a-455c-8489-7ad084fdbe4d",
        "Indexed": false,
        "IndexStatus": 0,
        "InternalName": "Files_x0020__x0028_Image_x0020_F",
        "IsModern": false,
        "JSLink": "clienttemplates.js",
        "PinnedToFiltersPane": false,
        "ReadOnlyField": false,
        "Required": false,
        "SchemaXml": "<Field DisplayName="Files (Image Files Only)" Type="URL" Required="FALSE" ID="{c3645596-cd5a-455c-8489-7ad084fdbe4d}" SourceID="{ca3a557c-cbe8-44be-9f86-04c68884d222}" StaticName="Files_x0020__x0028_Image_x0020_F" Name="Files_x0020__x0028_Image_x0020_F" ColName="nvarchar9" RowOrdinal="0" ColName2="nvarchar10" RowOrdinal2="0" Version="2" Format="Image" Hidden="FALSE" CanToggleHidden="TRUE" />",
        "Scope": "/sites/DataStrategy/Lists/Test Table",
        "Sealed": false,
        "ShowInFiltersPane": 0,
        "Sortable": true,
        "StaticName": "Files_x0020__x0028_Image_x0020_F",
        "Title": "Files (Image Files Only)",
        "FieldTypeKind": 11,
        "TypeAsString": "URL",
        "TypeDisplayName": "Hyperlink or Picture",
        "TypeShortDescription": "Hyperlink or Picture",
        "ValidationFormula": null,
        "ValidationMessage": null,
        "DisplayFormat": 1
      }
    

    Column C

    {
        "__metadata": {
          "id": "https://greymatterconcepts.sharepoint.com/sites/DataStrategy/_api/Web/Lists(guid'ca3a557c-cbe8-44be-9f86-04c68884d222')/Fields(guid'983cfe9e-e2a8-453f-8d82-5600d8641415')",
          "uri": "https://greymatterconcepts.sharepoint.com/sites/DataStrategy/_api/Web/Lists(guid'ca3a557c-cbe8-44be-9f86-04c68884d222')/Fields(guid'983cfe9e-e2a8-453f-8d82-5600d8641415')",
          "type": "SP.FieldMultiLineText"
        },
        "DescriptionResource": {
          "__deferred": {
            "uri": "https://greymatterconcepts.sharepoint.com/sites/DataStrategy/_api/Web/Lists(guid'ca3a557c-cbe8-44be-9f86-04c68884d222')/Fields(guid'983cfe9e-e2a8-453f-8d82-5600d8641415')/DescriptionResource"
          }
        },
        "TitleResource": {
          "__deferred": {
            "uri": "https://greymatterconcepts.sharepoint.com/sites/DataStrategy/_api/Web/Lists(guid'ca3a557c-cbe8-44be-9f86-04c68884d222')/Fields(guid'983cfe9e-e2a8-453f-8d82-5600d8641415')/TitleResource"
          }
        },
        "AutofillInfo": null,
        "AutoIndexed": false,
        "CanBeDeleted": true,
        "ClientSideComponentId": "00000000-0000-0000-0000-000000000000",
        "ClientSideComponentProperties": null,
        "ClientValidationFormula": null,
        "ClientValidationMessage": null,
        "CustomFormatter": null,
        "DefaultFormula": null,
        "DefaultValue": null,
        "Description": "",
        "Direction": "none",
        "EnforceUniqueValues": false,
        "EntityPropertyName": "ManualImageColumn",
        "Filterable": false,
        "FromBaseType": false,
        "Group": "Custom Columns",
        "Hidden": false,
        "Id": "983cfe9e-e2a8-453f-8d82-5600d8641415",
        "Indexed": false,
        "IndexStatus": 0,
        "InternalName": "ManualImageColumn",
        "IsModern": false,
        "JSLink": "clienttemplates.js",
        "PinnedToFiltersPane": false,
        "ReadOnlyField": false,
        "Required": false,
        "SchemaXml": "<Field ClientSideComponentId="00000000-0000-0000-0000-000000000000" DisplayName="Another Image Column" Format="Thumbnail" Name="ManualImageColumn" Title="Another Image Column" Type="Thumbnail" ID="{983cfe9e-e2a8-453f-8d82-5600d8641415}" Version="2" StaticName="ManualImageColumn" SourceID="{ca3a557c-cbe8-44be-9f86-04c68884d222}" ColName="ntext2" RowOrdinal="0" />",
        "Scope": "/sites/DataStrategy/Lists/Test Table",
        "Sealed": false,
        "ShowInFiltersPane": 0,
        "Sortable": false,
        "StaticName": "ManualImageColumn",
        "Title": "Another Image Column",
        "FieldTypeKind": 34,
        "TypeAsString": "Thumbnail",
        "TypeDisplayName": "Thumbnail",
        "TypeShortDescription": "Image",
        "ValidationFormula": null,
        "ValidationMessage": null,
        "AllowHyperlink": false,
        "AppendOnly": false,
        "IsLongHyperlink": false,
        "NumberOfLines": 6,
        "RestrictedMode": true,
        "RichText": false,
        "UnlimitedLengthInDocumentLibrary": false,
        "WikiLinking": false
      }
    

    Note: The EntityPropertyName is the original name of the column when it was created, thus you will see 'ManualImageColumn' in a few places. I've changed the name to 'Another Image Column' to match my initial post.

    Key Differences:

    1. Under __metadata at the very top we see that for column B the type is: "SP.FieldUrl" whereas for column C the type is: "SP.MultiLineText". This obviously does not make any sense to me.
    2. Comparing the SchemaXML shows very clearly that these two are completely different in how they are being rendered. Column C has a SchemaXML.Format value of "Thumbnail" and it's .Type property is also "Thumbnail".

    I really wish the Microsoft team cleans up the documentation for the API because it is very difficult to navigate all of these different field types and differences with Graph API and the Sharepoint REST API.

    I guess my next step would be to figure out if its possible to create a column with the field type as Thumbnail. Otherwise I am not sure how to proceed with any fix.


  2. The following payload works!

    const sharePointPayload = {
        "__metadata": { "type": "SP.Field" },
        "Title": columnName,
        "FieldTypeKind": 4,       // FieldTypeKind 4 corresponds to the Thumbnail column type
        "TypeAsString": "Thumbnail",
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search