msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

Sharepoint create listitem with hyperlink field

Open adriallongarriu opened this issue 2 years ago • 1 comments

I want to create an item to a Sharepoint List with one column being hyperlink. But theres is no examples and the errors returned by the api are not very clear.

I used a similar code to this https://learn.microsoft.com/en-us/graph/api/listitem-create?view=graph-rest-1.0&tabs=php

My code:

 <?php
 use Microsoft\Kiota\Abstractions\ApiException;
 use Microsoft\Graph\Generated\Models\ListItem;
 use Microsoft\Graph\Generated\Models\FieldValueSet;

    try {
        $requestBody = new ListItem();
        $fields = new FieldValueSet();

        $additionalData = [
            'URL' => [
                'Description' => 'Example Website',
                'Url' => 'https://www.example.com'
            ],
            'Title' => 'Test',
        ];
        $fields->setAdditionalData($additionalData);

        $requestBody->setFields($fields);
        $newItemRequest = $this->graphServiceClient
            ->sites()->bySiteId($this->siteId)
            ->lists()->byListId($this->listId)
            ->items()->post($requestBody)->wait();
    } catch (ApiException $ex) {
        dd($ex);
        abort(500, "The record was not created correctly");
    }
    dd($newItemRequest);

Bud the api return error 400: "Invalid request"

If i send the url like the title

 $additionalData = [
            'URL' =>  'https://www.example.com',
            'Title' => 'Test',
        ];

The api return error 500: "General exception while processing"

The problems is definitely related to the hyperlink since if I comment and just leave title inside $additionalData the item is created.

When you get one item from the api creade by the sharpoint website the object ListItem return the field URL like an array with two properties Description and Url. image

I reserch and only fount a post saying is not supported. But I can't find any official source of what type of columns does Microsoft Graph API support on Sharepoint List Items? Or how I can create or edit a item with a hyperlink?

adriallongarriu avatar Jan 09 '24 16:01 adriallongarriu

any news here?

ski7777 avatar Sep 30 '24 06:09 ski7777

Hello @adriallongarriu and @ski7777 Is this still an issue, which version of thhe SDK are you on.

Also, what entity are you creating as a ListItem, note that:

  1. A site is a shareoint site collection.
  2. A list is a collection of shareable data - which can be of any entity such as contact, task, event , a custom item etc, and the fileds would comply with the entity fields for the entity you are interested in.

Retrying and looking for a fix.

shemogumbe avatar Mar 12 '25 14:03 shemogumbe

Looking at this, I notice two issues:

  1. Error handling - your error handling automatically assumes every apierror is a 500 statuds code error,
  2. there error messages returned are not comprehensive anough to give a solution.

In the time being -

Making tweaks to:


try {
    $requestBody = new ListItem();
    $fields = new FieldValueSet();

    $additionalData = [
        'URL' => [
            'Description' => 'Example Website',
            'Url' => 'https://www.example.com'
        ],
        'Title' => 'Test',
    ];

    $fields->setAdditionalData($additionalData);
    $requestBody->setFields($fields);

    $result = $graphServiceClient->sites()->bySiteId($siteId)->lists()->byListId($listId)->items()->post($requestBody)->wait();
    echo "Response code: " . $result->getResponse()->getStatusCode() . "\n";    

} catch (ApiException $ex) {
    $error = new ODataError();
    $error->setError($ex->getError());
    echo "Error: " . $ex->getResponseStatusCode() . "\n";
    echo "Error: " . $error->getResponseMessage() . "\n";
    echo "Error: " . print_r($ex->getResponseHeaders(), true) . "\n";
}

In your code gives us:

Error: 400 Error: Invalid request Error: Array ( [cache-control] => Array ( [0] => no-store, no-cache ) [content-type] => Array ( [0] => application/json ) [strict-transport-security] => Array ( [0] => max-age=31536000 ) [request-id] => Array ( [0] => f59408f0-9c05-4b00-a619-1b7e07875eab ) [client-request-id] => Array ( [0] => 08d2d45d-48e5-41b1-a33c-d07cf5571244 ) [x-ms-ags-diagnostic] => Array ( [0] => {"ServerInfo":{"DataCenter":"North Europe","Slice":"E","Ring":"4","ScaleUnit":"002","RoleInstance":"DU2PEPF000105E9"}} ) [date] => Array ( [0] => Wed, 12 Mar 2025 17:56:06 GMT ) )

More errors that point to the problem is message='Files and folders should only be added to a DocumentLibrary via the OneDrive API',

Working on a new ticket to display the correct error for this, for your case, refer to resource types needed for list items here https://learn.microsoft.com/en-us/graph/api/resources/listitem?view=graph-rest-1.0 and if a file or folder, use drive api to upload instead of the sharepoint api

shemogumbe avatar Mar 12 '25 18:03 shemogumbe