Condividi tramite


IOrganizationService.Execute(OrganizationRequest) Method

Definition

Executes a message in the form of a request, and returns a response.

public:
 Microsoft::Xrm::Sdk::OrganizationResponse ^ Execute(Microsoft::Xrm::Sdk::OrganizationRequest ^ request);
[System.ServiceModel.FaultContract(typeof(Microsoft.Xrm.Sdk.OrganizationServiceFault))]
[System.ServiceModel.OperationContract]
public Microsoft.Xrm.Sdk.OrganizationResponse Execute (Microsoft.Xrm.Sdk.OrganizationRequest request);
[<System.ServiceModel.FaultContract(typeof(Microsoft.Xrm.Sdk.OrganizationServiceFault))>]
[<System.ServiceModel.OperationContract>]
abstract member Execute : Microsoft.Xrm.Sdk.OrganizationRequest -> Microsoft.Xrm.Sdk.OrganizationResponse
Public Function Execute (request As OrganizationRequest) As OrganizationResponse

Parameters

request
OrganizationRequest

A request instance that defines the action to be performed.

Returns

The response from the request. If you need to get data from the response class, you must cast the return value of this method to the specific type of the response that corresponds to the Request parameter.

Attributes

Examples

For the following samples to work correctly, you must be connected to the server to get an IOrganizationService interface instance.

The following example shows how to use the Execute(OrganizationRequest) method to use the WhoAmIRequest class and access the data returned with the WhoAmIResponse class. This request has no parameters.

/// <summary>
/// Demonstrates executing a WhoAmI request
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
static void WhoAmIExample(IOrganizationService service) {

      WhoAmIRequest request = new();
      var response = (WhoAmIResponse)service.Execute(request);

      Console.WriteLine($"UserId: {response.UserId}");
      Console.WriteLine($"OrganizationId: {response.OrganizationId}");
      Console.WriteLine($"BusinessUnitId: {response.BusinessUnitId}");
}

The following example shows how to use the Execute(OrganizationRequest) method to share a record to a user or team using the GrantAccessRequest class. While a GrantAccessResponse class exists, you don't need to use it because this operation doesn't return any data.

/// <summary>
/// Demonstrates executing a GrantAccess request
/// </summary>
/// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
/// <param name="principal">The user, team, or organization to share the record with.</param>
/// <param name="access">The access rights to grant</param>
/// <param name="record">The record to share</param>
static void GrantAccessExample(
   IOrganizationService service,
   EntityReference principal,
   AccessRights access,
   EntityReference record)
{
      PrincipalAccess principalAccess = new()
      {
         AccessMask = access,
         Principal = principal
      };

      GrantAccessRequest request = new()
      {
         PrincipalAccess = principalAccess,
         Target = record
      };

      service.Execute(request);
}

Learn more about using sharing and assigning records.

Remarks

The Execute method is the only method that the Organization service uses. The other IOrganizationService methods are helper methods that instantiate the appropriate AssociateRequest, CreateRequest, DisassociateRequest, RetrieveRequest, RetrieveMultipleRequest, or UpdateRequest class instances and send the request using Execute. Understanding the Execute method is central to understanding how Dataverse data operations work. Learn how to use messages with the SDK for .NET.

Privileges and Access Rights

The privileges and access rights necessary when using the Execute method depend entirely on the nature of the data operation being performed. If the operation creates, updates, or deletes data, the caller must have the appropriate Create, Read, Write, Delete, Append, AppendTo, Assign, or Share privileges on any tables and the access rights on the individual records that the operation is applied to. Learn more about how to verify access with code.

For more information, see the Request class for the action you want to execute. For example, see GrantAccessRequest, or documentation for the message you want to use if you are using the base OrganizationRequest and OrganizationResponse classes.

Notes for Callers

For more information about the exceptions that can be thrown when this method is called, see Handle exceptions in your code.

Applies to