[SalesForce] Create a file using .NET toolkit

Is there a way to create a file using the .NET toolkit ?

I can't find it when configuring my data services in visual studio 2015.

I found Attachment and Document but I heard Attachment will be deprecated soon and Document can't be directly linked to an opportunity.

Below is the code I use today :

public bool AttachProjectDocuments(string siren, string description, IEnumerable<DocumentWithDataDto> projectDocuments)
    {
        var account = Query<Account>("SELECT ID FROM Account WHERE SIREN__c = " + siren);
        if (account != null && account.Records.Any())
        {
            var opportunity = Query<Opportunity>("SELECT ID, Name, CreatedDate, eligibility_rating__c, loan_duration_request__c, loan_reason__c, Loan_amount_request__c FROM Opportunity WHERE AccountId = '" + account.Records[0].Id + "'");
            if (opportunity != null && opportunity.Records.Any())
            {
                var lastOpportunity = opportunity.Records.OrderByDescending(x => x.CreatedDate).First();

                Update("Opportunity", lastOpportunity.Id,
                       new Opportunity
                       {
                           Id = lastOpportunity.Id,
                           AccountId = account.Records[0].Id,
                           Loan_amount_request__c = lastOpportunity.Loan_amount_request__c,
                           loan_duration_request__c = lastOpportunity.loan_duration_request__c,
                           loan_reason__c = lastOpportunity.loan_reason__c,
                           eligibility_rating__c = lastOpportunity.eligibility_rating__c,
                           LeadSource = lastOpportunity.LeadSource,
                           Loan_request_details__c = description,
                           CloseDate = DateTime.UtcNow,
                           Request_submission__c = DateTime.UtcNow,
                           StageName = "Needs Analysis",
                           Name = lastOpportunity.Name,
                           Year_N_accounts__c = true,
                           Year_N_1_accounts__c = true,
                           Year_N_2_accounts__c = true
                       });
                foreach (var doc in projectDocuments)
                {
                    var attachment = new Attachment
                                     {
                                         ParentId = lastOpportunity.Id,
                                         BodyLength = doc.Data.Length,
                                         Name = doc.Name,
                                         ContentType = doc.MimeType,
                                         Body = doc.Data
                                     };
                    Create("Attachment", attachment);
                }
                return true;
            }
        }
        return false;
    }

Best Answer

So the rumor you heard would probably be Lightning Experience: Attachments are being replaced by Files and not returned in search:

This is expected behavior and due to Attachments being deprecated in Lightning Experience. Attachments are being superseded and replaced in favor of the more robust Files feature. For more details on Files see Files Overview.

As best as I can tell, Files are exposed via Chatter REST API resources. , The example Id given in docs for a file is 069D00000001FHF. The 069 keyprefix is for ContentDocument, so that means you could probably use the Content Objects data model to find the correct objects to work with.

Give that a try. There are generic instructions for working with ContentDocument and ContentVersion in Insert or Update Blob Data.

See also: Differences Between Files, Salesforce CRM Content, Salesforce Knowledge, Documents, and Attachments

Related Topic