Produced by Araxis Merge on 2/1/2017 2:56:47 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\Metadata\MetadataQuery | MetadataQuerySample.cs | Tue Dec 20 19:51:47 2016 UTC |
| 2 | Wed Feb 1 19:56:47 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 641 |
| 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 | //<snippet MetadataQu ery> | |||||
| 16 | using Syst em; | |||||
| 17 | using Syst em.Service Model; | |||||
| 18 | using Syst em.Service Model.Desc ription; | |||||
| 19 | using Syst em.IO; | |||||
| 20 | using Syst em.Collect ions.Gener ic; | |||||
| 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 | using Micr osoft.Xrm. Sdk.Messag es; | |||||
| 28 | using Micr osoft.Xrm. Sdk.Metada ta; | |||||
| 29 | using Micr osoft.Xrm. Sdk.Metada ta.Query; | |||||
| 30 | ||||||
| 31 | ||||||
| 32 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 33 | { | |||||
| 34 | public cl ass Metada taQuerySam ple | |||||
| 35 | { | |||||
| 36 | #region Class Leve l Members | |||||
| 37 | ||||||
| 38 | private Organizati onServiceP roxy _serv iceProxy; | |||||
| 39 | private IOrganizat ionService _service; | |||||
| 40 | private List<Optio nSetOption > _optionL abelList = new List< OptionSetO ption>(); | |||||
| 41 | private String _cu stomEntity SchemaName = "sample _SampleEnt ityForMeta dataQuery" ; | |||||
| 42 | private String _cu stomAttrib uteSchemaN ame = "sam ple_Exampl eOptionSet "; | |||||
| 43 | //<snipp etLabelQue ryExpressi on0> | |||||
| 44 | private Guid _user Id; | |||||
| 45 | private int _langu ageCode; | |||||
| 46 | //</snip petLabelQu eryExpress ion0> | |||||
| 47 | ||||||
| 48 | ||||||
| 49 | ||||||
| 50 | #endregi on Class L evel Membe rs | |||||
| 51 | ||||||
| 52 | ||||||
| 53 | /// <sum mary> | |||||
| 54 | /// This method co nnects to the Organi zation _se rvice. | |||||
| 55 | /// </su mmary> | |||||
| 56 | /// <par am name="s erverConfi g">Contain s server c onnection informatio n.</param> | |||||
| 57 | ||||||
| 58 | public v oid Run(Se rverConnec tion.Confi guration s erverConfi g) | |||||
| 59 | { | |||||
| 60 | try | |||||
| 61 | { | |||||
| 62 | ||||||
| 63 | // Con nect to th e Organiza tion _serv ice. | |||||
| 64 | // The using sta tement ass ures that the _servi ce proxy w ill be pro perly disp osed. | |||||
| 65 | usi ng (_servi ceProxy = new Organi zationServ iceProxy(s erverConfi g.Organiza tionUri, s erverConfi g.HomeReal mUri, serv erConfig.C redentials , serverCo nfig.Devic eCredentia ls)) | |||||
| 66 | { | |||||
| 67 | // Th is stateme nt is requ ired to en able early -bound typ e support. | |||||
| 68 | _serv iceProxy.E nableProxy Types(); | |||||
| 69 | ||||||
| 70 | _serv ice = (IOr ganization Service)_s erviceProx y; | |||||
| 71 | //<sn ippetLabel QueryExpre ssion1> | |||||
| 72 | _user Id = ((Who AmIRespons e)_service .Execute(n ew WhoAmIR equest())) .UserId; | |||||
| 73 | _lang uageCode = RetrieveU serUILangu ageCode(_u serId); | |||||
| 74 | //</s nippetLabe lQueryExpr ession1> | |||||
| 75 | ||||||
| 76 | ||||||
| 77 | ||||||
| 78 | //<sn ippetEntit yFilter> | |||||
| 79 | ||||||
| 80 | // An array Sch emaName va lues for n on-interse ct, user-o wned entit ies that s hould not be returne d. | |||||
| 81 | Strin g[] exclud edEntities = { | |||||
| 82 | "WorkflowL og", | |||||
| 83 | "Template" , | |||||
| 84 | "CustomerO pportunity Role", | |||||
| 85 | "Import", | |||||
| 86 | "UserQuery Visualizat ion", | |||||
| 87 | "UserEntit yInstanceD ata", | |||||
| 88 | "ImportLog ", | |||||
| 89 | "Recurrenc eRule", | |||||
| 90 | "QuoteClos e", | |||||
| 91 | "UserForm" , | |||||
| 92 | "SharePoin tDocumentL ocation", | |||||
| 93 | "Queue", | |||||
| 94 | "Duplicate Rule", | |||||
| 95 | "Opportuni tyClose", | |||||
| 96 | "Workflow" , | |||||
| 97 | "Recurring Appointmen tMaster", | |||||
| 98 | "CustomerR elationshi p", | |||||
| 99 | "Annotatio n", | |||||
| 100 | "SharePoin tSite", | |||||
| 101 | "ImportDat a", | |||||
| 102 | "ImportFil e", | |||||
| 103 | "OrderClos e", | |||||
| 104 | "Contract" , | |||||
| 105 | "BulkOpera tion", | |||||
| 106 | "CampaignR esponse", | |||||
| 107 | "Connectio n", | |||||
| 108 | "Report", | |||||
| 109 | "CampaignA ctivity", | |||||
| 110 | "UserEntit yUISetting s", | |||||
| 111 | "IncidentR esolution" , | |||||
| 112 | "GoalRollu pQuery", | |||||
| 113 | "MailMerge Template", | |||||
| 114 | "Campaign" , | |||||
| 115 | "PostFollo w", | |||||
| 116 | "ImportMap ", | |||||
| 117 | "Goal", | |||||
| 118 | "AsyncOper ation", | |||||
| 119 | "ProcessSe ssion", | |||||
| 120 | "UserQuery ", | |||||
| 121 | "ActivityP ointer", | |||||
| 122 | "List", | |||||
| 123 | "ServiceAp pointment" }; | |||||
| 124 | ||||||
| 125 | //A f ilter expr ession to limit enti ties retur ned to non -intersect , user-own ed entitie s not foun d in the l ist of exc luded enti ties. | |||||
| 126 | Metad ataFilterE xpression EntityFilt er = new M etadataFil terExpress ion(Logica lOperator. And); | |||||
| 127 | Entit yFilter.Co nditions.A dd(new Met adataCondi tionExpres sion("IsIn tersect", MetadataCo nditionOpe rator.Equa ls, false) ); | |||||
| 128 | Entit yFilter.Co nditions.A dd(new Met adataCondi tionExpres sion("Owne rshipType" , Metadata ConditionO perator.Eq uals, Owne rshipTypes .UserOwned )); | |||||
| 129 | Entit yFilter.Co nditions.A dd(new Met adataCondi tionExpres sion("Sche maName", M etadataCon ditionOper ator.NotIn , excluded Entities)) ; | |||||
| 130 | Metad ataConditi onExpressi on isVisib ileInMobil eTrue = ne w Metadata ConditionE xpression( "IsVisible InMobile", MetadataC onditionOp erator.Equ als, true) ; | |||||
| 131 | Entit yFilter.Co nditions.A dd(isVisib ileInMobil eTrue); | |||||
| 132 | ||||||
| 133 | ||||||
| 134 | ||||||
| 135 | //</s nippetEnti tyFilter> | |||||
| 136 | //<sn ippetEntit yPropertie s> | |||||
| 137 | //A p roperties expression to limit the proper ties to be included with entit ies | |||||
| 138 | Metad ataPropert iesExpress ion Entity Properties = new Met adataPrope rtiesExpre ssion() | |||||
| 139 | { | |||||
| 140 | AllP roperties = false | |||||
| 141 | }; | |||||
| 142 | Entit yPropertie s.Property Names.AddR ange(new s tring[] { "Attribute s" }); | |||||
| 143 | //</s nippetEnti tyProperti es> | |||||
| 144 | ||||||
| 145 | //<sn ippetAttri buteQueryE xpression> | |||||
| 146 | //A c ondition e xpresson t o return o ptionset a ttributes | |||||
| 147 | Metad ataConditi onExpressi on[] optio nsetAttrib uteTypes = new Metad ataConditi onExpressi on[] { | |||||
| 148 | new M etadataCon ditionExpr ession("At tributeTyp e", Metada taConditio nOperator. Equals, At tributeTyp eCode.Pick list), | |||||
| 149 | new M etadataCon ditionExpr ession("At tributeTyp e", Metada taConditio nOperator. Equals, At tributeTyp eCode.Stat e), | |||||
| 150 | new M etadataCon ditionExpr ession("At tributeTyp e", Metada taConditio nOperator. Equals, At tributeTyp eCode.Stat us), | |||||
| 151 | new M etadataCon ditionExpr ession("At tributeTyp e", Metada taConditio nOperator. Equals, At tributeTyp eCode.Bool ean) | |||||
| 152 | }; | |||||
| 153 | ||||||
| 154 | //A f ilter expr ession to apply the optionsetA ttributeTy pes condit ion expres sion | |||||
| 155 | Metad ataFilterE xpression AttributeF ilter = ne w Metadata FilterExpr ession(Log icalOperat or.Or); | |||||
| 156 | Attri buteFilter .Condition s.AddRange (optionset AttributeT ypes); | |||||
| 157 | ||||||
| 158 | //A P roperties expression to limit the proper ties to be included with attri butes | |||||
| 159 | Metad ataPropert iesExpress ion Attrib utePropert ies = new MetadataPr opertiesEx pression() { AllProp erties = f alse }; | |||||
| 160 | Attri buteProper ties.Prope rtyNames.A dd("Option Set"); | |||||
| 161 | Attri buteProper ties.Prope rtyNames.A dd("Attrib uteType"); | |||||
| 162 | ||||||
| 163 | //</s nippetAttr ibuteQuery Expression > | |||||
| 164 | ||||||
| 165 | //<sn ippetLabel QueryExpre ssion3> | |||||
| 166 | ||||||
| 167 | //A l abel query expressio n to limit the label s returned to only t hose for t he user's preferred language | |||||
| 168 | Label QueryExpre ssion labe lQuery = n ew LabelQu eryExpress ion(); | |||||
| 169 | label Query.Filt erLanguage s.Add(_lan guageCode) ; | |||||
| 170 | ||||||
| 171 | //</s nippetLabe lQueryExpr ession3> | |||||
| 172 | ||||||
| 173 | //<sn ippetIniti alRequest> | |||||
| 174 | //An entity que ry express ion to com bine the f ilter expr essions an d property expressio ns for the query. | |||||
| 175 | Entit yQueryExpr ession ent ityQueryEx pression = new Entit yQueryExpr ession() | |||||
| 176 | { | |||||
| 177 | ||||||
| 178 | Crit eria = Ent ityFilter, | |||||
| 179 | Prop erties = E ntityPrope rties, | |||||
| 180 | Attr ibuteQuery = new Att ributeQuer yExpressio n() | |||||
| 181 | { | |||||
| 182 | Cri teria = At tributeFil ter, | |||||
| 183 | Pro perties = AttributeP roperties | |||||
| 184 | }, | |||||
| 185 | Labe lQuery = l abelQuery | |||||
| 186 | ||||||
| 187 | }; | |||||
| 188 | ||||||
| 189 | //Ret rieve the metadata f or the que ry without a ClientV ersionStam p | |||||
| 190 | Retri eveMetadat aChangesRe sponse ini tialReques t = getMet adataChang es(entityQ ueryExpres sion, null , DeletedM etadataFil ters.Optio nSet); | |||||
| 191 | //</s nippetInit ialRequest > | |||||
| 192 | ||||||
| 193 | //Add option la bels to th e cache an d display the change s | |||||
| 194 | addOp tionLabels ToCache(in itialReque st.EntityM etadata, f alse); | |||||
| 195 | Strin g ClientVe rsionStamp = initial Request.Se rverVersio nStamp; | |||||
| 196 | Conso le.WriteLi ne("{0} op tion label s for {1} entities a dded to th e cache.", | |||||
| 197 | _opt ionLabelLi st.Count, | |||||
| 198 | init ialRequest .EntityMet adata.Coun t); | |||||
| 199 | Conso le.WriteLi ne(""); | |||||
| 200 | Conso le.WriteLi ne("Client VersionSta mp: {0}", ClientVers ionStamp); | |||||
| 201 | Conso le.WriteLi ne(""); | |||||
| 202 | ||||||
| 203 | ||||||
| 204 | //Add new custo m entity w ith option set | |||||
| 205 | Conso le.WriteLi ne("Adding a custom entity nam ed {0} wit h a custom optionset attribute named : { 1}", | |||||
| 206 | _cus tomEntityS chemaName, _customAt tributeSch emaName); | |||||
| 207 | Conso le.WriteLi ne(""); | |||||
| 208 | addCu stomEntity WithOption Set(); | |||||
| 209 | //Pub lishing is n't necess ary when a dding a cu stom entit y | |||||
| 210 | ||||||
| 211 | //Add new optio n labels t o the cach e and disp lay the re sults | |||||
| 212 | Clien tVersionSt amp = upda teOptionLa belList(en tityQueryE xpression, ClientVer sionStamp) ; | |||||
| 213 | ||||||
| 214 | Conso le.WriteLi ne("Client VersionSta mp: {0}", ClientVers ionStamp); | |||||
| 215 | Conso le.WriteLi ne(""); | |||||
| 216 | ||||||
| 217 | //Add a new opt ion to the custom op tionset in the custo m entity a nd publish the custo m entity | |||||
| 218 | Conso le.WriteLi ne("Adding an additi onal optio n to the { 0} attribu te options .", | |||||
| 219 | _cus tomAttribu teSchemaNa me); | |||||
| 220 | Conso le.WriteLi ne(""); | |||||
| 221 | addOp tionToCust omEntityOp tionSet(); | |||||
| 222 | //It is necessa ry to publ ish update s to metad ata. Creat e and Dele te operati ons are pu blished au tomaticall y. | |||||
| 223 | publi shUpdatedE ntity(); | |||||
| 224 | ||||||
| 225 | ||||||
| 226 | ||||||
| 227 | //Add the new o ption labe l to the c ache and d isplay the results | |||||
| 228 | Clien tVersionSt amp = upda teOptionLa belList(en tityQueryE xpression, ClientVer sionStamp) ; | |||||
| 229 | ||||||
| 230 | Conso le.WriteLi ne("Client VersionSta mp: {0}", ClientVers ionStamp); | |||||
| 231 | Conso le.WriteLi ne(""); | |||||
| 232 | ||||||
| 233 | Conso le.WriteLi ne(""); | |||||
| 234 | Conso le.WriteLi ne("Curren t Options: {0}", _op tionLabelL ist.Count. ToString() ); | |||||
| 235 | Conso le.WriteLi ne(""); | |||||
| 236 | ||||||
| 237 | //Del ete the cu stom entit y | |||||
| 238 | Conso le.WriteLi ne(""); | |||||
| 239 | Conso le.WriteLi ne("Deleti ng the {0} custom en tity", | |||||
| 240 | _cus tomEntityS chemaName) ; | |||||
| 241 | Conso le.WriteLi ne(""); | |||||
| 242 | delet eCustomEnt ityWithOpt ionSet(); | |||||
| 243 | //Pub lishing is n't necess ary when d eleting a custom ent ity | |||||
| 244 | ||||||
| 245 | ||||||
| 246 | //Ret rieve meta data chang es to remo ve option labels fro m deleted attributes and displ ay the res ults | |||||
| 247 | Clien tVersionSt amp = upda teOptionLa belList(en tityQueryE xpression, ClientVer sionStamp) ; | |||||
| 248 | ||||||
| 249 | Conso le.WriteLi ne("Client VersionSta mp: {0}", ClientVers ionStamp); | |||||
| 250 | Conso le.WriteLi ne(""); | |||||
| 251 | ||||||
| 252 | } | |||||
| 253 | ||||||
| 254 | } | |||||
| 255 | ||||||
| 256 | // Catc h any _ser vice fault exception s that Mic rosoft Dyn amics CRM throws. | |||||
| 257 | catch ( FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault>) | |||||
| 258 | { | |||||
| 259 | // You can handl e an excep tion here or pass it back to t he calling method. | |||||
| 260 | throw; | |||||
| 261 | } | |||||
| 262 | } | |||||
| 263 | ||||||
| 264 | //<snipp etgetMetad ataChanges > | |||||
| 265 | protecte d Retrieve MetadataCh angesRespo nse getMet adataChang es( | |||||
| 266 | EntityQ ueryExpres sion entit yQueryExpr ession, | |||||
| 267 | String clientVers ionStamp, | |||||
| 268 | Deleted MetadataFi lters dele tedMetadat aFilter) | |||||
| 269 | { | |||||
| 270 | Retriev eMetadataC hangesRequ est retrie veMetadata ChangesReq uest = new RetrieveM etadataCha ngesReques t() | |||||
| 271 | { | |||||
| 272 | Query = entityQu eryExpress ion, | |||||
| 273 | Client VersionSta mp = clien tVersionSt amp, | |||||
| 274 | Delete dMetadataF ilters = d eletedMeta dataFilter | |||||
| 275 | }; | |||||
| 276 | ||||||
| 277 | return (RetrieveM etadataCha ngesRespon se)_servic e.Execute( retrieveMe tadataChan gesRequest ); | |||||
| 278 | ||||||
| 279 | } | |||||
| 280 | //</snip petgetMeta dataChange s> | |||||
| 281 | ||||||
| 282 | //<snipp etupdateOp tionLabelL ist> | |||||
| 283 | protecte d String u pdateOptio nLabelList (EntityQue ryExpressi on entityQ ueryExpres sion, Stri ng clientV ersionStam p) | |||||
| 284 | { | |||||
| 285 | //Retri eve metada ta changes and add t hem to the cache | |||||
| 286 | Retriev eMetadataC hangesResp onse updat eResponse; | |||||
| 287 | try | |||||
| 288 | { | |||||
| 289 | update Response = getMetada taChanges( entityQuer yExpressio n, clientV ersionStam p, Deleted MetadataFi lters.Opti onSet); | |||||
| 290 | addOpt ionLabelsT oCache(upd ateRespons e.EntityMe tadata, tr ue); | |||||
| 291 | remove OptionLabe lsFromCach e(updateRe sponse.Del etedMetada ta, true); | |||||
| 292 | ||||||
| 293 | } | |||||
| 294 | catch ( FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> e x) | |||||
| 295 | { | |||||
| 296 | // Che ck for Err orCodes.Ex piredVersi onStamp (0 x80044352) | |||||
| 297 | // Wil l occur wh en the tim estamp exc eeds the O rganizatio n.ExpireSu bscription sInDays va lue, which is 90 by default. | |||||
| 298 | if (ex .Detail.Er rorCode == unchecked ((int)0x80 044352)) | |||||
| 299 | { | |||||
| 300 | //rei nitialize cache | |||||
| 301 | _opti onLabelLis t.Clear(); | |||||
| 302 | ||||||
| 303 | updat eResponse = getMetad ataChanges (entityQue ryExpressi on, null, DeletedMet adataFilte rs.OptionS et); | |||||
| 304 | //Add them to t he cache a nd display the chang es | |||||
| 305 | addOp tionLabels ToCache(up dateRespon se.EntityM etadata, t rue); | |||||
| 306 | ||||||
| 307 | } | |||||
| 308 | else | |||||
| 309 | { | |||||
| 310 | throw ex; | |||||
| 311 | } | |||||
| 312 | ||||||
| 313 | } | |||||
| 314 | return updateResp onse.Serve rVersionSt amp; | |||||
| 315 | } | |||||
| 316 | //</snip petupdateO ptionLabel List> | |||||
| 317 | //<snipp etaddOptio nLabelsToC ache> | |||||
| 318 | protecte d void add OptionLabe lsToCache( EntityMeta dataCollec tion entit yMetadataC ollection, Boolean s howChanges ) | |||||
| 319 | { | |||||
| 320 | ||||||
| 321 | List<Op tionSetOpt ion> chang es = new L ist<Option SetOption> (); | |||||
| 322 | ||||||
| 323 | foreach (EntityMe tadata em in entityM etadataCol lection) | |||||
| 324 | { | |||||
| 325 | foreac h (Attribu teMetadata am in em. Attributes ) | |||||
| 326 | { | |||||
| 327 | switc h (am.Attr ibuteType) | |||||
| 328 | { | |||||
| 329 | case Attribute TypeCode.B oolean: | |||||
| 330 | Boo leanAttrib uteMetadat a booleanA ttribute = (BooleanA ttributeMe tadata)am; | |||||
| 331 | //L abels will not be in cluded if they aren' t new | |||||
| 332 | if (booleanAt tribute.Op tionSet.Fa lseOption. Label.User LocalizedL abel != nu ll) | |||||
| 333 | { | |||||
| 334 | ch anges.Add( new Option SetOption( | |||||
| 335 | (G uid)boolea nAttribute .OptionSet .MetadataI d, | |||||
| 336 | 0, | |||||
| 337 | bo oleanAttri bute.Optio nSet.False Option.Lab el.UserLoc alizedLabe l.Label) | |||||
| 338 | ); | |||||
| 339 | } | |||||
| 340 | //L abels will not be in cluded if they aren' t new | |||||
| 341 | if (booleanAt tribute.Op tionSet.Tr ueOption.L abel.UserL ocalizedLa bel != nul l) | |||||
| 342 | { | |||||
| 343 | ch anges.Add( new Option SetOption( | |||||
| 344 | (G uid)boolea nAttribute .OptionSet .MetadataI d, | |||||
| 345 | 1, | |||||
| 346 | bo oleanAttri bute.Optio nSet.TrueO ption.Labe l.UserLoca lizedLabel .Label)); | |||||
| 347 | } | |||||
| 348 | bre ak; | |||||
| 349 | defa ult: | |||||
| 350 | Enu mAttribute Metadata o ptionsetAt tribute = (EnumAttri buteMetada ta)am; | |||||
| 351 | for each (Opti onMetadata option in optionset Attribute. OptionSet. Options) | |||||
| 352 | { | |||||
| 353 | // Labels wil l not be i ncluded if they aren 't new | |||||
| 354 | if (option.L abel.UserL ocalizedLa bel != nul l) | |||||
| 355 | { | |||||
| 356 | c hanges.Add (new Optio nSetOption ( | |||||
| 357 | (Guid)opti onsetAttri bute.Optio nSet.Metad ataId, | |||||
| 358 | ( int)option .Value, | |||||
| 359 | o ption.Labe l.UserLoca lizedLabel .Label)); | |||||
| 360 | } | |||||
| 361 | } | |||||
| 362 | bre ak; | |||||
| 363 | } | |||||
| 364 | } | |||||
| 365 | } | |||||
| 366 | ||||||
| 367 | _option LabelList. AddRange(c hanges); | |||||
| 368 | ||||||
| 369 | if (sho wChanges) | |||||
| 370 | { | |||||
| 371 | ||||||
| 372 | if (ch anges.Coun t > 0) | |||||
| 373 | { | |||||
| 374 | Conso le.WriteLi ne("{0} op tion label s for {1} entities w ere added to the cac he.", chan ges.Count, entityMet adataColle ction.Coun t); | |||||
| 375 | Conso le.WriteLi ne("{0} Op tion Label s cached", _optionLa belList.Co unt); | |||||
| 376 | } | |||||
| 377 | else | |||||
| 378 | { Cons ole.WriteL ine("No op tion label s were add ed to the cache."); } | |||||
| 379 | ||||||
| 380 | } | |||||
| 381 | } | |||||
| 382 | //</snip petaddOpti onLabelsTo Cache> | |||||
| 383 | //<snipp etremoveOp tionLabels FromCache> | |||||
| 384 | protecte d void rem oveOptionL abelsFromC ache(Delet edMetadata Collection DeletedMe tadata, Bo olean show Changes) | |||||
| 385 | { | |||||
| 386 | List<Op tionSetOpt ion> optio nSetOption sToRemove = new List <OptionSet Option>(); | |||||
| 387 | ||||||
| 388 | if (Del etedMetada ta.Keys.Co ntains(Del etedMetada taFilters. OptionSet) ) | |||||
| 389 | { | |||||
| 390 | DataCo llection<G uid> optio nsetmetada taids = (D ataCollect ion<Guid>) DeletedMet adata[Dele tedMetadat aFilters.O ptionSet]; | |||||
| 391 | foreac h (Guid me tadataid i n optionse tmetadatai ds) | |||||
| 392 | { | |||||
| 393 | forea ch (Option SetOption oso in _op tionLabelL ist) | |||||
| 394 | { | |||||
| 395 | if ( metadataid == oso.op tionsetId) | |||||
| 396 | { | |||||
| 397 | opt ionSetOpti onsToRemov e.Add(oso) ; | |||||
| 398 | } | |||||
| 399 | } | |||||
| 400 | } | |||||
| 401 | } | |||||
| 402 | foreach (OptionSe tOption op tion in op tionSetOpt ionsToRemo ve) | |||||
| 403 | { | |||||
| 404 | _optio nLabelList .Remove(op tion); | |||||
| 405 | } | |||||
| 406 | if (sho wChanges) | |||||
| 407 | { | |||||
| 408 | if (op tionSetOpt ionsToRemo ve.Count > 0) | |||||
| 409 | { | |||||
| 410 | Conso le.WriteLi ne("{0} Op tion Label s removed" , optionSe tOptionsTo Remove.Cou nt); | |||||
| 411 | Conso le.WriteLi ne("{0} To tal Option Labels cu rrently ca ched", _op tionLabelL ist.Count) ; | |||||
| 412 | Conso le.WriteLi ne(""); | |||||
| 413 | } | |||||
| 414 | else | |||||
| 415 | { | |||||
| 416 | Conso le.WriteLi ne("No Opt ion Labels removed." ); | |||||
| 417 | Conso le.WriteLi ne(""); | |||||
| 418 | } | |||||
| 419 | ||||||
| 420 | } | |||||
| 421 | } | |||||
| 422 | //</snip petremoveO ptionLabel sFromCache > | |||||
| 423 | ||||||
| 424 | protecte d void add CustomEnti tyWithOpti onSet() | |||||
| 425 | { | |||||
| 426 | String primaryAtt ributeSche maName = " sample_Sam pleEntityF orMetadata QueryName" ; | |||||
| 427 | ||||||
| 428 | CreateE ntityReque st createE ntityReque st = new C reateEntit yRequest | |||||
| 429 | { | |||||
| 430 | ||||||
| 431 | //Defi ne the ent ity | |||||
| 432 | Entity = new Ent ityMetadat a | |||||
| 433 | { | |||||
| 434 | Schem aName = _c ustomEntit ySchemaNam e, | |||||
| 435 | Logic alName = _ customEnti tySchemaNa me.ToLower (), | |||||
| 436 | Displ ayName = n ew Label(" Entity for MetadataQ uery Sampl e", _langu ageCode), | |||||
| 437 | Displ ayCollecti onName = n ew Label(" Entity for MetadataQ uery Sampl e", _langu ageCode), | |||||
| 438 | Descr iption = n ew Label(" An entity created fo r the Meta dataQuery Sample", _ languageCo de), | |||||
| 439 | Owner shipType = Ownership Types.User Owned, | |||||
| 440 | IsVis ibleInMobi le = new B ooleanMana gedPropert y(true), | |||||
| 441 | IsAct ivity = fa lse, | |||||
| 442 | ||||||
| 443 | }, | |||||
| 444 | ||||||
| 445 | // Def ine the pr imary attr ibute for the entity | |||||
| 446 | ||||||
| 447 | ||||||
| 448 | Primar yAttribute = new Str ingAttribu teMetadata | |||||
| 449 | { | |||||
| 450 | Schem aName = pr imaryAttri buteSchema Name, | |||||
| 451 | Logic alName = p rimaryAttr ibuteSchem aName.ToLo wer(), | |||||
| 452 | Requi redLevel = new Attri buteRequir edLevelMan agedProper ty(Attribu teRequired Level.None ), | |||||
| 453 | MaxLe ngth = 100 , | |||||
| 454 | Forma t = String Format.Tex t, | |||||
| 455 | Displ ayName = n ew Label(" Entity for MetadataQ uery Sampl e Name", _ languageCo de), | |||||
| 456 | Descr iption = n ew Label(" The primar y attribut e for the Bank Accou nt entity. ", _langua geCode) | |||||
| 457 | } | |||||
| 458 | ||||||
| 459 | }; | |||||
| 460 | _service.E xecute(cre ateEntityR equest); | |||||
| 461 | ||||||
| 462 | ||||||
| 463 | //Publi shXmlReque st publish XmlRequest = new Pub lishXmlReq uest { Par ameterXml = String.F ormat("<im portexport xml><entit ies><entit y>{0}</ent ity></enti ties></imp ortexportx ml>", _cus tomEntityS chemaName. ToLower()) }; | |||||
| 464 | //_serv ice.Execut e(publishX mlRequest) ; | |||||
| 465 | ||||||
| 466 | //Add a n optionse t attribut e | |||||
| 467 | ||||||
| 468 | CreateA ttributeRe quest crea teAttribut eRequest = new Creat eAttribute Request | |||||
| 469 | { | |||||
| 470 | Entity Name = _cu stomEntity SchemaName .ToLower() , | |||||
| 471 | Attrib ute = new PicklistAt tributeMet adata | |||||
| 472 | { | |||||
| 473 | Schem aName = _c ustomAttri buteSchema Name, | |||||
| 474 | Displ ayName = n ew Label(" Example Op tionSet fo r Metadata Query Samp le", _lang uageCode), | |||||
| 475 | Requi redLevel = new Attri buteRequir edLevelMan agedProper ty(Attribu teRequired Level.None ), | |||||
| 476 | ||||||
| 477 | Optio nSet = new OptionSet Metadata | |||||
| 478 | { | |||||
| 479 | IsGl obal = fal se, | |||||
| 480 | Opti onSetType = OptionSe tType.Pick list, | |||||
| 481 | Opti ons = { | |||||
| 482 | new OptionMet adata(new Label("Fir st Option" ,_language Code),null ), | |||||
| 483 | new OptionMet adata(new Label("Sec ond Option ",_languag eCode),nul l), | |||||
| 484 | new OptionMet adata(new Label("Thi rd Option" ,_language Code),null ), | |||||
| 485 | new OptionMet adata(new Label("Fou rth Option ",_languag eCode),nul l) | |||||
| 486 | } | |||||
| 487 | } | |||||
| 488 | } | |||||
| 489 | }; | |||||
| 490 | ||||||
| 491 | _servic e.Execute( createAttr ibuteReque st); | |||||
| 492 | ||||||
| 493 | } | |||||
| 494 | ||||||
| 495 | protecte d void add OptionToCu stomEntity OptionSet( ) | |||||
| 496 | { | |||||
| 497 | ||||||
| 498 | InsertO ptionValue Request in sertOption ValueReque st = | |||||
| 499 | new In sertOption ValueReque st | |||||
| 500 | { | |||||
| 501 | Attri buteLogica lName = _c ustomAttri buteSchema Name.ToLow er(), | |||||
| 502 | Entit yLogicalNa me = _cust omEntitySc hemaName.T oLower(), | |||||
| 503 | Label = new Lab el("Fifth Option", _ languageCo de) | |||||
| 504 | }; | |||||
| 505 | ||||||
| 506 | _servic e.Execute( insertOpti onValueReq uest); | |||||
| 507 | ||||||
| 508 | ||||||
| 509 | } | |||||
| 510 | ||||||
| 511 | ||||||
| 512 | protecte d void del eteCustomE ntityWithO ptionSet() | |||||
| 513 | { | |||||
| 514 | ||||||
| 515 | DeleteE ntityReque st request = new Del eteEntityR equest() | |||||
| 516 | { | |||||
| 517 | Logica lName = _c ustomEntit ySchemaNam e.ToLower( ), | |||||
| 518 | }; | |||||
| 519 | _servic e.Execute( request); | |||||
| 520 | ||||||
| 521 | ||||||
| 522 | } | |||||
| 523 | //<snipp etLabelQue ryExpressi on2> | |||||
| 524 | protecte d int Retr ieveUserUI LanguageCo de(Guid us erId) | |||||
| 525 | { | |||||
| 526 | QueryEx pression u serSetting sQuery = n ew QueryEx pression(" usersettin gs"); | |||||
| 527 | userSet tingsQuery .ColumnSet .AddColumn s("uilangu ageid", "s ystemuseri d"); | |||||
| 528 | userSet tingsQuery .Criteria. AddConditi on("system userid", C onditionOp erator.Equ al, userId ); | |||||
| 529 | EntityC ollection userSettin gs = _serv ice.Retrie veMultiple (userSetti ngsQuery); | |||||
| 530 | if (use rSettings. Entities.C ount > 0) | |||||
| 531 | { | |||||
| 532 | return (int)user Settings.E ntities[0] ["uilangua geid"]; | |||||
| 533 | } | |||||
| 534 | return 0; | |||||
| 535 | } | |||||
| 536 | //</snip petLabelQu eryExpress ion2> | |||||
| 537 | ||||||
| 538 | protecte d void pub lishUpdate dEntity() | |||||
| 539 | { | |||||
| 540 | Publish XmlRequest publishXm lRequest = new Publi shXmlReque st | |||||
| 541 | { | |||||
| 542 | Parame terXml = " <importexp ortxml><en tities><en tity>" + _ customEnti tySchemaNa me.ToLower () + "</en tity></ent ities></im portexport xml>" | |||||
| 543 | }; | |||||
| 544 | _servic e.Execute( publishXml Request); | |||||
| 545 | } | |||||
| 546 | ||||||
| 547 | ||||||
| 548 | #region Main metho d | |||||
| 549 | ||||||
| 550 | /// <sum mary> | |||||
| 551 | /// Base d on the M ain() meth od used by most SDK samples. | |||||
| 552 | /// </su mmary> | |||||
| 553 | /// <par am name="a rgs"></par am> | |||||
| 554 | static p ublic void Main(stri ng[] args) | |||||
| 555 | { | |||||
| 556 | try | |||||
| 557 | { | |||||
| 558 | // Obt ain the ta rget organ ization's Web addres s and clie nt logon | |||||
| 559 | // cre dentials f rom the us er. | |||||
| 560 | Server Connection serverCon nect = new ServerCon nection(); | |||||
| 561 | Server Connection .Configura tion confi g = server Connect.Ge tServerCon figuration (); | |||||
| 562 | ||||||
| 563 | Metada taQuerySam ple app = new Metada taQuerySam ple(); | |||||
| 564 | app.Ru n(config); | |||||
| 565 | } | |||||
| 566 | ||||||
| 567 | catch ( FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> e x) | |||||
| 568 | { | |||||
| 569 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 570 | Consol e.WriteLin e("Timesta mp: {0}", ex.Detail. Timestamp) ; | |||||
| 571 | Consol e.WriteLin e("Code: { 0}", ex.De tail.Error Code); | |||||
| 572 | Consol e.WriteLin e("Message : {0}", ex .Detail.Me ssage); | |||||
| 573 | Consol e.WriteLin e("Trace: {0}", ex.D etail.Trac eText); | |||||
| 574 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 575 | nu ll == ex.D etail.Inne rFault ? " Has Inner Fault" : " No Inner F ault"); | |||||
| 576 | } | |||||
| 577 | catch ( System.Tim eoutExcept ion ex) | |||||
| 578 | { | |||||
| 579 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 580 | Consol e.WriteLin e("Message : {0}", ex .Message); | |||||
| 581 | Consol e.WriteLin e("Stack T race: {0}" , ex.Stack Trace); | |||||
| 582 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 583 | nu ll == ex.I nnerExcept ion.Messag e ? "Has I nner Fault " : "No In ner Fault" ); | |||||
| 584 | } | |||||
| 585 | catch ( System.Exc eption ex) | |||||
| 586 | { | |||||
| 587 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 588 | Consol e.WriteLin e(ex.Messa ge); | |||||
| 589 | ||||||
| 590 | // Dis play the d etails of the inner exception. | |||||
| 591 | if (ex .InnerExce ption != n ull) | |||||
| 592 | { | |||||
| 593 | Conso le.WriteLi ne(ex.Inne rException .Message); | |||||
| 594 | ||||||
| 595 | Fault Exception< Microsoft. Xrm.Sdk.Or ganization ServiceFau lt> fe = e x.InnerExc eption | |||||
| 596 | a s FaultExc eption<Mic rosoft.Xrm .Sdk.Organ izationSer viceFault> ; | |||||
| 597 | if (f e != null) | |||||
| 598 | { | |||||
| 599 | Cons ole.WriteL ine("Times tamp: {0}" , fe.Detai l.Timestam p); | |||||
| 600 | Cons ole.WriteL ine("Code: {0}", fe. Detail.Err orCode); | |||||
| 601 | Cons ole.WriteL ine("Messa ge: {0}", fe.Detail. Message); | |||||
| 602 | Cons ole.WriteL ine("Trace : {0}", fe .Detail.Tr aceText); | |||||
| 603 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 604 | null == fe .Detail.In nerFault ? "Has Inne r Fault" : "No Inner Fault"); | |||||
| 605 | } | |||||
| 606 | } | |||||
| 607 | } | |||||
| 608 | ||||||
| 609 | //Addi tional exc eptions to catch: Se curityToke nValidatio nException , ExpiredS ecurityTok enExceptio n, | |||||
| 610 | //Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 611 | ||||||
| 612 | finally | |||||
| 613 | { | |||||
| 614 | Consol e.WriteLin e("Press < Enter> to exit."); | |||||
| 615 | Consol e.ReadLine (); | |||||
| 616 | } | |||||
| 617 | } | |||||
| 618 | #endregi on Main me thod | |||||
| 619 | ||||||
| 620 | } | |||||
| 621 | ||||||
| 622 | public cl ass Option SetOption { | |||||
| 623 | public O ptionSetOp tion(Guid OptionsetI d, int Opt ionValue, String Lab el) | |||||
| 624 | { | |||||
| 625 | this._op tionsetId = Optionse tId; | |||||
| 626 | this._op tionValue = OptionVa lue; | |||||
| 627 | this._la bel = Labe l; | |||||
| 628 | } | |||||
| 629 | ||||||
| 630 | private Guid _opti onsetId; | |||||
| 631 | private int _optio nValue; | |||||
| 632 | private String _la bel; | |||||
| 633 | ||||||
| 634 | public G uid option setId { ge t { return this._opt ionsetId; } } | |||||
| 635 | public i nt optionV alue { get { return this._opti onValue; } } | |||||
| 636 | public S tring labl e { get { return thi s._label; } } | |||||
| 637 | } | |||||
| 638 | ||||||
| 639 | ||||||
| 640 | } | |||||
| 641 | //</snippe tMetadataQ uery> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.