{"record":{"id":"805807daf4d0d604","repo":"nextcloud/server","slug":"could-not-create-new-calendar-event-message","errorCode":null,"errorMessage":"Could not create new calendar event: {message}","messagePattern":"Could not create new calendar event: (.+?)","errorType":"exception","errorClass":"CalendarException","httpStatus":500,"severity":"error","filePath":"apps/dav/lib/CalDAV/CalendarImpl.php","lineNumber":223,"sourceCode":"\t\t\tthrow new CalendarException('Could not write to calendar as URI parameter is missing');\n\t\t}\n\n\t\t// Build full calendar path\n\t\t[, $user] = uriSplit($this->calendar->getPrincipalURI());\n\t\t$fullCalendarFilename = sprintf('calendars/%s/%s/%s', $user, $this->calendarInfo['uri'], $name);\n\n\t\t// Force calendar change URI\n\t\t/** @var Schedule\\Plugin $schedulingPlugin */\n\t\t$schedulingPlugin = $server->getPlugin('caldav-schedule');\n\t\t$schedulingPlugin->setPathOfCalendarObjectChange($fullCalendarFilename);\n\n\t\t$stream = fopen('php://memory', 'rb+');\n\t\tfwrite($stream, $calendarData);\n\t\trewind($stream);\n\t\ttry {\n\t\t\t$server->createFile($fullCalendarFilename, $stream);\n\t\t} catch (Conflict $e) {\n\t\t\tthrow new CalendarException('Could not create new calendar event: ' . $e->getMessage(), 0, $e);\n\t\t} finally {\n\t\t\tfclose($stream);\n\t\t}\n\t}\n\n\t#[\\Override]\n\tpublic function createFromString(string $name, string $calendarData): void {\n\t\t$server = new EmbeddedCalDavServer(false);\n\t\t$this->createFromStringInServer($name, $calendarData, $server->getServer());\n\t}\n\n\t#[\\Override]\n\tpublic function createFromStringMinimal(string $name, string $calendarData): void {\n\t\t$server = new InvitationResponseServer(false);\n\t\t$this->createFromStringInServer($name, $calendarData, $server->getServer());\n\t}\n\n\t/**","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/nextcloud/server/blob/ecdeb153ffdf227235c9a7e2d13dbe0f9c817bc3/apps/dav/lib/CalDAV/CalendarImpl.php#L205-L241","documentation":"Thrown as OCA\\DAV\\CalDAV\\CalendarException by CalendarImpl::createFromStringInServer() when the embedded sabre/dav server rejects $server->createFile() with a Conflict while writing a new event object. The original sabre Conflict message is appended, so the CalendarException text is a wrapper around the underlying DAV conflict reason. It surfaces through the public ICalendar API methods createFromString() and createFromStringMinimal().","triggerScenarios":"Calling $calendar->createFromString($name, $calendarData) (or createFromStringMinimal) on an OCA\\DAV\\CalDAV\\CalendarImpl when an object with the same filename already exists under calendars/<user>/<calendarUri>/<name>, or when the CalDAV scheduling/plugin layer reports a conflict during the PUT into the embedded server. Typical from calendar import scripts that reuse a fixed object name or re-run after a partial failure.","commonSituations":"Bulk iCalendar imports that derive the object name from a UID and hit the same UID twice; retrying an import after a timeout without skipping already-created events; concurrent requests creating the same event name; testing event creation in a loop with a hardcoded '.ics' filename.","solutions":["Generate a unique object name per event (e.g. the event UID plus a random suffix) before calling createFromString().","Before creating, check whether an object with that name already exists (calendarQuery/search on the ICalendar or the CalDAV backend) and update instead of create.","Catch CalendarException around createFromString(), inspect the chained Conflict via getPrevious(), and decide skip/update/rename on collision.","For imports, track which UIDs were already written so re-runs are idempotent."],"exampleFix":"// before\n$calendar->createFromString('event.ics', $calendarData);\n\n// after\n$name = $eventUid . '-' . bin2hex(random_bytes(4)) . '.ics';\ntry {\n\t$calendar->createFromString($name, $calendarData);\n} catch (CalendarException $e) {\n\t// object name collision or scheduling conflict; inspect $e->getPrevious()\n\t$logger->warning('Event create failed: ' . $e->getMessage());\n}","handlingStrategy":"try-catch","validationCode":"// derive a collision-free name before creating\n$objectName = $eventUid . '.ics';\n$existing = $calendar->search('', ['UID'], ['uid' => $eventUid]); // or calendarQuery via backend\nif ($existing !== []) {\n    $objectName = $eventUid . '-' . bin2hex(random_bytes(4)) . '.ics';\n}","typeGuard":null,"tryCatchPattern":"try {\n    $calendar->createFromString($name, $calendarData);\n} catch (\\OCA\\DAV\\CalDAV\\CalendarException $e) {\n    if ($e->getPrevious() instanceof \\Sabre\\DAV\\Exception\\Conflict) {\n        // name collision or scheduling conflict -> rename and retry once, or update existing\n    }\n    throw $e;\n}","preventionTips":["Always derive object filenames from a unique UID instead of constants like 'event.ics'.","Make import scripts idempotent by recording written UIDs.","Log the chained Conflict message (getPrevious()) to distinguish collision from other createFile conflicts."],"tags":["caldav","calendar","conflict","sabre-dav","nextcloud-dav","duplicate"],"backgroundTag":"duplicate-resource-conflict","analyzedSha":"ecdeb153ffdf227235c9a7e2d13dbe0f9c817bc3","analyzedAt":"2026-08-17T01:36:13.386Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}