前段时间为了做项目调研,写了一些测试API的例子。这些API主要涉及这些模块: BusinessUnit, User, Role。把它分享出来,希望对大家的工作有所帮助。

APIs

No

Module  Name

Name

Comments

1

BusinessUnit

GetBUs

Retrieve all of Bus in current CRM  system.

2

DisableBU

Disable BU entry

3

EnableBU

Enable BU entry

4

DeleteBU

Delete BU entry

5

ChangeParentBU

Change BU entry’s parent

6

Role

GetRoles

Retrieve all of role entries

7

DeleteRole

Delete role entry

8

Team

GetTeams

Retrieve all of team entries

9

GetTeamRoles

Retrieve all role of one team

10

GraintTeamRole

Grant some roles to one team

11

RevokeTeamRole

Revoke some roles from one team

12

AddTeamMembers

Add some members into one team

13

RemoveTeamMembers

Remove some members from one team

14

ChangeTeamBU

Change one team’s BU

15

RetrieveTeamMembers

Retrieve all members of one team

16

User

GetUsers

Get all of users in current CRM system

17

GrantUserRole

Grant some roles to user

18

RevokeUserRole

Revoke some roles from user

19

AssignTeamToUser

Involve user into a team

20

RemoveTeamFromUser

Exclude user from a team

21

ChangeUserBU

Change user’s BU

Codes

      #region BU

        static void GetBUs()
{
QueryExpression query = new QueryExpression("businessunit");
query.ColumnSet = new ColumnSet(true);
EntityCollection businessUnits = crmSvc.RetrieveMultiple(query); if (businessUnits == null)
{
Console.WriteLine("Did not get any BU record");
return;
}
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("|{0,3}|{1,30}|{2,30}|","Num","Name","Parent Name");
Console.BackgroundColor = ConsoleColor.Black;
for (int i = 0; i < businessUnits.Entities.Count; i++)
{
Console.WriteLine("|{0,3}|{1,30}|{2,30}|",i, businessUnits.Entities[i].GetAttributeValue<string>("name")
, businessUnits.Entities[i].GetAttributeValue<EntityReference>("parentbusinessunitid")==null?"NULL":businessUnits.Entities[i].GetAttributeValue<EntityReference>("parentbusinessunitid").Name);
}
Console.WriteLine();
} static void DisableBU()
{
SetStateRequest request = new SetStateRequest();
request.EntityMoniker = new EntityReference("businessunit", TEST_BU_1);
request.State = new OptionSetValue(1);
request.Status = new OptionSetValue(0);
SetStateResponse response = crmSvc.Execute(request) as SetStateResponse;
Console.WriteLine("Disabled operation is done!");
} static void EnableBU()
{
SetStateRequest request = new SetStateRequest();
request.EntityMoniker = new EntityReference("businessunit", TEST_BU_1);
request.State = new OptionSetValue(0);
request.Status = new OptionSetValue(0);
SetStateResponse response = crmSvc.Execute(request) as SetStateResponse;
Console.WriteLine("Enabled operation is done!");
} static void DeleteBU()
{
try
{
crmSvc.Delete("businessunit", TEST_BU_1);
}
catch (FaultException ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("Deleted operation is done!");
} static void ChangeParentBU()
{
SetParentBusinessUnitRequest request = new SetParentBusinessUnitRequest();
request.BusinessUnitId = TEST_BU_2;
request.ParentId = TEST_BU_1; crmSvc.Execute(request);
Console.WriteLine("Changing BU's parent BU is done!");
} #endregion #region Role
static void GetRoles()
{
QueryExpression query = new QueryExpression("role");
query.ColumnSet = new ColumnSet(true);
query.LinkEntities.Add(new LinkEntity("role", "businessunit", "businessunitid", "businessunitid", JoinOperator.Inner));
query.LinkEntities[0].Columns = new ColumnSet(true);
query.LinkEntities[0].EntityAlias = "bu";
EntityCollection roles = crmSvc.RetrieveMultiple(query); if (roles == null)
{
Console.WriteLine("Did not get any Role record");
return;
} Console.WindowWidth = 192;
Console.WindowHeight = 54; Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", "Num", "Role Name", "BU Name","Id");
Console.BackgroundColor = ConsoleColor.Black;
for (int i = 0; i < roles.Entities.Count; i++)
{
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", i, roles.Entities[i].GetAttributeValue<string>("name")
, ((Microsoft.Xrm.Sdk.AliasedValue)roles.Entities[i]["bu.name"]).Value
, roles.Entities[i]["roleid"].ToString());
}
Console.WriteLine(); } static void DeleteRole()
{
crmSvc.Delete("role", TEST_ROLE);
}
#endregion #region Team
static void GetTeams()
{
QueryExpression query = new QueryExpression("team");
query.ColumnSet = new ColumnSet(true);
query.LinkEntities.Add(new LinkEntity("team", "businessunit", "businessunitid", "businessunitid", JoinOperator.Inner));
query.LinkEntities[0].Columns = new ColumnSet(true);
query.LinkEntities[0].EntityAlias = "bu"; EntityCollection teams = crmSvc.RetrieveMultiple(query);
if (teams == null && teams.Entities.Count == 0)
{
Console.WriteLine("Did not get any Role record");
return;
} Console.WindowWidth = 192;
Console.WindowHeight = 54;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", "Num", "Team Name", "BU Name","Id");
Console.BackgroundColor = ConsoleColor.Black;
for (int i = 0; i < teams.Entities.Count; i++)
{
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", i, teams.Entities[i].GetAttributeValue<string>("name")
, ((Microsoft.Xrm.Sdk.AliasedValue)teams.Entities[i]["bu.name"]).Value
, teams.Entities[i]["teamid"]);
}
Console.WriteLine(); } static void GetTeamRoles()
{
QueryExpression query = new QueryExpression("team");
query.ColumnSet = new ColumnSet(true);
query.LinkEntities.Add(new LinkEntity("team", "teamroles", "teamid", "teamid", JoinOperator.Inner));
query.LinkEntities[0].Columns = new ColumnSet(true);
query.LinkEntities[0].EntityAlias = "multi"; query.LinkEntities[0].AddLink("role", "roleid", "roleid", JoinOperator.Inner);
query.LinkEntities[0].LinkEntities[0].Columns = new ColumnSet(true);
query.LinkEntities[0].LinkEntities[0].EntityAlias = "role"; query.LinkEntities[0].LinkEntities[0].AddLink("businessunit", "businessunitid", "businessunitid", JoinOperator.Inner);
query.LinkEntities[0].LinkEntities[0].LinkEntities[0].Columns = new ColumnSet(true);
query.LinkEntities[0].LinkEntities[0].LinkEntities[0].EntityAlias = "bu"; query.Criteria.AddCondition(new ConditionExpression("teamid", ConditionOperator.Equal, TEST_TEAM_1)); EntityCollection roles = crmSvc.RetrieveMultiple(query); if (roles == null)
{
Console.WriteLine("Did not get any Role record");
return;
} Console.WindowWidth = 192;
Console.WindowHeight = 54; Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}|", "Num", "Role Name", "BU Name","Id");
Console.BackgroundColor = ConsoleColor.Black;
for (int i = 0; i < roles.Entities.Count; i++)
{
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}|", i, ((Microsoft.Xrm.Sdk.AliasedValue)roles.Entities[i]["role.name"]).Value
, ((Microsoft.Xrm.Sdk.AliasedValue)roles.Entities[i]["bu.name"]).Value
, ((Microsoft.Xrm.Sdk.AliasedValue)roles.Entities[i]["role.roleid"]).Value);
}
Console.WriteLine(); } static void GraintTeamRole()
{
Relationship rp = new Relationship("teamroles_association");
EntityReferenceCollection roleCollection = new EntityReferenceCollection();
roleCollection.Add(new EntityReference("role",TEST_USER_ROLE_1));
roleCollection.Add(new EntityReference("role", TEST_USER_ROLE_2)); crmSvc.Associate("team", TEST_TEAM_1, rp, roleCollection);
Console.WriteLine("Grainting team role is done!");
} static void RevokeTeamRole()
{
Relationship rp = new Relationship("teamroles_association");
EntityReferenceCollection roleCollection = new EntityReferenceCollection();
roleCollection.Add(new EntityReference("role", TEST_TEAM_ROLE_1));
roleCollection.Add(new EntityReference("role", TEST_TEAM_ROLE_2)); crmSvc.Disassociate("team", TEST_TEAM_1, rp, roleCollection);
Console.WriteLine("Revoking team role is done!"); } static void AddTeamMembers()
{
AddMembersTeamRequest request = new AddMembersTeamRequest();
request.MemberIds=new Guid[]{TEST_SYSTEMUSER_1,TEST_SYSTEMUSER_2};
request.TeamId = TEST_TEAM_1; crmSvc.Execute(request);
Console.WriteLine("Adding team members is done!");
} static void RemoveTeamMembers()
{
RemoveMembersTeamRequest request = new RemoveMembersTeamRequest();
request.MemberIds = new Guid[] { TEST_SYSTEMUSER_1};
request.TeamId = TEST_TEAM_1; crmSvc.Execute(request);
Console.WriteLine("Remove team member is done!");
} static void ChangeTeamBU()
{
SetParentTeamRequest request = new SetParentTeamRequest();
request.BusinessId = TEST_BU_1;
request.TeamId = TEST_TEAM_1; crmSvc.Execute(request);
Console.WriteLine("Changing Business Unit is done!");
} static void RetrieveTeamMembers()
{
List<Guid> members = new List<Guid>();
RetrieveMembersTeamRequest request = new RetrieveMembersTeamRequest();
request.EntityId = TEST_TEAM_1;
request.MemberColumnSet = new ColumnSet(true); RetrieveMembersTeamResponse response = crmSvc.Execute(request) as RetrieveMembersTeamResponse;
for (int i = 0; i < response.EntityCollection.Entities.Count; i++)
{
members.Add(response.EntityCollection.Entities[i].GetAttributeValue<Guid>("systemuserid"));
} //Get user list
QueryExpression query = new QueryExpression("systemuser");
query.ColumnSet = new ColumnSet(true);
query.LinkEntities.Add(new LinkEntity("systemuser", "businessunit", "businessunitid", "businessunitid", JoinOperator.Inner));
query.LinkEntities[0].Columns = new ColumnSet(true);
query.LinkEntities[0].EntityAlias = "bu"; query.Criteria.FilterOperator = LogicalOperator.Or;
foreach (Guid item in members)
{
query.Criteria.AddCondition(new ConditionExpression("systemuserid",ConditionOperator.Equal,item));
} EntityCollection users = crmSvc.RetrieveMultiple(query);
if (users == null && users.Entities.Count == 0)
{
Console.WriteLine("Did not get any User record");
return;
} Console.WindowWidth = 192;
Console.WindowHeight = 54;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", "Num", "Team Name", "BU Name", "Id");
Console.BackgroundColor = ConsoleColor.Black;
for (int i = 0; i < users.Entities.Count; i++)
{
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", i, users.Entities[i].GetAttributeValue<string>("fullname")
, ((Microsoft.Xrm.Sdk.AliasedValue)users.Entities[i]["bu.name"]).Value
, users.Entities[i]["systemuserid"]);
}
Console.WriteLine(); Console.WriteLine();
} #endregion #region systemuser static void GetUsers()
{
QueryExpression query = new QueryExpression("systemuser");
query.ColumnSet = new ColumnSet(true);
query.LinkEntities.Add(new LinkEntity("systemuser", "businessunit", "businessunitid", "businessunitid", JoinOperator.Inner));
query.LinkEntities[0].Columns = new ColumnSet(true);
query.LinkEntities[0].EntityAlias = "bu"; EntityCollection users = crmSvc.RetrieveMultiple(query);
if (users == null && users.Entities.Count == 0)
{
Console.WriteLine("Did not get any User record");
return;
} Console.WindowWidth = 192;
Console.WindowHeight = 54;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", "Num", "Team Name", "BU Name","Id");
Console.BackgroundColor = ConsoleColor.Black;
for (int i = 0; i < users.Entities.Count; i++)
{
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", i, users.Entities[i].GetAttributeValue<string>("fullname")
, ((Microsoft.Xrm.Sdk.AliasedValue)users.Entities[i]["bu.name"]).Value
, users.Entities[i]["systemuserid"]);
}
Console.WriteLine();
} static void GrantUserRole()
{
Relationship rp1 = new Relationship("systemuserroles_association");
EntityReferenceCollection roleList = new EntityReferenceCollection();
roleList.Add(new EntityReference("role", TEST_USER_ROLE_1));
roleList.Add(new EntityReference("role", TEST_USER_ROLE_2)); crmSvc.Associate("systemuser", TEST_USER_1, rp1, roleList);
Console.WriteLine("Granting user role is done!"); } static void RevokeUserRole()
{
Relationship rp1 = new Relationship("systemuserroles_association");
EntityReferenceCollection roleList = new EntityReferenceCollection();
roleList.Add(new EntityReference("role", TEST_USER_ROLE_1));
roleList.Add(new EntityReference("role", TEST_USER_ROLE_2)); crmSvc.Disassociate("systemuser", TEST_USER_1, rp1, roleList);
Console.WriteLine("Granting user role is done!");
} static void AssignTeamToUser()
{
Relationship rp1 = new Relationship("teammembership_association");
EntityReferenceCollection teamList = new EntityReferenceCollection();
teamList.Add(new EntityReference("team",TEST_TEAM_1)); crmSvc.Associate("systemuser",TEST_USER_1,rp1,teamList);
Console.WriteLine("Assigning team is done!");
} static void RemoveTeamFromUser()
{
Relationship rp1 = new Relationship("teammembership_association");
EntityReferenceCollection teamList = new EntityReferenceCollection();
teamList.Add(new EntityReference("team",TEST_TEAM_1)); crmSvc.Disassociate("systemuser",TEST_USER_1,rp1,teamList);
Console.WriteLine("Removing team is done!");
} static void ChangeUserBU()
{
SetBusinessSystemUserRequest request = new SetBusinessSystemUserRequest();
request.BusinessId = TEST_BU_1;
request.UserId = TEST_USER_1;
request.ReassignPrincipal = new EntityReference("systemuser",TEST_USER_1);
crmSvc.Execute(request);
Console.WriteLine("Changing user's bu is done!");
} #endregion #region Announcemnets static void GetAnnouncements()
{
QueryExpression query = new QueryExpression("businessunitnewsarticle");
query.ColumnSet = new ColumnSet(true); EntityCollection news = crmSvc.RetrieveMultiple(query);
if (news == null || news.Entities.Count == 0)
{
Console.WriteLine("Did not get any new from crm!");
return;
} Console.WindowWidth = 192;
Console.WindowHeight = 54;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", "Num", "Title", "Expired Date","Id");
Console.BackgroundColor = ConsoleColor.Black;
for (int i = 0; i < news.Entities.Count; i++)
{
Console.WriteLine("|{0,3}|{1,30}|{2,30}|{3,38}", i, news.Entities[i].GetAttributeValue<string>("articletitle")
, news.Entities[i].GetAttributeValue<DateTime>("activeuntil")!=null?news.Entities[i].GetAttributeValue<DateTime>("activeuntil").ToString("yyyy-MM-dd"):"NULL"
, news.Entities[i].GetAttributeValue<Guid>("businessunitnewsarticleid"));
}
} static void AddAnnouncements()
{
Entity news = new Entity("businessunitnewsarticle");
news["articletitle"] = "Test Announcement 3";
news["newsarticle"] = "Announcement Body";
news["activeuntil"]=DateTime.Now.AddDays(10);
crmSvc.Create(news); Console.WriteLine("Adding Announcement is done!");
} static void DeleteAnnouncements()
{ //Entity news = new Entity("businessunitnewsarticle");
//news["articletitle"] = "Test Announcement 3";
//news["newsarticle"] = "Announcement Body";
//news["activeuntil"] = DateTime.Now;
//news.Id=crmSvc.Create(news); crmSvc.Delete("businessunitnewsarticle", TEST_ANNOUNCEMENT_1);
Console.WriteLine("Deleting Announcement is done!");
} static void UpdateAnnouncements()
{
Entity news = crmSvc.Retrieve("businessunitnewsarticle",TEST_ANNOUNCEMENT_1,new ColumnSet(true));
news["newsarticle"]+="_update!";
crmSvc.Update(news);
Console.WriteLine("Updating Announcement is done!"); } #endregion #region System Job static void GetSystemJobList()
{
FetchExpression fetch = new FetchExpression(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='asyncoperation'>
<attribute name='asyncoperationid' />
<attribute name='name' />
<attribute name='regardingobjectid' />
<attribute name='operationtype' />
<attribute name='statuscode' />
<attribute name='ownerid' />
<attribute name='startedon' />
<attribute name='statecode' />
<attribute name='messagename' />
<attribute name='friendlymessage' />
<attribute name='message' />
<order attribute='startedon' descending='true' />
</entity>
</fetch>");
var result = crmSvc.RetrieveMultiple(fetch); foreach (Entity item in result.Entities)
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine(" ");
Console.BackgroundColor = ConsoleColor.Black; Console.WriteLine("{0}:{1}", "NAME", item["name"]);
Console.WriteLine("{0}:{1}", "OPERATIONTYPE", ParseOperationType(((OptionSetValue)item["operationtype"]).Value));
Console.WriteLine("{0}:{1}", "STATECODE", ParseOperationState(((OptionSetValue)item["statecode"]).Value));
Console.WriteLine("{0}:{1}", "STATUSCODE", ParseOperationStatus(((OptionSetValue)item["statuscode"]).Value)); if(item.Contains("friendlymessage"))
Console.WriteLine("{0}:{1}", "FRIENDLY MESSAGE", item["friendlymessage"]!=null?item["friendlymessage"]:"NULL");
if (item.Contains("message"))
Console.WriteLine("{0}:{1}", "MESSAGE", item["message"]!=null?item["message"]:"NULL"); } //Console.WriteLine(result.Entities.Count);
} static string ParseOperationType(int value)
{
string result=string.Empty;
Type operationType = typeof(AsyncOperationType);
foreach (var field in operationType.GetFields())
{
if ((int)field.GetValue(operationType) == value)
{
result = field.Name;
break;
}
}
return result;
} static string ParseOperationStatus(int value)
{
string result = string.Empty;
Type operationStatus = typeof(AsyncOperationStatus);
foreach (var field in operationStatus.GetFields())
{
if ((int)field.GetValue(operationStatus) == value)
{
result = field.Name;
break;
}
}
return result;
} static string ParseOperationState(int value)
{
var tmp = (AsyncOperationState)value;
return tmp.ToString(); } #endregion #region Common Functions #endregion

BusinessUnit, User, Role 中常用的APIs的更多相关文章

  1. elasticsearch中常用的API

    elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...

  2. 收集一些工作中常用的经典SQL语句

    作为一枚程序员来说和数据库打交道是不可避免的,现收集一下工作中常用的SQL语句,希望能给大家带来一些帮助,当然不全面,欢迎补充! 1.执行插入语句,获取自动生成的递增的ID值 INSERT INTO ...

  3. ansible中常用模块详解

    ansible中常用的模块详解: file模块 ansible内置的可以查看模块用法的命令如下: [root@docker5 ~]# ansible-doc -s file - name: Sets ...

  4. ES6系列之项目中常用的新特性

    ES6系列之项目中常用的新特性 ES6常用特性 平时项目开发中灵活运用ES6+语法可以让开发者减少很多开发时间,提高工作效率.ES6版本提供了很多新的特性,接下来我列举项目中常用的ES6+的特性: l ...

  5. 【原】实时渲染中常用的几种Rendering Path

    [原]实时渲染中常用的几种Rendering Path 本文转载请注明出处 —— polobymulberry-博客园 本文为我的图形学大作业的论文部分,介绍了一些Rendering Path,比较简 ...

  6. J2EE项目开发中常用到的公共方法

    在项目IDCM中涉及到多种工单,包括有:服务器|网络设备上下架工单.服务器|网络设备重启工单.服务器光纤网线更换工单.网络设备撤线布线工单.服务器|网络设备替换工单.服务器|网络设备RMA工单.通用原 ...

  7. 工作中常用的js、jquery自定义扩展函数代码片段

    仅记录一些我工作中常用的自定义js函数. 1.获取URL请求参数 //根据URL获取Id function GetQueryString(name) { var reg = new RegExp(&q ...

  8. 【转载】Linux中常用操作命令

    说明:开始学习linux系统,为了方便查看,特转载一篇Linux中常用操作命令,转载地址:http://www.cnblogs.com/laov/p/3541414.html 正文: Linux简介及 ...

  9. 【Unity3d游戏开发】Unity3D中常用的物理学公式

    马三最近在一直负责Unity中的物理引擎这一块,众所周知,Unity内置了NVIDIA公司PhysX物理引擎.然而,马三一直觉得只会使用引擎而不去了解原理的程序猿不是一位老司机.所以对一些常用的物理学 ...

随机推荐

  1. django 项目运行时static静态文件不能加载问题处理

    一.首先检查网页中的加载路径是否正确,如果和文件所在路径不一致,就把html改下路径 二.加载路径和文件实际路径一致,看下配置文件: STATIC_URL = '/static/'STATIC_ROO ...

  2. html (第四本书第五章参考)

    上机1 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...

  3. Nginx增加模块

    http://blog.csdn.net/loyachen/article/details/50902667

  4. spring-boot 速成(11) - 单元测试

    一.添加依赖项: testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.2.RELEASE' 二.单元测试代码示例 im ...

  5. LightOJ 1074 - Extended Traffic (SPFA)

    http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic   PDF (English) Stati ...

  6. CentOS 6.8 搭建 Git 代码托管系统 Gitea

    [荐] Gitea:Git with a cup of tea,在 Gogs 基础上,发展起来的 自助 Git 服务系统.Gogs是一个个人维护的版本,而Gitea是一个社区组织维护的,版本迭代更新快 ...

  7. kcon 黑客大会 github

    https://github.com/knownsec/KCon https://paper.seebug.org/107/ CISP认证 https://habo.qq.com/ https://g ...

  8. Python yield使用

    https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 您可能听说过,带有 yield 的函数在 Python 中被称 ...

  9. AngularJS路由系列(4)-- UI-Router的$state服务、路由事件、获取路由参数

    本系列探寻AngularJS的路由机制,在WebStorm下开发.主要包括: ● UI-Router的$state服务● UI-Router的路由事件● UI-Router获取路由参数 Angular ...

  10. 使用Visual Studio 2012远程调试Windows Azure网站

    登录Windows Azure门户,点击"所有项目"中的网站名称. 点击"配置". 在"远程调试"选项中选择"打开",在 ...