Produced by Araxis Merge on 2/1/2017 2:56:28 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\BusinessDataModel\UsersAndRoles | AddRoleToUser.cs | Tue Dec 20 19:51:44 2016 UTC |
| 2 | Wed Feb 1 19:56:28 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 240 |
| 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 AddRoleToU ser> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Service Model; | |||||
| 19 | using Syst em.Service Model.Desc ription; | |||||
| 20 | ||||||
| 21 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 22 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 23 | using Micr osoft.Xrm. Sdk; | |||||
| 24 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 25 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 26 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 27 | ||||||
| 28 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 29 | { | |||||
| 30 | /// <s ummary> | |||||
| 31 | /// De monstrates how to do basic rol e associat ion with t he system user. | |||||
| 32 | /// </ summary> | |||||
| 33 | /// <r emarks> | |||||
| 34 | /// At run-time, you will be given t he option to revert the role | |||||
| 35 | /// as sociation created by this prog ram.</rema rks> | |||||
| 36 | public class Add RoleToUser | |||||
| 37 | { | |||||
| 38 | #r egion Clas s Level Me mbers | |||||
| 39 | ||||||
| 40 | // Define th e IDs need ed for thi s sample. | |||||
| 41 | pr ivate Guid _userId; | |||||
| 42 | pr ivate Guid _roleId; | |||||
| 43 | pr ivate Stri ng _givenR ole = "sal esperson"; | |||||
| 44 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 45 | ||||||
| 46 | #e ndregion C lass Level Members | |||||
| 47 | ||||||
| 48 | #r egion How To Sample Code | |||||
| 49 | // / <summary > | |||||
| 50 | // / This met hod first connects t o the Orga nization s ervice. Af terwards, | |||||
| 51 | // / creates/ retrieves a system u ser, | |||||
| 52 | // / updates the system user to a ssociate w ith the sa lesperson role. | |||||
| 53 | // / Note: Cr eating a u ser is onl y supporte d | |||||
| 54 | // / in an on -premises/ active dir ectory env ironment. | |||||
| 55 | // / </summar y> | |||||
| 56 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 57 | // / <param n ame="promp tforDelete ">When Tru e, the use r is promp ted to del ete all | |||||
| 58 | // / created entities.< /param> | |||||
| 59 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt forDelete) | |||||
| 60 | { | |||||
| 61 | try | |||||
| 62 | { | |||||
| 63 | //<s nippetAddR oleToUser1 > | |||||
| 64 | // C onnect to the Organi zation ser vice. | |||||
| 65 | // T he using s tatement a ssures tha t the serv ice proxy is properl y disposed . | |||||
| 66 | usin g (_servic eProxy = n ew Organiz ationServi ceProxy(se rverConfig .Organizat ionUri, se rverConfig .HomeRealm Uri, | |||||
| 67 | s erverConfi g.Credenti als, serve rConfig.De viceCreden tials)) | |||||
| 68 | { | |||||
| 69 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 70 | ||||||
| 71 | CreateRequ iredRecord s(); | |||||
| 72 | ||||||
| 73 | // Find th e role. | |||||
| 74 | QueryExpre ssion quer y = new Qu eryExpress ion | |||||
| 75 | { | |||||
| 76 | Entity Name = Rol e.EntityLo gicalName, | |||||
| 77 | Column Set = new ColumnSet( "roleid"), | |||||
| 78 | Criter ia = new F ilterExpre ssion | |||||
| 79 | { | |||||
| 80 | Co nditions = | |||||
| 81 | { | |||||
| 82 | ||||||
| 83 | new ConditionE xpression | |||||
| 84 | { | |||||
| 85 | AttributeN ame = "nam e", | |||||
| 86 | Operator = Condition Operator.E qual, | |||||
| 87 | Values = { _givenRole } | |||||
| 88 | } | |||||
| 89 | } | |||||
| 90 | } | |||||
| 91 | }; | |||||
| 92 | ||||||
| 93 | // Get the role. | |||||
| 94 | EntityColl ection rol es = _serv iceProxy.R etrieveMul tiple(quer y); | |||||
| 95 | if (roles. Entities.C ount > 0) | |||||
| 96 | { | |||||
| 97 | Role s alesRole = _serviceP roxy.Retri eveMultipl e(query).E ntities[0] .ToEntity< Role>(); | |||||
| 98 | ||||||
| 99 | Consol e.WriteLin e("Role {0 } is retri eved for t he role as signment." , _givenRo le); | |||||
| 100 | ||||||
| 101 | _roleI d = salesR ole.Id; | |||||
| 102 | ||||||
| 103 | // Ass ociate the user with the role. | |||||
| 104 | if (_r oleId != G uid.Empty && _userId != Guid.E mpty) | |||||
| 105 | { | |||||
| 106 | _s erviceProx y.Associat e( | |||||
| 107 | "systemuse r", | |||||
| 108 | _userId, | |||||
| 109 | new Relati onship("sy stemuserro les_associ ation"), | |||||
| 110 | new Entity ReferenceC ollection( ) { new En tityRefere nce(Role.E ntityLogic alName, _r oleId) }); | |||||
| 111 | ||||||
| 112 | Co nsole.Writ eLine("Rol e is assoc iated with the user. "); | |||||
| 113 | } | |||||
| 114 | } | |||||
| 115 | ||||||
| 116 | DeleteRequ iredRecord s(promptfo rDelete); | |||||
| 117 | } | |||||
| 118 | //</ snippetAdd RoleToUser 1> | |||||
| 119 | } | |||||
| 120 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 121 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 122 | { | |||||
| 123 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 124 | thro w; | |||||
| 125 | } | |||||
| 126 | } | |||||
| 127 | ||||||
| 128 | // / <summary > | |||||
| 129 | // / Creates any entity records t hat this s ample requ ires. | |||||
| 130 | // / </summar y> | |||||
| 131 | pu blic void CreateRequ iredRecord s() | |||||
| 132 | { | |||||
| 133 | // For t his sample , all requ ired entit ies are cr eated in t he Run() m ethod. | |||||
| 134 | // Creat e/retrieve a user fo r role ass ignment. | |||||
| 135 | _userId = SystemUs erProvider .RetrieveA UserWithou tAnyRoleAs signed(_se rviceProxy ); | |||||
| 136 | ||||||
| 137 | if (_use rId != Gui d.Empty) | |||||
| 138 | Cons ole.WriteL ine("{0} u ser retrie ved.", _us erId); | |||||
| 139 | } | |||||
| 140 | ||||||
| 141 | // / <summary > | |||||
| 142 | // / Deletes/ reverts an y entity r ecords tha t were cre ated for t his sample . | |||||
| 143 | // / <param n ame="promp t">Indicat es whether to prompt the user | |||||
| 144 | // / to delet e/revert t he records created i n this sam ple.</para m> | |||||
| 145 | // / </summar y> | |||||
| 146 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 147 | { | |||||
| 148 | bool del eteRecords = true; | |||||
| 149 | ||||||
| 150 | if (prom pt) | |||||
| 151 | { | |||||
| 152 | Cons ole.Write( "\nDo you want these entity re cords dele ted/revert ed? (y/n) [y]: "); | |||||
| 153 | Stri ng answer = Console. ReadLine() ; | |||||
| 154 | ||||||
| 155 | dele teRecords = (answer. StartsWith ("y") || a nswer.Star tsWith("Y" ) || answe r == Strin g.Empty); | |||||
| 156 | } | |||||
| 157 | ||||||
| 158 | if (dele teRecords) | |||||
| 159 | { | |||||
| 160 | _ser viceProxy. Disassocia te("system user", | |||||
| 161 | _userId, | |||||
| 162 | new Relati onship("sy stemuserro les_associ ation"), | |||||
| 163 | new Entity ReferenceC ollection( ) { new En tityRefere nce("role" , _roleId) }); | |||||
| 164 | Cons ole.WriteL ine("Entit y records have been deleted/re verted."); | |||||
| 165 | } | |||||
| 166 | } | |||||
| 167 | ||||||
| 168 | #e ndregion H ow To Samp le Code | |||||
| 169 | ||||||
| 170 | #r egion Main method | |||||
| 171 | ||||||
| 172 | // / <summary > | |||||
| 173 | // / Standard Main() me thod used by most SD K samples. | |||||
| 174 | // / </summar y> | |||||
| 175 | // / <param n ame="args" ></param> | |||||
| 176 | st atic publi c void Mai n(string[] args) | |||||
| 177 | { | |||||
| 178 | try | |||||
| 179 | { | |||||
| 180 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 181 | // c redentials from the user. | |||||
| 182 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 183 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 184 | ||||||
| 185 | AddR oleToUser app = new AddRoleToU ser(); | |||||
| 186 | app. Run( confi g, true ); | |||||
| 187 | } | |||||
| 188 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 189 | { | |||||
| 190 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 191 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 192 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 193 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 194 | Cons ole.WriteL ine("Trace : {0}", ex .Detail.Tr aceText); | |||||
| 195 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 196 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 197 | } | |||||
| 198 | catch (S ystem.Time outExcepti on ex) | |||||
| 199 | { | |||||
| 200 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 201 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 202 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 203 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 204 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 205 | } | |||||
| 206 | catch (S ystem.Exce ption ex) | |||||
| 207 | { | |||||
| 208 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 209 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 210 | ||||||
| 211 | // D isplay the details o f the inne r exceptio n. | |||||
| 212 | if ( ex.InnerEx ception != null) | |||||
| 213 | { | |||||
| 214 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 215 | ||||||
| 216 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 217 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 218 | if (fe != null) | |||||
| 219 | { | |||||
| 220 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 221 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 222 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 223 | Consol e.WriteLin e("Trace: {0}", fe.D etail.Trac eText); | |||||
| 224 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 225 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 226 | } | |||||
| 227 | } | |||||
| 228 | } | |||||
| 229 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 230 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 231 | finally | |||||
| 232 | { | |||||
| 233 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 234 | Cons ole.ReadLi ne(); | |||||
| 235 | } | |||||
| 236 | } | |||||
| 237 | #e ndregion M ain method | |||||
| 238 | } | |||||
| 239 | } | |||||
| 240 | //</snippe tAddRoleTo User> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.