How get contact in other application and edit this

Introduction

Hello, I am a french student in trainee at an association, and I must create and manage a Nextcloud server.

My clients want I create a Nextcloud application for get contacts store in Nextcloud, send them in a Laurux Database, and they want that we can add a contact in a “show” and filter the contacts by show.

For example if I am a show named : “FunShow”, we would like add a contact in this show.

For the moment the app look like this (Sorry it’s in french) :

The problem

I would like find how get a specific contact (with UID for example), edit this and save this in Nextcloud.
For the moment I use IManager for that but, it don’t save correctly in Nextcloud, here I want say, the profil picture of contact are delete and several information are bad formated.

An example of code

public function ajoutContactDansSpe($utilisateur, $spectacle)
	{
		$contacts = $this->contactsManager->search($filter ?: '', Contact::SEARCHPROPERTIES);

		$contact;

		foreach($contacts as $c)
		{
			if(strcmp($c['UID'], $utilisateur) == 0)
			{
				$contact = $c;
				break;
			}
		}
		
		if(isset($contact) != 0)
		{
			if(isset($contact['SPE']) != 0)
			{
				$contact['SPE'] = $contact['SPE'].$spectacle.";";
			}
			else
			{
				$contact['SPE'] = $spectacle;
			}
			$this->contactsManager->createOrUpdate($contact, $contact['addressbook-key']);
		}
	}

Do you have a solution ?

You can e.g. use Thunderbird with the Cardbook add-in installed to edit contacts stored in the Nextcloud database.

Yes, but I want edit contacts automatically with the app.

Sorry, but that’s exactly what the recommended app does. Please be more precise and describe IN DETAIL what you’re planning to do.

it’s up to date

Ok, now I understand. You want to access the Nextcloud contacts database with a self programmed application. Due to the fact that Nextcloud relies on Sabre/dav, you’re most likely already aware of the Nextcloud developer guide, aren’t you?

https://docs.nextcloud.com/server/19/developer_manual/app/requests/container.html?highlight=contacts

Additionally you will find further information about how to build your own CardDAV client here:

https://sabre.io/dav/building-a-carddav-client/

or here:

The Nextcloud developer guid purpose to use IManager but it not it not really useful here, because the only function for get contact is “search()” but this function it’s not reliable.

Your github link it’s maybe the solution but, I don’t understand the line :

 * ownCloud:					https://example.com/apps/contacts/carddav.php/addressbooks/{resource|principal|username}/{collection}/

What is the difference between resource, principal and username, and what is collection ?

@skjnldsv Would it be possible that you provide some more details about how to use the function?!

You can get the IAddressBook by dependency injections
You can find an example on some recent work on the contacts app here: https://github.com/nextcloud/contacts/pull/1580/files#diff-176d601ac39a7c37d86292e698badec2R159

	/**
	 * @NoAdminRequired
	 *
	 * Gets the addressbook of an addressbookId
	 *
	 * @param {String} addressbookId the identifier of the addressbook
	 *
	 * @returns {IAddressBook} the corresponding addressbook or null
	 */
	protected function getAddressBook(string $addressbookId) : ?IAddressBook {
		$addressBook = null;
		$addressBooks = $this->manager->getUserAddressBooks();
		foreach($addressBooks as $ab) {
			if ($ab->getUri() === $addressbookId) {
				$addressBook = $ab;
			}
		}
		return $addressBook;
	}

	try {
		// get corresponding addressbook
		$addressBook = $this->getAddressBook($addressbookId);
		if (is_null($addressBook)) {
			return new JSONResponse([], Http::STATUS_BAD_REQUEST);
		}

		// search contact in that addressbook, get social data
		$contact = $addressBook->search($contactId, ['UID'], ['types' => true])[0];
1 Like

Hello,
I don’t undestand how you have keep all data of photo of contact…
Because the problem, is the profile picture of contact are destroy after update :confused:

I have undestanded !
An example of the solution :

public function ajoutContactDansSpe($utilisateur, $spectacle)
	{
		$contact = $this->contactsManager->search($utilisateur, ['UID'], ['types' => true])[0];

		if(isset($contact) != 0)
		{
			$change = array();
			$change['URI'] = $contact['URI'];
			$change['UID'] = $contact['UID'];
			if(isset($contact['SPE']) != 0)
			{
				$change['SPE'] = $contact['SPE'].$spectacle.";";
			}
			else
			{
				$change['SPE'] = $spectacle;
			}

			$this->contactsManager->createOrUpdate($change, $contact['addressbook-key']);
		}
	}
1 Like