.NET Server

ASP.NET CardDAV Server Example with Microsoft SQL Back-end, C#

In this article

ASP.NET CardDAV Server Example with Microsoft SQL Back-end, C#

The CardDAVServer.SqlStorage C# example is a fully-functional CardDAV server example provided with SDK that can be used to store address books and contacts in it, demonstrating how to build a CardDAV Server with SQL back-end. Here we will describe how to install and configure it. This example runs in ASP.NET on IIS or IIS Express and stores all address book data in Microsoft SQL Server database. It uses Basic authentication that validates credentials against a custom membership provider with credentials stored in web.config for the sake of simplicity.

Interoperability

This sample converts contacts created by iOS and OS X clients to a standard vCard format and extracts all data into database, including non-standard Apple properties and labels provided for E-mails, telephones, URLs, addresses, instant messengers, etc are stored and a regular vCard data. When generating vCard for Apple contacts application this sample converts a standard properties to the format understood by iOS and OS X devices, so they can propery display contact data. In case of any non-Apple client it generates a standard vCard output understood by any CardDAV client.

Custom vCard properties (X-PROP) are updated/deleted only if the client application updating the property is of the same type as the client app that created custom propery. This preserves custom properties from being overwritten by by CardDAV clients unaware how to interpret them. For example custom properties created by Apple client is sent back to Apple client only and could be updated or deleted from apple client only.

Prerequisites

To run this example you will need:

  • ASP.NET 4.5 or later on Windows Server 2012, Windows Server 2008, Windows 10, Windows 8, Windows 7 or Windows Vista.   
  • Microsoft SQL Server Express LocalDB or Microsoft SQL Express or Microsoft SQL Server 2014 / 2012 / 2008 / 2005.

Because this sample uses null-conditional operator you need Visual Studio 2015 to compile it. If you need to support Visual Studio 2013 or earlier use the 'ASP.NET CalDAV/CardDAV Server Application' wizard in Visual Studio 2013 or earlier to generate similar code that compiles in VS 2010-2013 and does not uses null-conditional operator.

Installation and Configuration

By default CardDAV Server example with SQL back-end is installed to C:\Users\<UserName>\Documents\IT Hit\WebDAV Server Engine\vX.X.X\Samples\CardDAVServer.SqlStorage\ folder.

Setting the License

To run the example, you will need a valid IT Hit WebDAV Server Engine License. You can download the license here. Note that the Engine is fully functional with a trial license and does not have any limitations. The trial license is valid for one month and the engine will stop working after this. You can check the expiration date inside the license file.

Place the license file in the root folder of the CardDAV Example (\Samples\CardDAVServer.SqlStorage\).

Configuring the Database

In case you have Microsoft SQL LocalDB installed and are running the sample from Visual Studio you do not need to configure the database. In this case your database file, WebDav.mdf, is located in \Samples\CardDAVServer.SqlStorage\App_Data\WebDav\DB\ and will be automatically attached when you run the sample from VS, see below:

CalDAV sample Microsoft SQL database default location.

If you have Microsoft SQL Express installed you need to update the connection string. The connection string for Microsoft SQL Express is provided in a web.config file.

If you do not have Microsoft SQL Express or LocalDB installed or running your sample in IIS you will need to create and configure the database. The database script, DB.sql is located in \Samples\CardDAVServer.SqlStorage\ folder. In most case, you will open the Microsoft SQL Server console and execute this script. The script will create the database with a name ‘WebDav’ and all required tables. Finally update the connection string in the web.config file to access your new database.

SQL Back-end

The database consists of entities depicted in the figure below.

CardDAV Server with SQL Back-end Diagram

card_Address Table

Stores addresses.

 Column NameData TypeAllow NullsDescription
  AddressId uniqueidentifier No  
  UID nvarchar No Card ID to which this address belongs.
  Type nvarchar Yes Coma separated list of type flags (WORK/HOME/DOM/INTL/POSTAL/PARCEL/etc.)
  PoBox nvarchar Yes PO box. For max interoperability, should be empty.
  AppartmentNumber nvarchar Yes Appartment number. For max interoperability, should be empty.
  Street nvarchar Yes Street name.
  Locality nvarchar Yes City name.
  Region nvarchar Yes Region (e.g., state or province).
  PostalCode nvarchar Yes Postal code.
  Country nvarchar Yes Country name.
  PreferenceLevel tinyint Yes Stores prefernce level for vCard 4.0. 1-100, 1-most prefered. In case of vCard 3.0 stores 1 for prefered property.
  Label nvarchar Yes Delivery address label.
  Geo geography Yes Coordinates associated with this address. vCard 4.0 only.
  TimeZone nvarchar Yes Time zone for this address. vCard 4.0 only.
  SortIndex int Yes Propery position in vCard. Used to keep properties in the order submitted by the CardDAV client app.

card_AddressbookFolder Table

Contain CardDAV addressbooks (addressbook folders)

 Column NameData TypeAllow NullsDescription
  AddressbookFolderId uniqueidentifier No  
  Name nvarchar No Address book name.
  Description nvarchar No Address book description.

card_AddressbookFolderProperty Table

Addressbook WebDAV custom properties.

 Column NameData TypeAllow NullsDescription
  AddressbookFolderPropertyId uniqueidentifier No  
  AddressbookFolderId uniqueidentifier No Addressbook to which this custom property belongs to.
  Name nvarchar No Custom property name.
  Namespace nvarchar No Custom property namespace.
  PropVal nvarchar No Custom property value.

card_CardFile Table

Stores business card (vCard files).

 Column NameData TypeAllow NullsDescription
  UID nvarchar No Business card UID
  AddressbookFolderId uniqueidentifier No Address book (address book folder) to which this business card belongs.
  FileName nvarchar No File name without extension. In case of CardDAV this is different from UID.
  ETag timestamp No Automaticaly changes each time this object is updated.
  CreatedUtc datetime2 No Time when this file was created. Typically CardDAV clients never request this property, used for demo purposes only.
  ModifiedUtc datetime2 No Time when this file was modified. This property is updated to trigger ETag update. Typically CardDAV clients never request this property, used for demo purposes only.
  Version nvarchar No vCard version - 2.1/3.0/4.0.
  FormattedName nvarchar No Formatted name.
  FamilyName nvarchar No Last name.
  GivenName nvarchar No First name.
  AdditionalNames nvarchar No Middle names, coma-separated list.
  HonorificPrefix nvarchar No Honorific prefix.
  HonorificSuffix nvarchar No Honorific suffix.
  Product nvarchar Yes Client app that created this card. For exmple -//Apple Inc.//iOS 10.0.2//EN
  Kind nvarchar Yes Kind of object the business card represents (individual/group/org/locaton). vCard 4.0 only.
  Nickname nvarchar Yes Business card nickname.
  Photo varbinary Yes Photoghraph image associated with this business card, typically when bysiness card represents an individual.
  PhotoMediaType nvarchar Yes Photo media type. PNG/JPEG/etc.
  Logo varbinary Yes Logo associated with this business card typically when bysiness card represents an organization.
  LogoMediaType nvarchar Yes Logo media type. PNG/JPEG/etc.
  Sound varbinary Yes Digital sound content associated with this business card.
  SoundMediaType nvarchar Yes Sound media type. PNG/JPEG/etc.
  Birthday datetime2 Yes Birth date of the object the business card represents.
  Anniversary datetime2 Yes Anniversary of the object the business card represents. vCard 4.0 only.
  Gender nvarchar Yes Gender of the identity this business card represents (M - male, F- female, O - other, N - none, U - unknown). vCard 4.0 only.
  RevisionUtc datetime2 Yes Revision date-time in UTC.
  SortString nvarchar Yes Text to be used as a sort string when displaying info on a client side. Typically family name or combination of family name and given name.
  Language nvarchar Yes Language used for contacting. vCard 4.0 only.
  TimeZone nvarchar Yes Represents information related to the time zone of the business object.
  Geo geography Yes Business object location - latitude and longitude.
  Title nvarchar Yes The position or job of the business card.
  Role nvarchar Yes The function or part played in a particular situation by the business object.
  OrgName nvarchar Yes Organization name.
  OrgUnit nvarchar Yes Organization unit.
  Categories nvarchar Yes Tags associated with business card.
  Note nvarchar Yes Business card supplemental information or a comment.
  Classification nvarchar Yes Business card access classification.

card_Email Table

Stores e-mails.

 Column NameData TypeAllow NullsDescription
  EmailId uniqueidentifier No  
  UID nvarchar No Card ID to which this telephone belongs.
  Type nvarchar Yes Coma separated list of type flags (WORK/HOME/INTERNET/X400/etc.).
  Email nvarchar No E-mail.
  PreferenceLevel tinyint Yes Stores prefernce level for vCard 4.0. 1-100, 1-most prefered. In case of vCard 2.1 and 3.0 stores 1 for prefered property.
  SortIndex int Yes Propery position in vCard. Used to keep properties in the order submitted by the CardDAV client app.

card_InstantMessenger Table

Stores stores instant messengers.

 Column NameData TypeAllow NullsDescription
  InstantMessengerId uniqueidentifier No  
  UID nvarchar No Card ID to which this instant messenger belongs.
  Type nvarchar Yes Coma separated list of type flags (WORK/HOME/PERSONAL/BUSINESS/MOBILE/etc.).
  InstantMessenger nvarchar No Instant messenger.
  PreferenceLevel tinyint Yes Stores prefernce level for vCard 4.0. 1-100, 1-most prefered. In case of vCard 2.1 and 3.0 stores 1 for prefered property.
  SortIndex int Yes Propery position in vCard. Used to keep properties in the order submitted by the CardDAV client app.

card_Telephone Table

Stores telephones

 Column NameData TypeAllow NullsDescription
  TelephoneId uniqueidentifier No  
  UID nvarchar No Card ID to which this telephone belongs.
  Type nvarchar Yes Coma separated list of type flags (WORK/HOME/TEXT/TEXTPHONE/PCS/VOICE/FAX/MSG/CELL/PAGER/BBS/MODEM/CAR/ISDN/VIDEO/etc.).
  Telephone nvarchar No Telephone.
  PreferenceLevel tinyint Yes Stores prefernce level for vCard 4.0. 1-100, 1-most prefered. In case of vCard 2.1 and 3.0 stores 1 for prefered property.
  SortIndex int Yes Propery position in vCard. Used to keep properties in the order submitted by the CardDAV client app.

card_Url Table

Stores URLs

 Column NameData TypeAllow NullsDescription
  UrlId uniqueidentifier No  
  UID nvarchar No Card ID to which this URL belongs.
  Type nvarchar Yes Coma separated list of type flags.
  Url nvarchar No Url.
  PreferenceLevel tinyint Yes Stores prefernce level for vCard 4.0. 1-100, 1-most prefered. In case of vCard 2.1 and 3.0 stores 1 for prefered property.
  SortIndex int Yes Propery position in vCard. Used to keep properties in the order submitted by the CardDAV client app.

card_CustomProperty Table

Stores vCard custom properties and parameters.

 Column NameData TypeAllow NullsDescription
  CustomPropertyId uniqueidentifier No  
  ParentId nvarchar No Parent Card ID or parent property ID to which this custom property or parameter belongs to. This could be UID, EmailId, InstantMessengerId, AddressId, TelephoneId, UrlId.
  UID nvarchar No Card ID to which this custom property or property parameter belongs to.
  ClientAppName nvarchar Yes Client application name that created this property. If business card is updated by any other client application except specified in this field, property will be preserved. Used to prevent custom props deletion by client apps that can not interpret this property.
  PropertyName nvarchar No Property name. This could be a custom property name (starting with 'X-') or standard property name in case standard property contains custom parameters.
  ParameterName nvarchar Yes Parameter name. If null - Value field contains property value. Otherwise Value field contains parameter value.
  Value nvarchar No Property or parameter value. If ParameterName is null - this is a property value. If ParameterName is not null - this is a parameter value.
  SortIndex int Yes Propery position in vCard. Used to keep properties in the order submitted by the CardDAV client app.

card_Access Table

Stores user address books access privileges.

 Column NameData TypeAllow NullsDescription
  AccessId uniqueidentifier No  
  AddressbookFolderId uniqueidentifier No Address book (address book folder) for which user privileges are applied.
  UserId nvarchar No User ID of the user that has access to an address book.
  Owner bit No Specifies if a user owns an address book.
  Read bit No User has a read privilege on an address book.
  Write bit No User has a write privilege on an address book.

 

See Also:

Next Article:

Calendar Server with File System Back-end Example, C#