protected static OrganizationService CreateOrganizationService(string portalName = null, bool allowDefaultFallback = false, string serviceName = null) { var portalContextElement = PortalCrmConfigurationManager.GetPortalContextElement(portalName, allowDefaultFallback); var contextName = !string.IsNullOrWhiteSpace(portalContextElement.ContextName) ? portalContextElement.ContextName : portalContextElement.Name; var connection = new CrmConnection(CrmConfigurationManager.GetConnectionStringNameFromContext(contextName)); return(CrmConfigurationManager.CreateService(connection, serviceName) as OrganizationService); }
/// <summary> /// Retrieves the configured <see cref="IOrganizationService"/>. /// </summary> /// <param name="portalName"></param> /// <param name="allowDefaultFallback"></param> /// <returns></returns> public virtual IOrganizationService CreateOrganizationService(string portalName = null, bool allowDefaultFallback = false) { var portalContextElement = GetPortalContextElement(portalName, allowDefaultFallback); var contextName = !string.IsNullOrWhiteSpace(portalContextElement.ContextName) ? portalContextElement.ContextName : portalContextElement.Name; var service = CrmConfigurationManager.CreateService(contextName, true); return(service); }
public override IOrganizationService Create() { //try //{ //if (HttpContext.Current.Request.Cookies["Branch"] != null) //{ // return CrmConfigurationManager.CreateService(new CrmConnection(HttpContext.Current.Request.Cookies["Branch"]["branch"])); //} //return CrmConfigurationManager.CreateService(new CrmConnection("Xrm")); //} //catch (System.Exception) //{ return(CrmConfigurationManager.CreateService(new CrmConnection("Xrm"))); //} }
public static CrmDbContext Create() { return(new ApplicationDbContext(CrmConfigurationManager.CreateService(new CrmConnection("Xrm")))); }
public CrmOrganizationServiceContext(CrmConnection connection) : this(CrmConfigurationManager.CreateService(connection)) { _shouldDisposeService = true; }
public CrmOrganizationServiceContext(string contextName) : this(CrmConfigurationManager.CreateService(contextName)) { _shouldDisposeService = true; }
private void PopulateDropDown(DropDownList dropDown) { if (dropDown.Items.Count > 0) { return; } var empty = new ListItem(string.Empty, string.Empty); empty.Attributes["label"] = " "; dropDown.Items.Add(empty); var context = CrmConfigurationManager.CreateContext(); var service = CrmConfigurationManager.CreateService(); // By default a lookup field cell defined in the form XML does not contain view parameters unless the user has specified a view that is not the default for that entity and we must query to find the default view. Saved Query entity's QueryType code 64 indicates a lookup view. var view = Metadata.LookupViewID == Guid.Empty ? context.CreateQuery("savedquery").FirstOrDefault(s => s.GetAttributeValue <string>("returnedtypecode") == "subject" && s.GetAttributeValue <bool?>("isdefault").GetValueOrDefault(false) && s.GetAttributeValue <int>("querytype") == 64) : context.CreateQuery("savedquery").FirstOrDefault(s => s.GetAttributeValue <Guid>("savedqueryid") == Metadata.LookupViewID); List <Entity> subjects; if (view != null) { var fetchXML = view.GetAttributeValue <string>("fetchxml"); var xElement = XElement.Parse(fetchXML); var parentsubjectElement = xElement.Descendants("attribute").FirstOrDefault(e => { var xAttribute = e.Attribute("name"); return(xAttribute != null && xAttribute.Value == "parentsubject"); }); if (parentsubjectElement == null) { //If fetchxml does not contain the parentsubject attribute then it must be injected so the results can be organized in a hierarchical order. var entityElement = xElement.Element("entity"); if (entityElement == null) { return; } var p = new XElement("attribute", new XAttribute("name", "parentsubject")); entityElement.Add(p); fetchXML = xElement.ToString(); } var data = service.RetrieveMultiple(new FetchExpression(fetchXML)); if (data == null || data.Entities == null) { return; } subjects = data.Entities.ToList(); } else { subjects = context.CreateQuery("subject").ToList(); } var parents = subjects.Where(s => s.GetAttributeValue <EntityReference>("parentsubject") == null).OrderBy(s => s.GetAttributeValue <string>("title")); foreach (var parent in parents) { if (parent == null) { continue; } dropDown.Items.Add(new ListItem(parent.GetAttributeValue <string>("title"), parent.Id.ToString())); var parentId = parent.Id; var children = subjects.Where(s => s.GetAttributeValue <EntityReference>("parentsubject") != null && s.GetAttributeValue <EntityReference>("parentsubject").Id == parentId).OrderBy(s => s.GetAttributeValue <string>("title")); AddChildItems(dropDown, subjects, children, 1); } }
public override IOrganizationService Create() { return(CrmConfigurationManager.CreateService(new CrmConnection("Xrm"))); }
private void PopulateDropDownIfFirstLoad(DropDownList dropDown, string lookupEntityName) { if (dropDown.Items.Count > 0) { return; } var empty = new ListItem(string.Empty, string.Empty); empty.Attributes["label"] = " "; dropDown.Items.Add(empty); var context = CrmConfigurationManager.CreateContext(); var service = CrmConfigurationManager.CreateService(); var metadataCache = new Dictionary <string, EntityMetadata>(); var entityMetadata = GetEntityMetadata(context, Metadata.LookupTargets[0], metadataCache); var primaryNameAttribute = entityMetadata.PrimaryNameAttribute; var primaryKeyAttribute = entityMetadata.PrimaryIdAttribute; var localizedPrimaryNameAttribute = primaryNameAttribute; // get a localized primary attribute name if (Metadata.LanguageCode > 0) { var defaultLanguageCode = RetrieveOrganizationBaseLanguageCode(context); if (Metadata.LanguageCode != defaultLanguageCode) { localizedPrimaryNameAttribute = string.Format("{0}_{1}", primaryNameAttribute, Metadata.LanguageCode); foreach (var att in entityMetadata.Attributes.Where(att => att.LogicalName.EndsWith(localizedPrimaryNameAttribute))) { primaryNameAttribute = att.LogicalName; break; } } } // By default a lookup field cell defined in the form XML does not contain view parameters unless the user has specified a view that is not the default for that entity and we must query to find the default view. Saved Query entity's QueryType code 64 indicates a lookup view. var view = Metadata.LookupViewID == Guid.Empty ? context.CreateQuery("savedquery").FirstOrDefault(s => s.GetAttributeValue <string>("returnedtypecode") == lookupEntityName && s.GetAttributeValue <bool>("isdefault") && s.GetAttributeValue <int>("querytype") == 64) : context.CreateQuery("savedquery").FirstOrDefault(s => s.GetAttributeValue <Guid>("savedqueryid") == Metadata.LookupViewID); IQueryable <Entity> lookupEntities; if (view != null) { var fetchXml = view.GetAttributeValue <string>("fetchxml"); lookupEntities = GetLookupRecords(fetchXml, context); if (lookupEntities == null) { return; } } else { string fetchXml = string.Format(@" <fetch mapping='logical'> <entity name='{0}'> <attribute name='{1}'/> <attrbiute name='{2}'/> </entity> </fetch> " , lookupEntityName, primaryKeyAttribute, primaryNameAttribute); lookupEntities = GetLookupRecords(fetchXml, context); if (lookupEntities == null) { return; } } foreach (var entity in lookupEntities) { dropDown.Items.Add(new ListItem { Value = entity.Id.ToString(), Text = entity.Attributes.ContainsKey(localizedPrimaryNameAttribute) ? entity.GetAttributeValue(localizedPrimaryNameAttribute).ToString() : entity.Attributes.ContainsKey(primaryNameAttribute) ? entity.GetAttributeValue(primaryNameAttribute).ToString() : string.Empty }); } }
public static bool ProvisioningInProgress() { if (!PortalSettings.Instance.UseOnlineSetup) { return(false); } bool inProgress; var setupInProgress = bool.TryParse(ConfigurationManager.AppSettings["PortalSetupInProgress"], out inProgress) && inProgress; if (!setupInProgress) { return(false); } var websiteId = ConfigurationManager.AppSettings["PortalPackageName"]; // Try to query the CRM for a PackageImportComplete setting to see if the package may have been installed already. try { var query = new QueryExpression("adx_setting") { ColumnSet = new ColumnSet("adx_name") }; query.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0); query.Criteria.AddCondition("adx_name", ConditionOperator.Equal, "PackageImportComplete"); query.Criteria.AddCondition("adx_value", ConditionOperator.Equal, websiteId); var service = CrmConfigurationManager.CreateService(); if (service.RetrieveMultiple(query).Entities.Count > 0) { return(false); } } catch (Exception e) { // Ignore connection exceptions ADXTrace.Instance.TraceInfo(TraceCategory.Exception, string.Format("PackageImportComplete: Provisioning is in progress: {0}", e)); } // If there is a website record & the plugins are enabled then we can create a website binding. try { var adxWebsiteQuery = new QueryExpression("adx_website"); adxWebsiteQuery.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0); adxWebsiteQuery.Criteria.AddCondition("adx_websiteid", ConditionOperator.Equal, websiteId); var service = CrmConfigurationManager.CreateService(); if (service.RetrieveMultiple(adxWebsiteQuery).Entities.Count > 0) { // Now Check if the plugins are enabled. If they are enabled then we know that the data import is complete or if // its not then the cache invalidation plugin is enabled so the webapp will be told of any new changes. var webNotificationPluginQuery = new QueryExpression("pluginassembly"); webNotificationPluginQuery.Criteria.AddCondition("name", ConditionOperator.Equal, "Adxstudio.Xrm.Plugins.WebNotification"); webNotificationPluginQuery.Criteria.AddCondition("componentstate", ConditionOperator.Equal, 0); if (service.RetrieveMultiple(webNotificationPluginQuery).Entities.Count > 0) { ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Ending provisioning in progress. Creating the AdxSetting"); SetSettingForPackageComplete(service, websiteId); return(false); } } } catch (Exception e) { // Ignore connection exceptions ADXTrace.Instance.TraceInfo(TraceCategory.Exception, string.Format("Website: Provisioning is in progress: {0}", e)); } return(true); }