BusinessUnit, User, Role 中常用的APIs
前段时间为了做项目调研,写了一些测试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