phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

How to Set ContentType Property while uploading file?

Open subashdbc opened this issue 7 years ago • 1 comments

Hello Sir, I am able to upload the file to my site and set the meta data, but I also want to set ContentType property, how do I do that ? I am trying the same setProperty(name, value); statement to set the value, I am passing the ContentType object in the value parameter but it gives me this error "A node of type 'StartArray' was read from the JSON reader when trying to read the contents of a resource reference navigation link:..." , What am I missing here ? Thanks.

subashdbc avatar Nov 22 '16 08:11 subashdbc

@subashdbc To set the content type property you need to set the ID of the desired content type as the value. The property name to set would be "ContentTypeId". $listItem->setProperty("ContentTypeId",$contentTypeID , true);.

To get the IDs of your content types you can use the following code:

try {
    $contentTypes = $ctx->getWeb()->getLists()->getByTitle("<LIST TITLE>")->getContentTypes();
    $ctx->load($contentTypes);
    $ctx->executeQuery();
    foreach($contentTypes as $contentType) {
        $logger->debug($contentType->getName() . " - ID [" . $contentType->getId() .  "]");

    }
    
} catch (Exception $e) {
    $logger->error($e->getMessage());
}

mwe-code avatar Oct 06 '20 18:10 mwe-code