WCF client request examples
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:
-
Create a
LoginClass
class. -
Implement the
authServiceUri
(a string that requests theLogin
method of theAuthService.svc
authentication service) andAuthCookie
(Creatio's authentication cookies) fields. -
Implement the
TryLogin(string userName, string userPassword)
method that authenticates the user and saves the server's response to theAuthCookie
field. -
Implement the
OnSendingRequestCookie(object sender, SendingRequestEventArgs e)
method that will be called in response to an event of theSendingRequest
context instance (creating a newHttpWebRequest
instance).The
OnSendingRequestCookie
method authenticates the user and adds the cookies received in response to the data request.OnSendingRequestCookiestatic 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.
Define and implement the LINQ request that returns all contact entities of the EntityDataService.svc
service.
Implement the example
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
Use context to execute an implicit request that retrieves all contact entities of the EntityDataService.svc
service.
Implement the example
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
Use the DataServiceContext context to execute an explicit request that retrieves all contact entities of the EntityDataService.svc
service.
Implement the example
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
- Retrieve an object
- Create an object
- Update an object
- Delete an object
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.
}
public static void CreateCreatioEntityByOdataWcfExample()
{
// Create a new contact and initialize its properties.
var contact = new Contact()
{
Id = Guid.NewGuid(),
Name = "New Test User"
};
// Create a new account to which the contact is connected and initialize the account's properties.
var account = new Account()
{
Id = Guid.NewGuid(),
Name = "Some Company"
};
contact.Account = account;
// 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>l(OnSendingRequestCookie);
// Add the contact to the service data model's contact collection.
context.AddToAccountCollection(account);
// Add the account to the service data model's account collection.
context.AddToContactCollection(contact);
// Connect the contact to the account in the service data model.
context.SetLink(contact, "Account", account);
// Save the changes to Creatio in a single request.
DataServiceResponse responces = context.SaveChanges(SaveChangesOptions.Batch);
// Process the server's responses.
}
public static void UpdateCreatioEntityByOdatetWcfExample()
{
// 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);
// Select a collection's contact to update.
var updateContact = context.ContactCollection.Where(c =< c.Name.Contains("Test")).First();
// Update the contact's properties.
updateContact.Notes = "New updated description for this contact.";
updateContact.Phone = "123456789";
// Save the changes to the service data model.
context.UpdateObject(updateContact);
// Save the changes to Creatio in a single request.
var responces = context.SaveChanges(SaveChangesOptions.Batch);
}
public static void DeleteCreatioEntityByOdataWcfExample()
{
// Create the Creatio application context.
var context = new Creatio(serverUri);
context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequestCookie);
// Select a contact collection's object to delete.
var deleteContact = context.ContactCollection.Where(c => c.Name.Contains("Test")).First();
// Delete the object from the service data model.
context.DeleteObject(deleteContact);
// Save the changes to Creatio in a single request.
var responces = context.SaveChanges(SaveChangesOptions.Batch);
// Process the server's responses.
}