Class OrganizationServiceExtensions
Extension methods for IOrganizationService
public static class OrganizationServiceExtensions
- Inheritance
-
OrganizationServiceExtensions
- Inherited Members
Methods
AssociateManySafe(IOrganizationService, string, EntityReference, string, IEnumerable<Guid>, bool)
Associates the given records unless they're already associated.
[Obsolete("Use native Associate method for a one-to-many relationship, or AssociateManyToManySafe for a many-to-many relationship")]
public static void AssociateManySafe(this IOrganizationService service, string relationshipName, EntityReference record1Ref, string associateEntityName, IEnumerable<Guid> idsToAssociate, bool skipExistingCheck)
Parameters
service
IOrganizationServicerelationshipName
stringrecord1Ref
EntityReferenceassociateEntityName
stringidsToAssociate
IEnumerable<Guid>skipExistingCheck
bool
AssociateManyToManySafe(IOrganizationService, EntityReference, Relationship, EntityReferenceCollection, string?, string?, string?)
Associates the records via the specified many-to-many relationship. The native Associate method will throw an exception if the provided records are already associated. This method avoids that by querying to determine which records are already associated, if any, and skips those.
If you know that the records are already not associated, it would be preferable to use the native Associate method which would avoid the unnecessary query.
public static void AssociateManyToManySafe(this IOrganizationService service, EntityReference record, Relationship relationship, EntityReferenceCollection relatedRecords, string? intersectEntityName = null, string? intersectEntityAttributeToRecord = null, string? intersectEntityAttributeToRelated = null)
Parameters
service
IOrganizationServiceThe org service.
record
EntityReferenceThe record to which
relatedRecords
will be associated.relationship
RelationshipThe many-to-many relationship. If the relationship is reflexive (same entity on both sides), then the PrimaryEntityRole property must be specified.
relatedRecords
EntityReferenceCollectionThe records to associate.
intersectEntityName
stringOptional, defaults to the logical name of the relationship. If the name of the intersect entity for the many-to-many relationship differs from the relationship's logical name, specify it with this parameter.
intersectEntityAttributeToRecord
stringOptional, defaults to
record
's logical name + "id" (or + "idone" or + "idtwo" if the relationship is relfexive). If the intersect entity's attribute torecord
differs from this pattern, specify it with this parameter.intersectEntityAttributeToRelated
stringOptional, defaults to
relatedRecords
's logical name + "id" (or + "idone" or + "idtwo" if the relationship is relfexive). If the intersect entity's attribute torelatedRecords
differs from this pattern, specify it with this parameter.
Exceptions
- ArgumentException
This exception is thrown if the relationship is reflexive (same entity on both sides) and
relationship
does not specify PrimaryEntityRole.
AssociateManyToManySafe(IOrganizationService, EntityReference, string, EntityReference, string?, string?, string?)
Associates the records via the specified many-to-many relationship. The native Associate method will throw an exception if the provided records are already associated. This method avoids that by querying to determine if the records are already associated.
If you know that the records are already not associated, it would be preferable to use the native Associate method which would avoid the unnecessary query.
public static void AssociateManyToManySafe(this IOrganizationService service, EntityReference record, string relationship, EntityReference relatedRecord, string? intersectEntityName = null, string? intersectEntityAttributeToRecord = null, string? intersectEntityAttributeToRelated = null)
Parameters
service
IOrganizationServiceThe org service.
record
EntityReferenceThe record to which
relatedRecord
will be associated.relationship
stringThe name of the many-to-many relationship. If the relationship is reflexive (same entity on both sides), then you must specify the PrimaryEntityRole property which means that you must use the overload of this method.
relatedRecord
EntityReferenceThe record to associate.
intersectEntityName
stringOptional, defaults to the logical name of the relationship. If the name of the intersect entity for the many-to-many relationship differs from the relationship's logical name, specify it with this parameter.
intersectEntityAttributeToRecord
stringOptional, defaults to
record
's logical name + "id" (or + "idone" or + "idtwo" if the relationship is relfexive). If the intersect entity's attribute torecord
differs from this pattern, specify it with this parameter.intersectEntityAttributeToRelated
stringOptional, defaults to
relatedRecord
's logical name + "id" (or + "idone" or + "idtwo" if the relationship is relfexive). If the intersect entity's attribute torelatedRecord
differs from this pattern, specify it with this parameter.
Exceptions
- ArgumentException
This exception is thrown if the relationship is reflexive (same entity on both sides) and
relationship
does not specify PrimaryEntityRole.
AssociateSafe(IOrganizationService, string, EntityReference, EntityReference, bool)
Associates the given records unless they're already associated.
[Obsolete("Use native Associate method for a one-to-many relationship, or AssociateManyToManySafe for a many-to-many relationship")]
public static void AssociateSafe(this IOrganizationService service, string relationshipName, EntityReference record1Ref, EntityReference record2Ref, bool skipExistingCheck)
Parameters
service
IOrganizationServicerelationshipName
stringrecord1Ref
EntityReferencerecord2Ref
EntityReferenceskipExistingCheck
bool
BulkRetrieveByIds(IOrganizationService, string, IEnumerable<Guid>, ColumnSet, BulkRetrieveByIdsOptions?)
Retrieves a set of records by primary id. This batches the ids into several requests if needed.
public static IEnumerable<Entity> BulkRetrieveByIds(this IOrganizationService svc, string entityName, IEnumerable<Guid> ids, ColumnSet columns, BulkRetrieveByIdsOptions? options = null)
Parameters
svc
IOrganizationServiceentityName
stringids
IEnumerable<Guid>columns
ColumnSetoptions
BulkRetrieveByIdsOptions
Returns
CreateMultiple(IOrganizationService, IEnumerable<Entity>)
Creates multiple records for a single dataverse table.
public static Guid[] CreateMultiple(this IOrganizationService svc, IEnumerable<Entity> entities)
Parameters
svc
IOrganizationServiceDataverse client
entities
IEnumerable<Entity>List of records to create
Returns
- Guid[]
Ids of the created records
DeleteFile(IOrganizationService, EntityReference, string)
Deletes the file from the given record / column
public static void DeleteFile(this IOrganizationService service, EntityReference targetEntityReference, string targetColumnName)
Parameters
service
IOrganizationServiceThe instance of the organization service
targetEntityReference
EntityReferenceAn Entity Reference to the desired record
targetColumnName
stringThe column name from which the file should be downloaded
DeleteFile(IOrganizationService, Guid)
Deletes the file identified via the GUID
public static void DeleteFile(this IOrganizationService service, Guid fileId)
Parameters
service
IOrganizationServiceThe instance of the organization service
fileId
GuidThe GUID of the file to be deleted
DisassociateSafe(IOrganizationService, string, EntityReference, EntityReference, bool)
Disassociates the given records unless they're already not associated.
[Obsolete("Use standard Disassociate method")]
public static void DisassociateSafe(this IOrganizationService service, string relationshipName, EntityReference record1Ref, EntityReference record2Ref, bool skipExistingCheck)
Parameters
service
IOrganizationServicerelationshipName
stringrecord1Ref
EntityReferencerecord2Ref
EntityReferenceskipExistingCheck
bool
DownloadFile(IOrganizationService, EntityReference, string, int)
Downloads a file from the given record / column
public static byte[] DownloadFile(this IOrganizationService service, EntityReference targetEntityReference, string targetColumnName, int maxDownloadBlockSizeInBytes = 4194304)
Parameters
service
IOrganizationServiceThe instance of the organization service
targetEntityReference
EntityReferenceAn Entity Reference to the desired record
targetColumnName
stringThe column name from which the file should be downloaded
maxDownloadBlockSizeInBytes
intAn optional parameter that can be used to control how large the individual blocks of data are when downloading large files. At the time of writing, this value cannot be larger than 4 MB (i.e. 4,194,304 Bytes)
Returns
- byte[]
A byte[] representation of the file
ExecuteMultiple(IOrganizationService, bool, bool, params OrganizationRequest[])
Executes the specified set of requests.
public static ExecuteMultipleResponse ExecuteMultiple(this IOrganizationService service, bool continueOnError, bool returnResponses, params OrganizationRequest[] requests)
Parameters
service
IOrganizationServicecontinueOnError
boolreturnResponses
boolrequests
OrganizationRequest[]
Returns
Fetch(IOrganizationService, string)
Fetches records based on the given fetchxml.
public static EntityCollection Fetch(this IOrganizationService service, string fetchXml)
Parameters
service
IOrganizationServicefetchXml
string
Returns
Fetch<T>(IOrganizationService, string)
Fetches records based on the given fetchxml.
public static List<T> Fetch<T>(this IOrganizationService service, string fetchXml) where T : Entity
Parameters
service
IOrganizationServicefetchXml
string
Returns
- List<T>
Type Parameters
T
Retrieve(IOrganizationService, EntityReference, params string[])
Retrieves the given record by alternate key or primary id.
public static Entity Retrieve(this IOrganizationService service, EntityReference entity, params string[] columns)
Parameters
service
IOrganizationServiceentity
EntityReferenceEither a primary id or alternate key reference to the record to retrieve
columns
string[]
Returns
RetrieveAllLazy(IOrganizationService, QueryExpression, RetrieveAllOptions?)
Pages through records until options.MaxResults is reached.
public static IEnumerable<Entity> RetrieveAllLazy(this IOrganizationService svc, QueryExpression query, RetrieveAllOptions? options = null)
Parameters
svc
IOrganizationServicequery
QueryExpressionoptions
RetrieveAllOptions
Returns
RetrieveAllLazy(IOrganizationService, string, RetrieveAllOptions?)
Retrieves all records for the given query instead of just the first 5000. NOTE: This method is lazy. Call ToList to iterate through the results if needed.
public static IEnumerable<Entity> RetrieveAllLazy(this IOrganizationService svc, string fetchXml, RetrieveAllOptions? options = null)
Parameters
svc
IOrganizationServicefetchXml
stringoptions
RetrieveAllOptions
Returns
RetrieveAllLazy<T>(IOrganizationService, QueryExpression, RetrieveAllOptions?)
Retrieves all records for the given query instead of just the first 5000. NOTE: This method is lazy. Call ToList to iterate through the results if needed.
public static IEnumerable<T> RetrieveAllLazy<T>(this IOrganizationService svc, QueryExpression query, RetrieveAllOptions? options = null) where T : Entity
Parameters
svc
IOrganizationServicequery
QueryExpressionoptions
RetrieveAllOptions
Returns
- IEnumerable<T>
Type Parameters
T
RetrieveMultiple(IOrganizationService, string, ColumnSet, params ConditionExpression[])
Retrieves a list of multiple DynamicEntities using the specified query from the ICrmService.
public static EntityCollection RetrieveMultiple(this IOrganizationService service, string entityName, ColumnSet columns, params ConditionExpression[] conditions)
Parameters
service
IOrganizationServiceentityName
stringcolumns
ColumnSetconditions
ConditionExpression[]
Returns
- EntityCollection
A list of DynamicEntities.
RetrieveMultiple(IOrganizationService, string, ColumnSet, string, ConditionOperator, object)
Retrieves a list of multiple DynamicEntities given the conditions.
public static EntityCollection RetrieveMultiple(this IOrganizationService service, string entityName, ColumnSet columns, string conditionAttribute, ConditionOperator conditionOp, object conditionValue)
Parameters
service
IOrganizationServiceentityName
stringcolumns
ColumnSetconditionAttribute
stringconditionOp
ConditionOperatorconditionValue
object
Returns
- EntityCollection
A list of DynamicEntities.
Retrieve<T>(IOrganizationService, Guid, params string[])
Retrieves the record with the given id.
public static T Retrieve<T>(this IOrganizationService service, Guid id, params string[] columns) where T : Entity
Parameters
service
IOrganizationServiceThe instance of the organization service
id
GuidPrimary id of the record to retrieve
columns
string[]The set of attributes to retrieve
Returns
- T
Retrieved record
Type Parameters
T
Early bound type to use.
UpdateMultiple(IOrganizationService, IEnumerable<Entity>)
Update multiple records for a single dataverse table.
public static void UpdateMultiple(this IOrganizationService svc, IEnumerable<Entity> entities)
Parameters
svc
IOrganizationServiceDataverse client
entities
IEnumerable<Entity>List of records to create
UploadFile(IOrganizationService, EntityReference, string, byte[], string, string, int)
Uploads a file to the given record / column
public static CommitFileBlocksUploadResponse UploadFile(this IOrganizationService service, EntityReference targetEntityReference, string targetColumnName, byte[] fileAsByteArray, string fileName, string mimeType, int maxUploadBlockSizeInBytes = 4194304)
Parameters
service
IOrganizationServiceThe instance of the organization service
targetEntityReference
EntityReferenceAn Entity Reference to the desired record
targetColumnName
stringThe column name to which the file should be uploaded
fileAsByteArray
byte[]The file to be uploaded as a byte[]
fileName
stringThe name to be associated with the uploaded file
mimeType
stringThe mime type that should be associated to the file (e.g. application/xml or application/pdf)
maxUploadBlockSizeInBytes
intAn optional parameter that can be used to control how large the individual blocks of data are when being uploading large files. At the time of writing, this value cannot be larger than 4 MB (i.e. 4,194,304 Bytes)
Returns
- CommitFileBlocksUploadResponse
The CommitFileBlocksUploadResponse returned by the upload process
UpsertMultiple(IOrganizationService, IEnumerable<Entity>)
Upserts multiple records for a single dataverse table.
public static UpsertResponse[] UpsertMultiple(this IOrganizationService svc, IEnumerable<Entity> entities)
Parameters
svc
IOrganizationServiceDataverse client
entities
IEnumerable<Entity>List of records to create