- IOrganization 是用于向组织提供元数据以及数据的编程访问的接口. (Dataverse的增删改查以及表联系的相关操作)
//ServiceClient、CrmServiceClient 都可实现 IOrganizationService接口
IOrganizationService service = new CrmServiceClient(connectionString);
IOrganizationService service = new ServiceClient(connectionString);
using Microsoft.Crm.Sdk.Messages;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
class Program
{
// Dataverse环境URL以及登录信息
static string url = "https://yourorg.crm.dynamics.com";
static string userName = "you@yourorg.onmicrosoft.com";
static string password = "yourPassword";
// 连接上述代码串进行测试
static string connectionString = $@"
AuthType = OAuth;
Url = {url};
UserName = {userName};
Password = {password};
AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;
RedirectUri = http://localhost;
LoginPrompt=Auto;
RequireNewInstance = True";
static void Main()
{
// 获取IorganizationService的接口实例
IOrganizationService service = new ServiceClient(connectionString);
var response = (WhoAmIResponse)service.Execute(new WhoAmIRequest());
Console.WriteLine($"User ID is {response.UserId}.");
// Pause the console so it does not close.
Console.WriteLine("Press the <Enter> key to exit.");
Console.ReadLine();
}
}
? 参考文献. c#连接Microsoft Dataver:c#连接 MicroSoft Dataverse
?
? 参考文献. IOrganization:IOrganizationService 接口
?
在这里插入代码片
在这里插入代码片
在这里插入代码片
? 参考文献. Entity对象:Entity对象
? a. 代码及使用
//public Microsoft::Xrm::Sdk::Entity ^ Retrieve(System::String ^ entityName, Guid id, Microsoft::Xrm::Sdk::Query::ColumnSet ^ columnSet);
static void Retrive(IOrganizationService service){
string entityName = "crda9_room"; // 逻辑名称
ColumnSet columnSet = new ColumnSet("crda9_name", "crda9_type", "crda9_site"); // 查询的属性
Guid guid = new Guid("4e4c81a1-439d-ee11-be37-000d3a85d073"); // 全局唯一标识符
Entity entity = service.Retrieve(entityName, guid, columnSet); // 发起查询
Console.WriteLine($"name:{entity.GetAttributeValue<string>("crda9_name")}"
+ " " + $"type:{entity.GetAttributeValue<OptionSetValue>("crda9_type").Value}"
+ " " + $"site:{entity.GetAttributeValue<string>("crda9_site")}");
}
?
? a. 代码及使用
// Microsoft::Xrm::Sdk::EntityCollection ^ RetrieveMultiple(Microsoft::Xrm::Sdk::Query::QueryBase ^ query);
static void RetriveMultiple(IOrganizationService service){
QueryExpression query = new("crda9_room") { }; // 表逻辑名称
EntityCollection results = service.RetrieveMultiple(query); // 查询发送
foreach (Entity entity in results.Entities){
Console.WriteLine($"Id:{entity.Id}"); // 全局唯一标识符(每行数据有一个Id)
Console.WriteLine($"name:{entity.Attributes["crda9_name"]}");
}
}
? b. QueryExpression 总结
public ref class QueryExpression sealed : Microsoft::Xrm::Sdk::Query::QueryBase
query.EntityName = "crda9_room"; // 1.EntityName --> 表逻辑名(可在构造函数时直接构造)
query.ColumnSet.AddColumns("crda9_name"); // 2.ColumnSet --> 负责查询列数
query.Criteria.AddCondition("crda9_type", ConditionOperator.Equal, 0); // 3.Criteria --> 查询限定条件
query.AddOrder("crda9_name", OrderType.Ascending); // 4. AddOrder --> 列排序
query.TopCount = 2; // 5. TopCount --> 控制显示的行数(与PageInfo不能同时使用)
query.PageInfo = new PagingInfo() { // 6. PageInfor --> 控制分页
PageNumber = 1, // 页数
Count = 2 // 每页数量
};
?
? a. 代码及使用
// Guid Create(Microsoft::Xrm::Sdk::Entity ^ entity);
static void CreateExpressionExample(IOrganizationService service)
{
Entity entity = new Entity("crda9_student");
entity["crda9_name"] = "新学生_张雪雪";
entity["crda9_age"] = 17;
Guid id = service.Create(entity);
Console.WriteLine("新学生的唯一id为:" + id);
}
?
? a. 代码及使用
// void Delete(System::String ^ entityName, Guid id);
static void DeleteExpressionExample(IOrganizationService service)
{
string entityName = "crda9_student"; // 逻辑名称
Guid guid = new Guid("e334ba87-4c9a-ee11-be37-000d3a85d073"); // 全局唯一标识符
service.Delete(entityName, guid);
}
?
? a. 代码及使用
// void Update(Microsoft::Xrm::Sdk::Entity ^ entity);
static void UpdateExpressionExample(IOrganizationService service)
{
Guid id = CreateExpressionExample(service); // 先调用函数创建一个新学生,返回其Id唯一标识
Entity entity = new Entity("crda9_student");
entity.Id = id;
entity["crda9_name"] = "更新后的学生";
service.Update(entity);
Console.WriteLine("successful update....");
}