Skip to content

Salesforce REST API client for .NET, Mono and Xamarin iOS/Android

License

Notifications You must be signed in to change notification settings

johndpope/sharpforce

 
 

Repository files navigation

Sharpforce - Salesforce REST API client for .NET and Xamarin iOS/Android

It implements a simple client for the Salesforce REST API using RestSharp and targeting multiple platforms.

Features

  • Supports .NET, Xamarin iOS, Xamarin Android
  • Supported Salesforce APIs: Versions, Query, CRUD
  • Auto-generated C# classes (POCOs) for Salesforce objects
// Instantiate the client using a RefreshToken
var service = new SalesforceClient("ConsumerKey", "ConsumerSecret", "RefreshToken");

//-----------------------------------------------------------------------------
// Queries
//-----------------------------------------------------------------------------

// Execute a SOQL query
IList<Contact> contacts = service.Query<Contact>("SELECT id, name from Contact");

// Iterate through the records returned.
foreach (Contact account in contacts)
{
    Console.WriteLine(account.Name);
}

//-----------------------------------------------------------------------------
// CRUD Operations
//-----------------------------------------------------------------------------

// Add a new record using annonymous object
var id = service.Add<Contact>(new { FirstName = "John", LastName = "Smith" });

// Add a new record using POCO object
id = service.Add<Contact>(new Contact { FirstName = "John", LastName = "Smith" });

// Read a record
Contact contact = service.Get<Contact>(id);

// Update a record using POCO object (null values are not serialized)
contact.Email = "jsmith@gmail.com";
service.Update<Contact>(contact);

// Update a record using annonymous object
service.Update<Contact>(new { Email = "jsmith@yahoo.com" }, id);

// Delete a record
service.Delete<Contact>(id);

//-----------------------------------------------------------------------------
// Error Handling
//-----------------------------------------------------------------------------

try
{
    service.Add<Contact>(new { Name = "Read-only property" });
}
catch (SalesforceException e)
{
    Console.WriteLine("ErrorCode={0}; StatusCode={1}; Message={2}", 
                        e.ErrorCode, e.StatusCode, e.Message);
    // Output:
    // ErrorCode=INVALID_FIELD_FOR_INSERT_UPDATE; 
    // StatusCode=BadRequest; 
    // Message=Unable to create/update fields: Name. Please check the security settings of this field
    //         and verify that it is read/write for your profile or permission set.
	            
    // TODO: handle the exception
}

About

Salesforce REST API client for .NET, Mono and Xamarin iOS/Android

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published