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 5 months ago • 0 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