Produced by Araxis Merge on 2/1/2017 2:56:33 PM Eastern Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | C:\Araxis_Merge_Comprasion\Pub_un\BTSSS_CIF_122016.zip\BTSSS_CIF_12_20_16\clean\CRM\trunk\SDK\SampleCode\CS\GeneralProgramming\EarlyBound | ReassignBusinessUnitMembers.cs | Tue Dec 20 19:51:45 2016 UTC |
| 2 | Wed Feb 1 19:56:33 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 592 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 2 | // This f ile is par t of the M icrosoft D ynamics CR M SDK code samples. | |||||
| 3 | // | |||||
| 4 | // Copyri ght (C) Mi crosoft Co rporation. All righ ts reserve d. | |||||
| 5 | // | |||||
| 6 | // This s ource code is intend ed only as a supplem ent to Mic rosoft | |||||
| 7 | // Develo pment Tool s and/or o n-line doc umentation . See the se other | |||||
| 8 | // materi als for de tailed inf ormation r egarding M icrosoft c ode sample s. | |||||
| 9 | // | |||||
| 10 | // THIS C ODE AND IN FORMATION ARE PROVID ED "AS IS" WITHOUT W ARRANTY OF ANY | |||||
| 11 | // KIND, EITHER EXP RESSED OR IMPLIED, I NCLUDING B UT NOT LIM ITED TO TH E | |||||
| 12 | // IMPLIE D WARRANTI ES OF MERC HANTABILIT Y AND/OR F ITNESS FOR A | |||||
| 13 | // PARTIC ULAR PURPO SE. | |||||
| 14 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 15 | ||||||
| 16 | //<snippet ReassignBu sinessUnit Members> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Collect ions.Gener ic; | |||||
| 19 | using Syst em.Linq; | |||||
| 20 | using Syst em.Service Model; | |||||
| 21 | ||||||
| 22 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 23 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 24 | using Micr osoft.Xrm. Sdk; | |||||
| 25 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 26 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 27 | ||||||
| 28 | // This na mespace is found in Microsoft. Crm.Sdk.Pr oxy.dll as sembly | |||||
| 29 | // found i n the SDK\ bin folder . | |||||
| 30 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 31 | ||||||
| 32 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 33 | { | |||||
| 34 | /// <s ummary> | |||||
| 35 | /// De monstrates how to us e the Reas signObject sOwnerRequ est and | |||||
| 36 | /// Re assignObje ctsSystemU serRequest objects, as well as how to re assign | |||||
| 37 | /// bu siness uni t members and teams to other b usiness un its and te ams and | |||||
| 38 | /// th en delete those busi ness units and teams . | |||||
| 39 | /// </ summary> | |||||
| 40 | /// <p aram name= "serverCon fig">Conta ins server connectio n informat ion.</para m> | |||||
| 41 | /// <p aram name= "promptfor Delete">Wh en True, t he user wi ll be prom pted to de lete all | |||||
| 42 | /// cr eated enti ties.</par am> | |||||
| 43 | public class Rea ssignBusin essUnitMem bers | |||||
| 44 | { | |||||
| 45 | #r egion Clas s Level Me mbers | |||||
| 46 | ||||||
| 47 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 48 | pr ivate Serv iceContext _context; | |||||
| 49 | ||||||
| 50 | pr ivate List <Guid> _us ers; | |||||
| 51 | pr ivate Team _team; | |||||
| 52 | pr ivate Lead [] _leads = new Lead [4]; | |||||
| 53 | pr ivate Busi nessUnit _ buisnessUn it; | |||||
| 54 | pr ivate Busi nessUnit _ rootBusine ssUnit; | |||||
| 55 | pr ivate List <Guid> _or iginalRole sIds; | |||||
| 56 | ||||||
| 57 | #e ndregion C lass Level Members | |||||
| 58 | ||||||
| 59 | #r egion How To Sample Code | |||||
| 60 | // / <summary > | |||||
| 61 | // / This met hod first creates 3 users, a t eam, 4 lea ds and a b usiness un it. | |||||
| 62 | // / It assig ns two use rs to the team, and gives each user and the team a lead. | |||||
| 63 | // / Then it reassigns all the le ads from o ne of the users to a nother use r using | |||||
| 64 | // / the Reas signObject sSystemUse rRequest. Next, it r eassigns a ll the lea ds from | |||||
| 65 | // / the team to a user using the ReassignO bjectsOwne rRequest. Third, it reassigns | |||||
| 66 | // / one user from the root busin ess unit t o the crea ted busine ss unit, u sing the | |||||
| 67 | // / SetBusin essSystemU serRequest message. Fourth, it reassigns all users from | |||||
| 68 | // / the crea ted busine ss unit to the root business u nit and de letes the created | |||||
| 69 | // / business unit. Fin ally, it r emoves all users fro m the crea ted team a nd | |||||
| 70 | // / deletes the team. | |||||
| 71 | // / </summar y> | |||||
| 72 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 73 | // / <param n ame="promp tforDelete ">When Tru e, the use r will be prompted t o delete a ll | |||||
| 74 | // / created entities.< /param> | |||||
| 75 | ||||||
| 76 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt forDelete) | |||||
| 77 | { | |||||
| 78 | try | |||||
| 79 | { | |||||
| 80 | //<snippet ReassignBu sinessUnit Members1> | |||||
| 81 | // C onnect to the Organi zation ser vice. | |||||
| 82 | // T he using s tatement a ssures tha t the serv ice proxy will be pr operly | |||||
| 83 | // d isposed. | |||||
| 84 | usin g (_servic eProxy = n ew Organiz ationServi ceProxy(se rverConfig .Organizat ionUri, se rverConfig .HomeRealm Uri,server Config.Cre dentials, serverConf ig.DeviceC redentials )) | |||||
| 85 | // U sing the S erviceCont ext class provides a ccess to t he LINQ pr ovider | |||||
| 86 | usin g (_contex t = new Se rviceConte xt(_servic eProxy)) | |||||
| 87 | { | |||||
| 88 | // This st atement is required to enable early-boun d type sup port. | |||||
| 89 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 90 | ||||||
| 91 | CreateRequ iredRecord s(); | |||||
| 92 | ||||||
| 93 | Console.Wr iteLine(); | |||||
| 94 | PrintLeads (); | |||||
| 95 | ||||||
| 96 | var users = from use r in _cont ext.System UserSet | |||||
| 97 | sele ct new { u ser.FullNa me, user.I d }; | |||||
| 98 | Dictionary <Guid, Str ing> userM apping = n ew Diction ary<Guid,S tring>(); | |||||
| 99 | foreach (v ar user in users) | |||||
| 100 | userMa pping.Add( user.Id, u ser.FullNa me); | |||||
| 101 | ||||||
| 102 | #region Re assignObje ctsSystemU serRequest | |||||
| 103 | ||||||
| 104 | // create the reques t | |||||
| 105 | ReassignOb jectsSyste mUserReque st reassig nRequest = | |||||
| 106 | new Re assignObje ctsSystemU serRequest () | |||||
| 107 | { | |||||
| 108 | Reassi gnPrincipa l = | |||||
| 109 | ne w EntityRe ference(Sy stemUser.E ntityLogic alName, _u sers[1]), | |||||
| 110 | UserId = _users[ 2] | |||||
| 111 | }; | |||||
| 112 | ||||||
| 113 | // execute the reque st | |||||
| 114 | Console.Wr iteLine(); | |||||
| 115 | Console.Wr iteLine( | |||||
| 116 | " Rea ssigning l eads from {0} to {1} ", | |||||
| 117 | userMa pping[_use rs[2]], | |||||
| 118 | userMa pping[_use rs[1]]); | |||||
| 119 | _servicePr oxy.Execut e(reassign Request); | |||||
| 120 | ||||||
| 121 | // check r esults | |||||
| 122 | PrintLeads (); | |||||
| 123 | ||||||
| 124 | #endregion | |||||
| 125 | ||||||
| 126 | #region Re assignObje ctsOwnerRe quest | |||||
| 127 | ||||||
| 128 | // create the reques t | |||||
| 129 | ReassignOb jectsOwner Request re quest = | |||||
| 130 | new Re assignObje ctsOwnerRe quest() | |||||
| 131 | { | |||||
| 132 | FromPr incipal = _team.ToEn tityRefere nce(), | |||||
| 133 | ToPrin cipal = | |||||
| 134 | ne w EntityRe ference(Sy stemUser.E ntityLogic alName, _u sers[0]) | |||||
| 135 | }; | |||||
| 136 | ||||||
| 137 | // execute the reque st | |||||
| 138 | Console.Wr iteLine(); | |||||
| 139 | Console.Wr iteLine( | |||||
| 140 | " Rea ssigning l eads from {0} to {1} ", | |||||
| 141 | _team. Name, user Mapping[_u sers[0]]); | |||||
| 142 | _servicePr oxy.Execut e(request) ; | |||||
| 143 | ||||||
| 144 | // check r esults | |||||
| 145 | PrintLeads (); | |||||
| 146 | ||||||
| 147 | #endregion | |||||
| 148 | ||||||
| 149 | #region re assign bus iness unit members | |||||
| 150 | ||||||
| 151 | Console.Wr iteLine(); | |||||
| 152 | Console.Wr iteLine(" Adding a user to th e created business u nit"); | |||||
| 153 | // track w hat permis sions the user had b efore reas signing to the new | |||||
| 154 | // busines s unit so that the p ermissions can be re stored whe n the | |||||
| 155 | // user is assigned back to th e business unit | |||||
| 156 | _originalR olesIds = new List<G uid>(); | |||||
| 157 | var roleId s = from u ser in _co ntext.Syst emUserSet | |||||
| 158 | join s ystemuserr ole in _co ntext.Syst emUserRole sSet | |||||
| 159 | on u ser.System UserId equ als system userrole.S ystemUserI d | |||||
| 160 | join r ole in _co ntext.Role Set | |||||
| 161 | on s ystemuserr ole.RoleId equals ro le.RoleId | |||||
| 162 | where user.Syste mUserId.Va lue == _us ers[2] | |||||
| 163 | select role.Role Id.Value; | |||||
| 164 | ||||||
| 165 | foreach (v ar roleId in roleIds ) | |||||
| 166 | _origi nalRolesId s.Add(role Id); | |||||
| 167 | ||||||
| 168 | // add use r to the c reated bus iness unit | |||||
| 169 | _servicePr oxy.Execut e(new SetB usinessSys temUserReq uest() | |||||
| 170 | { | |||||
| 171 | Busine ssId = _bu isnessUnit .Id, | |||||
| 172 | Reassi gnPrincipa l = new En tityRefere nce( | |||||
| 173 | Sy stemUser.E ntityLogic alName, | |||||
| 174 | _u sers[2]), | |||||
| 175 | UserId = _users[ 2] | |||||
| 176 | }); | |||||
| 177 | ||||||
| 178 | #endregion | |||||
| 179 | ||||||
| 180 | #region de lete busin ess unit | |||||
| 181 | ||||||
| 182 | Console.Wr iteLine(); | |||||
| 183 | Console.Wr iteLine(" Deleting created bu siness uni t"); | |||||
| 184 | ||||||
| 185 | // remove all users from the b usiness un it, moving them back to the | |||||
| 186 | // parent business u nit | |||||
| 187 | _servicePr oxy.Execut e(new SetB usinessSys temUserReq uest() | |||||
| 188 | { | |||||
| 189 | Busine ssId = _ro otBusiness Unit.Id, | |||||
| 190 | Reassi gnPrincipa l = new En tityRefere nce( | |||||
| 191 | Sy stemUser.E ntityLogic alName, _u sers[2]), | |||||
| 192 | UserId = _users[ 2] | |||||
| 193 | }); | |||||
| 194 | ||||||
| 195 | // give th e user bac k their or iginal sec urity role s | |||||
| 196 | foreach (v ar roleId in roleIds ) | |||||
| 197 | { | |||||
| 198 | _servi ceProxy.As sociate( | |||||
| 199 | S ystemUser. EntityLogi calName, | |||||
| 200 | _ users[2], | |||||
| 201 | n ew Relatio nship("sys temuserrol es_associa tion"), | |||||
| 202 | n ew EntityR eferenceCo llection() { | |||||
| 203 | new Enti tyReferenc e( | |||||
| 204 | Role .EntityLog icalName, | |||||
| 205 | role Id | |||||
| 206 | ) | |||||
| 207 | } | |||||
| 208 | ); | |||||
| 209 | } | |||||
| 210 | ||||||
| 211 | // deactiv ate busine ss unit be fore delet ing it | |||||
| 212 | _servicePr oxy.Execut e(new SetS tateReques t() | |||||
| 213 | { | |||||
| 214 | Entity Moniker = _buisnessU nit.ToEnti tyReferenc e(), | |||||
| 215 | // mar k the stat e as inact ive (value 1) | |||||
| 216 | State = new Opti onSetValue (1), | |||||
| 217 | Status = new Opt ionSetValu e(-1) | |||||
| 218 | }); | |||||
| 219 | ||||||
| 220 | // delete business u nit | |||||
| 221 | _servicePr oxy.Delete (BusinessU nit.Entity LogicalNam e, | |||||
| 222 | _buisn essUnit.Id ); | |||||
| 223 | ||||||
| 224 | #endregion | |||||
| 225 | ||||||
| 226 | #region re move users from team | |||||
| 227 | ||||||
| 228 | var teamMe mbers = fr om team in _context. TeamSet | |||||
| 229 | jo in members hip in _co ntext.Team Membership Set | |||||
| 230 | on team.Team Id equals membership .TeamId | |||||
| 231 | wh ere team.T eamId == _ team.Id | |||||
| 232 | se lect membe rship.Syst emUserId.V alue; | |||||
| 233 | ||||||
| 234 | _servicePr oxy.Execut e(new Remo veMembersT eamRequest () | |||||
| 235 | { | |||||
| 236 | Member Ids = team Members.To Array(), | |||||
| 237 | TeamId = _team.I d | |||||
| 238 | }); | |||||
| 239 | ||||||
| 240 | #endregion | |||||
| 241 | ||||||
| 242 | #region de lete team | |||||
| 243 | ||||||
| 244 | Console.Wr iteLine(); | |||||
| 245 | Console.Wr iteLine(" Deleting the team") ; | |||||
| 246 | ||||||
| 247 | // Delete the team | |||||
| 248 | _servicePr oxy.Delete (Team.Enti tyLogicalN ame, _team .Id); | |||||
| 249 | ||||||
| 250 | #endregion | |||||
| 251 | ||||||
| 252 | DeleteRequ iredRecord s(promptfo rDelete); | |||||
| 253 | } | |||||
| 254 | / /</snippet ReassignBu sinessUnit Members1> | |||||
| 255 | } | |||||
| 256 | ||||||
| 257 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 258 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 259 | { | |||||
| 260 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 261 | thro w; | |||||
| 262 | } | |||||
| 263 | } | |||||
| 264 | ||||||
| 265 | pr ivate void PrintLead s() | |||||
| 266 | { | |||||
| 267 | Console. WriteLine( " Fetchin g leads fr om CRM"); | |||||
| 268 | var lead s = from l ead in _co ntext.Lead Set | |||||
| 269 | join u ser in _co ntext.Syst emUserSet | |||||
| 270 | on l ead.Owning User.Id eq uals user. Id | |||||
| 271 | select new | |||||
| 272 | { | |||||
| 273 | Le adName = l ead.FullNa me, | |||||
| 274 | Ow ningUserNa me = user. FullName | |||||
| 275 | }; | |||||
| 276 | foreach (var lead in leads) | |||||
| 277 | Cons ole.WriteL ine( | |||||
| 278 | " Lead {0 } is owned by user { 1}", | |||||
| 279 | lead.LeadN ame, | |||||
| 280 | lead.Ownin gUserName) ; | |||||
| 281 | leads = from lead in _contex t.LeadSet | |||||
| 282 | join team in _contex t.TeamSet | |||||
| 283 | on lead. OwnerId.Id equals te am.Id | |||||
| 284 | select new | |||||
| 285 | { | |||||
| 286 | LeadNa me = lead. FullName, | |||||
| 287 | Owning UserName = team.Name | |||||
| 288 | }; | |||||
| 289 | foreach (var lead in leads) | |||||
| 290 | Cons ole.WriteL ine( | |||||
| 291 | " Lead {0 } is owned by user { 1}", | |||||
| 292 | lead.LeadN ame, | |||||
| 293 | lead.Ownin gUserName) ; | |||||
| 294 | } | |||||
| 295 | ||||||
| 296 | #r egion Publ ic methods | |||||
| 297 | ||||||
| 298 | // / <summary > | |||||
| 299 | // / Creates any entity records t hat this s ample requ ires. | |||||
| 300 | // / </summar y> | |||||
| 301 | pu blic void CreateRequ iredRecord s() | |||||
| 302 | { | |||||
| 303 | #region create use rs | |||||
| 304 | ||||||
| 305 | Console. WriteLine( " Creatin g users"); | |||||
| 306 | var ldap Path = ""; | |||||
| 307 | _users = | |||||
| 308 | Syst emUserProv ider.Retri eveSalespe rsons(_ser viceProxy, ref ldapP ath); | |||||
| 309 | _users.A dd(SystemU serProvide r.Retrieve SystemUser ( | |||||
| 310 | "dpa rker", "Da rren", "Pa rker", "Sa lesperson" , | |||||
| 311 | _ser viceProxy, ref ldapP ath)); | |||||
| 312 | ||||||
| 313 | #endregi on | |||||
| 314 | ||||||
| 315 | #region fetch root business unit | |||||
| 316 | ||||||
| 317 | // Retri eve the ro ot busines s unit to use for cr eating the team for the | |||||
| 318 | // sampl e. | |||||
| 319 | var busi nessUnitQu ery = new QueryExpre ssion | |||||
| 320 | { | |||||
| 321 | Enti tyName = B usinessUni t.EntityLo gicalName, | |||||
| 322 | Colu mnSet = ne w ColumnSe t("busines sunitid"), | |||||
| 323 | Crit eria = new FilterExp ression() | |||||
| 324 | }; | |||||
| 325 | ||||||
| 326 | business UnitQuery. Criteria.A ddConditio n("parentb usinessuni tid", | |||||
| 327 | Cond itionOpera tor.Null); | |||||
| 328 | var busi nessUnitRe sult = _se rviceProxy .RetrieveM ultiple(bu sinessUnit Query); | |||||
| 329 | _rootBus inessUnit = business UnitResult .Entities[ 0].ToEntit y<Business Unit>(); | |||||
| 330 | ||||||
| 331 | #endregi on | |||||
| 332 | ||||||
| 333 | #region create new business unit | |||||
| 334 | ||||||
| 335 | Console. WriteLine( " Creatin g new busi ness unit" ); | |||||
| 336 | _buisnes sUnit = ne w Business Unit() | |||||
| 337 | { | |||||
| 338 | Name = "A Samp le Busines s Unit", | |||||
| 339 | Pare ntBusiness UnitId = _ rootBusine ssUnit.ToE ntityRefer ence() | |||||
| 340 | }; | |||||
| 341 | _buisnes sUnit.Id = _serviceP roxy.Creat e(_buisnes sUnit); | |||||
| 342 | ||||||
| 343 | #endregi on | |||||
| 344 | ||||||
| 345 | #region create tea m | |||||
| 346 | ||||||
| 347 | Console. WriteLine( " Creatin g a user t eam"); | |||||
| 348 | _team = new Team | |||||
| 349 | { | |||||
| 350 | Admi nistratorI d = | |||||
| 351 | new Entity Reference( SystemUser .EntityLog icalName, _users[0]) , | |||||
| 352 | Name = "Sample team", | |||||
| 353 | Busi nessUnitId = _rootBu sinessUnit .ToEntityR eference() | |||||
| 354 | }; | |||||
| 355 | _team.Id = _servic eProxy.Cre ate(_team) ; | |||||
| 356 | ||||||
| 357 | var sale spersonRol e = (from role in _c ontext.Rol eSet | |||||
| 358 | where role.Name == "Sales person" | |||||
| 359 | && ro le.Busines sUnitId.Id == _rootB usinessUni t.Id | |||||
| 360 | selec t role).Fi rst(); | |||||
| 361 | ||||||
| 362 | // assig n role to the team | |||||
| 363 | _service Proxy.Asso ciate( | |||||
| 364 | Team .EntityLog icalName, | |||||
| 365 | _tea m.Id, | |||||
| 366 | new Relationsh ip("teamro les_associ ation"), | |||||
| 367 | new EntityRefe renceColle ction() { | |||||
| 368 | salesperso nRole.ToEn tityRefere nce() | |||||
| 369 | } | |||||
| 370 | ); | |||||
| 371 | ||||||
| 372 | // wait for the as ync job to finish | |||||
| 373 | for (int i = 1; i <= 30; i++ ) | |||||
| 374 | { | |||||
| 375 | Cons ole.WriteL ine(" Che cking to s ee if the async job has finish ed {0}/30" , i); | |||||
| 376 | var teamPrivil eges = (Re trieveTeam Privileges Response) | |||||
| 377 | _servicePr oxy.Execut e(new Retr ieveTeamPr ivilegesRe quest | |||||
| 378 | { | |||||
| 379 | TeamId = _team.I d | |||||
| 380 | }); | |||||
| 381 | if ( teamPrivil eges.RoleP rivileges. Any((rp) = > | |||||
| 382 | rp.Privile geId == | |||||
| 383 | new Gu id("A8ECAC 53-09E8-4A 13-B598-8D 8C87BC3D33 "))) // pr vReadLead | |||||
| 384 | { | |||||
| 385 | break; | |||||
| 386 | } | |||||
| 387 | Syst em.Threadi ng.Thread. Sleep(1000 ); | |||||
| 388 | } | |||||
| 389 | #endregi on | |||||
| 390 | ||||||
| 391 | #region add users to team | |||||
| 392 | ||||||
| 393 | Console. WriteLine( " Adding users to t he team"); | |||||
| 394 | AddMembe rsTeamRequ est addMem bers = new AddMember sTeamReque st() | |||||
| 395 | { | |||||
| 396 | Team Id = _team .Id, | |||||
| 397 | Memb erIds = ne w Guid[] { _users[0] , _users[1 ] } | |||||
| 398 | }; | |||||
| 399 | _service Proxy.Exec ute(addMem bers); | |||||
| 400 | ||||||
| 401 | #endregi on | |||||
| 402 | ||||||
| 403 | #region create lea ds | |||||
| 404 | ||||||
| 405 | Console. WriteLine( " Creatin g leads"); | |||||
| 406 | _leads[0 ] = new Le ad | |||||
| 407 | { | |||||
| 408 | Comp anyName = "A. Datum Corporatio n", | |||||
| 409 | Firs tName = "J oe", | |||||
| 410 | Last Name = "An dreshak", | |||||
| 411 | }; | |||||
| 412 | _leads[0 ].Id = _se rviceProxy .Create(_l eads[0]); | |||||
| 413 | ||||||
| 414 | _leads[1 ] = new Le ad | |||||
| 415 | { | |||||
| 416 | Comp anyName = "Wingtip T oys", | |||||
| 417 | Firs tName = "D iogo", | |||||
| 418 | Last Name = "An drade" | |||||
| 419 | }; | |||||
| 420 | _leads[1 ].Id = _se rviceProxy .Create(_l eads[1]); | |||||
| 421 | ||||||
| 422 | _leads[2 ] = new Le ad | |||||
| 423 | { | |||||
| 424 | Comp anyName = "The Phone Company", | |||||
| 425 | Firs tName = "R onaldo", | |||||
| 426 | Last Name = "Sm ith Jr." | |||||
| 427 | }; | |||||
| 428 | _leads[2 ].Id = _se rviceProxy .Create(_l eads[2]); | |||||
| 429 | ||||||
| 430 | _leads[3 ] = new Le ad | |||||
| 431 | { | |||||
| 432 | Comp anyName = "Tailspin Toys", | |||||
| 433 | Firs tName = "A ndrew", | |||||
| 434 | Last Name = "Su llivan", | |||||
| 435 | }; | |||||
| 436 | _leads[3 ].Id = _se rviceProxy .Create(_l eads[3]); | |||||
| 437 | ||||||
| 438 | #endregi on | |||||
| 439 | ||||||
| 440 | #region assign lea ds | |||||
| 441 | ||||||
| 442 | Console. WriteLine( " Assigni ng leads t o users an d teams"); | |||||
| 443 | _service Proxy.Exec ute(new As signReques t() | |||||
| 444 | { | |||||
| 445 | Assi gnee = new EntityRef erence(Sys temUser.En tityLogica lName, _us ers[0]), | |||||
| 446 | Targ et = _lead s[0].ToEnt ityReferen ce() | |||||
| 447 | }); | |||||
| 448 | ||||||
| 449 | _service Proxy.Exec ute(new As signReques t() | |||||
| 450 | { | |||||
| 451 | Assi gnee = new EntityRef erence(Sys temUser.En tityLogica lName, _us ers[1]), | |||||
| 452 | Targ et = _lead s[1].ToEnt ityReferen ce() | |||||
| 453 | }); | |||||
| 454 | ||||||
| 455 | _service Proxy.Exec ute(new As signReques t() | |||||
| 456 | { | |||||
| 457 | Assi gnee = new EntityRef erence(Sys temUser.En tityLogica lName, _us ers[2]), | |||||
| 458 | Targ et = _lead s[2].ToEnt ityReferen ce() | |||||
| 459 | }); | |||||
| 460 | ||||||
| 461 | // give the team a ccess to t he record so that it can be as signed to it | |||||
| 462 | _service Proxy.Exec ute(new Gr antAccessR equest() | |||||
| 463 | { | |||||
| 464 | Targ et = _lead s[3].ToEnt ityReferen ce(), | |||||
| 465 | Prin cipalAcces s = new Pr incipalAcc ess() | |||||
| 466 | { | |||||
| 467 | AccessMask = AccessR ights.Read Access|Acc essRights. WriteAcces s, | |||||
| 468 | Principal = _team.To EntityRefe rence() | |||||
| 469 | } | |||||
| 470 | }); | |||||
| 471 | ||||||
| 472 | // assig n the lead to the te am | |||||
| 473 | _service Proxy.Exec ute(new As signReques t() | |||||
| 474 | { | |||||
| 475 | Assi gnee = _te am.ToEntit yReference (), | |||||
| 476 | Targ et = _lead s[3].ToEnt ityReferen ce() | |||||
| 477 | }); | |||||
| 478 | ||||||
| 479 | #endregi on | |||||
| 480 | } | |||||
| 481 | ||||||
| 482 | // / <summary > | |||||
| 483 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 484 | // / <param n ame="promp t">Indicat es whether to prompt the user | |||||
| 485 | // / to delet e the reco rds create d in this sample.</p aram> | |||||
| 486 | // / </summar y> | |||||
| 487 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 488 | { | |||||
| 489 | bool toB eDeleted = true; | |||||
| 490 | ||||||
| 491 | if (prom pt) | |||||
| 492 | { | |||||
| 493 | // A sk the use r if the c reated ent ities shou ld be dele ted. | |||||
| 494 | Cons ole.Write( "\nDo you want these entity re cords dele ted? (y/n) [y]: "); | |||||
| 495 | Stri ng answer = Console. ReadLine() ; | |||||
| 496 | if ( answer.Sta rtsWith("y ") || | |||||
| 497 | answer.Sta rtsWith("Y ") || | |||||
| 498 | answer == String.Emp ty) | |||||
| 499 | { | |||||
| 500 | toBeDelete d = true; | |||||
| 501 | } | |||||
| 502 | else | |||||
| 503 | { | |||||
| 504 | toBeDelete d = false; | |||||
| 505 | } | |||||
| 506 | } | |||||
| 507 | ||||||
| 508 | if (toBe Deleted) | |||||
| 509 | { | |||||
| 510 | // D elete the leads | |||||
| 511 | fore ach (Lead lead in _l eads) | |||||
| 512 | _servicePr oxy.Delete (Lead.Enti tyLogicalN ame, lead. Id); | |||||
| 513 | Cons ole.WriteL ine("Entit y records have been deleted.") ; | |||||
| 514 | } | |||||
| 515 | } | |||||
| 516 | #e ndregion P ublic Meth ods | |||||
| 517 | ||||||
| 518 | #e ndregion H ow To Samp le Code | |||||
| 519 | ||||||
| 520 | #r egion Main method | |||||
| 521 | ||||||
| 522 | // / <summary > | |||||
| 523 | // / Standard Main() me thod used by most SD K samples. | |||||
| 524 | // / </summar y> | |||||
| 525 | // / <param n ame="args" ></param> | |||||
| 526 | st atic publi c void Mai n(string[] args) | |||||
| 527 | { | |||||
| 528 | try | |||||
| 529 | { | |||||
| 530 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 531 | // c redentials from the user. | |||||
| 532 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 533 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 534 | ||||||
| 535 | Reas signBusine ssUnitMemb ers app = new Reassi gnBusiness UnitMember s(); | |||||
| 536 | app. Run(config , true); | |||||
| 537 | } | |||||
| 538 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 539 | { | |||||
| 540 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 541 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 542 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 543 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 544 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 545 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 546 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 547 | } | |||||
| 548 | catch (S ystem.Time outExcepti on ex) | |||||
| 549 | { | |||||
| 550 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 551 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 552 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 553 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 554 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 555 | } | |||||
| 556 | catch (S ystem.Exce ption ex) | |||||
| 557 | { | |||||
| 558 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 559 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 560 | ||||||
| 561 | // D isplay the details o f the inne r exceptio n. | |||||
| 562 | if ( ex.InnerEx ception != null) | |||||
| 563 | { | |||||
| 564 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 565 | ||||||
| 566 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 567 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 568 | if (fe != null) | |||||
| 569 | { | |||||
| 570 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 571 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 572 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 573 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 574 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 575 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 576 | } | |||||
| 577 | } | |||||
| 578 | } | |||||
| 579 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 580 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 581 | ||||||
| 582 | finally | |||||
| 583 | { | |||||
| 584 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 585 | Cons ole.ReadLi ne(); | |||||
| 586 | } | |||||
| 587 | } | |||||
| 588 | ||||||
| 589 | #e ndregion M ain method | |||||
| 590 | } | |||||
| 591 | } | |||||
| 592 | //</snippe tReassignB usinessUni tMembers> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.