Skip to main content
Version: 8.1

WCF client request examples

Level: advanced

Use the DataServiceQuery universal class to retrieve the service's object collection. This class is a request to the service that retrieves the collection of a specific type of entities.

Create a context object instance of the Creatio application environment to execute a request to EntityDataService.svc.

The examples in this article will use the forms authentication.

To implement the forms authentication:

  1. Create a LoginClass class.

  2. Implement the authServiceUri (a string that requests the Login method of the AuthService.svc authentication service) and AuthCookie (Creatio's authentication cookies) fields.

  3. Implement the TryLogin(string userName, string userPassword) method that authenticates the user and saves the server's response to the AuthCookie field.

  4. Implement the OnSendingRequestCookie(object sender, SendingRequestEventArgs e) method that will be called in response to an event of the SendingRequest context instance (creating a new HttpWebRequest instance).

    The OnSendingRequestCookie method authenticates the user and adds the cookies received in response to the data request.

    OnSendingRequestCookie
    static void OnSendingRequestCookie(object sender, SendingRequestEventArgs e)
    {
    // Call the method of the LoginClass class that authenticates the user's method passed in the parameters.
    LoginClass.TryLogin("CreatioUserName", "CreatioUserPassword");
    var req = e.Request as HttpWebRequest;
    // Add the received authentication cookies to the data request.
    req.CookieContainer = LoginClass.AuthCookie;
    e.Request = req;
    }

There are several ways to execute the service request:

  • A LINQ request to the named DataServiceQuery object received from the service context.
  • Implicit enumeration of the DataServiceQuery object received from the service context.
  • Explicit call of the Execute method of the DataServiceQuery object. Call the BeginExecute method for asynchronous execution.

Retrieve a contact collection via a LINQ request.

Example

Define and implement the LINQ request that returns all contact entities of the EntityDataService.svc service.

Implement the example

LINQ request
public static void GetOdataCollectioByLinqWcfExample()
{
// Create the Creatio application context.
var context = new Creatio(serverUri);
// Define the method that adds authentication cookies upon creating a new request.
context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequestCookie);
try
{
// Build a LINQ request to retrieve the contact collection.
var allContacts = from contacts in context.ContactCollection
select contacts;
foreach (Contact contact in allContacts)
{
// Execute the contact actions.
}
}
catch (Exception ex)
{
// Process errors.
}
}

Retrieve a contact collection via an implicit request

Example

Use context to execute an implicit request that retrieves all contact entities of the EntityDataService.svc service.

Implement the example

GetOdataCollectionByImplicitRequestExample()
public static void GetOdataCollectionByImplicitRequestExample()
{
// Create the Creatio application context.
var context = new Creatio(serverUri);
// Define the method that adds authentication cookies upon creating a new request.
context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequestCookie);
try
{
// Define an implicit request that retrieves the contact collection from the service.
DataServiceQuery<Contact> allContacts = context.ContactCollection;
foreach (Contact contact in allContacts)
{
// Execute the actions with contacts.
}
}
catch (Exception ex)
{
// Process errors.
}
}

Retrieve a contact collection via an explicit request

Example

Use the DataServiceContext context to execute an explicit request that retrieves all contact entities of the EntityDataService.svc service.

Implement the example

GetOdataCollectionByExplicitRequestExample()
public static void GetOdataCollectionByExplicitRequestExample()
{
// Define the Uri of the service request that returns the contact collection.
Uri contactUri = new Uri(serverUri, "ContactCollection");
// Create the Creatio application context
var context = new Creatio(serverUri);
// Define the method that adds authentication cookies upon creating a new request.
context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequestCookie);
try
{
// Call the Execute<>() method to execute an explicit request to the service.
foreach (Contact contact in context.Execute<Contact>(contactUri))
{
// Execute the contact actions.
}
}
catch (Exception ex)
{
// Process errors.
}
}

CRUD operation examples

public static void GetOdataObjectByWcfExample()
{
// Create the Creatio application context.
var context = new Creatio(serverUri);
// Define the method that adds authentication cookies upon creating a new request.
context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequestCookie);
//
var contact = context.ContactCollection.Where(c => c.Name.Contains("User")).First();
// Execute the contact actions.
}

Resources

DataServiceQuery class description

DataServiceContext class description