Background
I am working on a script in JavaScript that generates new columns for a given Sharepoint List. I have been referencing the Graph API which defines the column types and what should be present in the payload for the POST requests to create new columns. To format the request I have referred to the following: https://learn.microsoft.com/en-us/graph/api/list-post-columns?view=graph-rest-1.0&tabs=http.
The following code from my script that is working for almost all types: (text, dateTime, boolean, choice, currency, etc…)
const graphApiUrl = `https://graph.microsoft.com/v1.0/sites/${siteId}/lists/${listId}/columns`;
const response = await fetch(graphApiUrl, {
method: "POST",
headers: {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(graphPayload),
});
if (!response.ok) {
const errorDetails = await response.text();
logError(`Failed to create column "${field.name}": ${response.statusText}nDetails: ${errorDetails}`);
return;
}
const responseData = await response.json();
Unfortunately, I am having an issue with the hyperlinkOrPicture columns… For instance the following combinations of payloads do not work:
// payload for hyperlink column
{
name: 'URL',
hyperlinkOrPicture: { isPicture: false }
}
// payload for picture column
{
name: 'Files (Image Files Only)',
hyperlinkOrPicture: { isPicture: true }
}
The following error occurs:
ERROR: Failed to create column "URL": Bad Request
Details: {"error":{"code":"invalidRequest","message":"Invalid request","innerError":{"date":"2025-01-03T21:32:54","request-id":"ad66da6b-c273-409a-801c-aabe918c80e2","client-request-id":"ad66da6b-c273-409a-801c-aabe918c80e2"}}}
# similar for the second column
The above formatting comes from the documentation:
Research and Attempt at Solution
Bad Graph API Documentation
Before resorting to making a post on here, I did my best to research as much as possible: both from other posts, and Microsoft’s API documentation. I knew there was a possibility that the API documentation was missing something because the Graph documentation in particular for Lists related requests is very minimal in detail, and tends to just include examples without a thorough breakdown of all the fields that are necessary in the body of the requests. I noticed this when working on the second part of my script which was to insert items into the columns I generated.
Example: Inserting a new Item to a “choice” column type with multipleSelects enabled.
If we look at the documentation it is very minimal and does not provide any example for all possible fields: https://learn.microsoft.com/en-us/graph/api/listitem-create?view=graph-rest-1.0&tabs=http
I had to resort to another post that mentioned how to have this done.
Thus, I was hoping that something like this was the case for the hyperlinkOrPicture columns – which in the Sharepoint API are referred to as different types: https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/ee540543(v=office.15). It states that there is a ‘URL’ Field, with a value of 11, and a ‘File’ field with a value of 18. Clearly, there is a disconnect here and cause of confusion.
Using Sharepoint API Instead
After not finding anything on the Graph API documentation as to why hyperlinkOrPicture column type happens to be the only column type that is not being created properly, even though I am following the formatting, I decided to try the Sharepoint API. However, I have been having a difficult time setting that up and having an issue with the token that I am using. Even though I have Sites.FullControl.All Application Access, and have followed the instructions in this post from MicroSoft to setup app-only token access: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread, by including a certificate to my registered app on azure, I am still getting an error that says the following:
ERROR: Failed to create SharePoint column "URL": Unauthorized
Unsupported app only token.
I am not sure why this is only an issue with SharePoint API when Graph API is working just fine with similar permissions.
I am using the following payload according to SharePoint API documentation to create the new column:
const isPicture = field.type === 'multipleAttachments';
const sharePointPayload = {
__metadata: { type: "SP.Field" },
Title: columnName,
FieldTypeKind: 11,
Required: false,
EnforceUniqueValues: false,
UrlFormat: isPicture ? 1 : 0, // 0 for Hyperlink, 1 for Picture
Description: columnPayload.description,
};
Request is below…
const sharePointApiUrl = `${SITE_URL}/_api/web/lists(guid'${listId}')/fields`;
try {
const response = await fetch(sharePointApiUrl, {
method: "POST",
headers: {
Authorization: `Bearer ${sharePointAccessToken}`,
Accept: "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
},
body: JSON.stringify(sharePointPayload),
});
if (!response.ok) {
const errorDetails = await response.text();
logError(
`Failed to create SharePoint column "${columnName}": ${response.statusText}n${errorDetails}`
);
}
log(`SharePoint column "${columnName}" created successfully.`);
} catch (error) {
logError(`Error creating SharePoint column "${columnName}": ${error.message}`);
}
I am not completely certain if the above is the correct payload for creating a hyperlinkOrPicture column type in SharePoint Lists. I would not be able to know since I am getting the previous error I mentioned with regard to the token.
If someone could come up with a solution by using just the Graph API, that would be the best; however, I would also like to know what I may be doing wrong in the setup process to have SharePoint API requests working.