Force.com Toolkit for .NET

repository·master·Indexed 18 days ago

https://github.com/wadewegner/force.com-toolkit-for-net

An SDK providing native .NET libraries to interact with Salesforce Lighting Platform and Chatter APIs. It includes the DeveloperForce.Force and DeveloperForce.Chatter NuGet packages, supporting standard REST CRUD operations via ForceClient and high-volume Bulk API operations via BulkForceClient. Features include Username-Password and Web-Server authentication flows.

Tokens
1.8K
Snippets
6
Records
6
Agent score
14%

What's inside Force.com Toolkit for .NET

  1. Authenticate using the Web-Server Authentication Flow

    master

    The Web-Server flow allows users to authenticate with their own credentials.

    1. Generate Authorization URL: Use Common.FormatAuthUrl to create a URL that directs the user to Salesforce. Replace login.salesforce.com with test.salesforce.com if using a sandbox.
    2. Handle Callback: After the user logs in, retrieve the code from the callback and use WebServerAsync to request the access token.
    // 1. Create the auth URL
    var url = Common.FormatAuthUrl(
        "https://login.salesforce.com/services/oauth2/authorize", 
        ResponseTypes.Code, 
        "YOURCONSUMERKEY", 
        HttpUtility.UrlEncode("YOURCALLBACKURL"));
    
    // 2. After user callback, exchange code for token
    await auth.WebServerAsync("YOURCONSUMERKEY", "YOURCONSUMERSECRET", "YOURCALLBACKURL", code);
  2. Install the Force.com and Chatter NuGet packages

    master

    You can install the libraries using the NuGet Package Manager or the .NET CLI. The two primary packages are DeveloperForce.Force for Lighting Platform APIs and DeveloperForce.Chatter for Chatter APIs.

    # Package Manager
    Install-Package DeveloperForce.Force
    Install-Package DeveloperForce.Chatter
    
    # .NET CLI
    dotnet add package DeveloperForce.Force
    dotnet add package DeveloperForce.Chatter
  3. Perform bulk operations with BulkForceClient

    master

    The BulkForceClient allows processing multiple records in batches using RunJobAndPollAsync. You can use SObjectList<T> for strongly-typed batches or SObjectList<SObject> for dynamic objects.

    Supported Operations:

    • BulkConstants.OperationType.Insert
    • BulkConstants.OperationType.Update
    • BulkConstants.OperationType.Delete
    • BulkConstants.OperationType.Upsert (requires specifying the External Id field name)

    Limitations:

    • CSV data type requests/responses are not supported.
    • Zipped attachment uploads are not supported.
    • Serial bulk jobs are not supported.
    • Query type bulk jobs are not supported.
    // Bulk Insert with strongly typed objects
    var accountsBatch1 = new SObjectList<Account> { new Account {Name = "Test1"}, new Account {Name = "Test2"} };
    var accountsBatchList = new List<SObjectList<Account>> { accountsBatch1 };
    
    var results = await bulkClient.RunJobAndPollAsync("Account", BulkConstants.OperationType.Insert, accountsBatchList);
    
    // Bulk Upsert using an External ID field
    var accountsBatchUpsert = new SObjectList<SObject> {
        new SObject { {"Name" = "TestDyAccount1"}, {"ExampleId" = "ID00001"} }
    };
    var resultsUpsert = await bulkClient.RunJobAndPollAsync("Account", "ExampleId", BulkConstants.OperationType.Upsert, accountsBatchUpsert);
  4. Initialize the ForceClient and BulkForceClient

    master

    Once authenticated, use the InstanceUrl, AccessToken, and ApiVersion from the AuthenticationClient to instantiate the clients used for API operations.

    var instanceUrl = auth.InstanceUrl;
    var accessToken = auth.AccessToken;
    var apiVersion = auth.ApiVersion;
    
    var client = new ForceClient(instanceUrl, accessToken, apiVersion);
    var bulkClient = new BulkForceClient(instanceUrl, accessToken, apiVersion);
  5. Authenticate using the Username-Password Flow

    master

    The AuthenticationClient provides a straightforward way to obtain an access token by providing your consumer credentials, username, and a password concatenated with your API Token. You can also specify a specific Salesforce API version during initialization; otherwise, it defaults to v36.0. You can use GetLatestVersionAsync() to retrieve the latest version from your instance.

    // Specify version during creation
    var auth = new AuthenticationClient("v44.0");
    
    // Or use default and fetch latest
    var auth = new AuthenticationClient();
    await auth.GetLatestVersionAsync();
    
    // Perform authentication
    await auth.UsernamePasswordAsync("YOURCONSUMERKEY", "YOURCONSUMERSECRET", "YOURUSERNAME", "YOURPASSWORDANDTOKEN");
  6. Perform CRUD operations with ForceClient

    master

    The ForceClient supports standard CRUD operations using either strongly-typed objects or anonymous/dynamic objects.

    // Create (Strongly Typed)
    var account = new Account() { Name = "New Account", Description = "New Account Description" };
    var id = await client.CreateAsync("Account", account);
    
    // Create (Anonymous/Dynamic)
    var accountDyn = new { Name = "New Name", Description = "New Description" };
    var idDyn = await client.CreateAsync("Account", accountDyn);
    
    // Update
    account.Name = "Updated Name";
    var success = await client.UpdateAsync("Account", id, account);
    
    // Delete
    var deleteSuccess = await client.DeleteAsync("Account", id);
    
    // Query (Strongly Typed)
    var accounts = await client.QueryAsync<Account>("SELECT id, name, description FROM Account");
    
    // Query Metadata
    var describe = await client.DescribeAsync<JObject>("Account");