Kentico 10 Filtered Repeater with multiple filters not working properly - filter

I have a data source, filter, and a filtered repeater that I have created custom filters for.
For some reason, both my filters work without the other on the screen. One works fine with the other filter on the screen.
The last filter refuses to work when I have the other filter on the screen but works fine without anything else.
I am pretty sure it has something to do with my code behind files which I will put below.
FILTER#1
using CMS.DocumentEngine;
using CMS.Helpers;
using System;
using System.Web;
using System.Web.UI.WebControls;
using CMS.DocumentEngine.Web.UI;
public partial class CMSGlobalFiles_SectorFilterControl : CMSAbstractDataFilterControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Sets up the inner child controls.
/// </summary>
private void SetupControl()
{
// Hides the filter if StopProcessing is enabled
if (this.StopProcessing)
{
this.Visible = false;
}
// Initializes only if the current request is NOT a postback
else if (!RequestHelper.IsPostBack())
{
// Loads product departments as filtering options
InitializeClientSectors();
}
}
/// <summary>
/// Loads all existing product departments as filtering options into the department drop-down list.
/// </summary>
private void InitializeClientSectors()
{
// Adds the default '(all)' option
this.drpSector.Items.Insert(0, new ListItem("(all)", "##ALL##"));
var clientSectors = DocumentHelper.GetDocuments("BBUS.Sector")
.Path("/Sector/", PathTypeEnum.Children)
.OnSite("Balfour-dev.allata.com");
if (!DataHelper.DataSourceIsEmpty(clientSectors))
{
int count = 1;
foreach (var clientSector in clientSectors)
{
var ClientSectorID = clientSector.GetValue("SectorID").ToString();
this.drpSector.Items.Insert(count++, new ListItem(clientSector.DocumentName, ClientSectorID));
}
}
}
/// <summary>
/// Generates a WHERE condition and ORDER BY clause based on the current filtering selection.
/// </summary>
private void SetFilter()
{
string where = null;
// Generates a WHERE condition based on the selected product department
if (this.drpSector.SelectedIndex > 0 && this.drpSector.SelectedValue != null)
{
//where = string.Format("clientSector = {0}", this.drpClientSector.SelectedValue);
where = string.Format(
"sector LIKE '%|{0}|%' " +
"OR sector LIKE '{0}|%' " +
"OR sector LIKE '%|{0}' " +
"OR sector = '{0}'", this.drpSector.SelectedValue);
}
if (where != null)
{
// Sets the Where condition
this.WhereCondition = where;
}
// Raises the filter changed event
this.RaiseOnFilterChanged();
}
/// <summary>
/// Init event handler.
/// </summary>
protected override void OnInit(EventArgs e)
{
// Creates the child controls
SetupControl();
base.OnInit(e);
}
/// <summary>
/// PreRender event handler
/// </summary>
protected override void OnPreRender(EventArgs e)
{
var ClientSectorID = HttpContext.Current.Request.QueryString.Get("SectorID");
// Checks if the current request is a postback
if (RequestHelper.IsPostBack())
{
// Applies the filter to the displayed data
SetFilter();
}
else if (!string.IsNullOrEmpty(ClientSectorID))
{
this.drpSector.SelectedIndex = this.drpSector.Items.IndexOf(this.drpSector.Items.FindByValue(ClientSectorID));
SetFilter();
}
base.OnPreRender(e);
}
protected void btnFilter_Click(object sender, EventArgs e)
{
// Remove Query Strings
string url = Request.RawUrl.Split(new[] { '?' })[0];
// Add ClientSectorID Query String
string updatedQueryString = "?" + "SectorID=" + this.drpSector.SelectedValue;
Response.Redirect(url + updatedQueryString);
}
}
FILTER#2
using CMS.DocumentEngine;
using CMS.Helpers;
using System;
using System.Web;
using System.Web.UI.WebControls;
using CMS.DocumentEngine.Web.UI;
public partial class CMSGlobalFiles_SectorFilterControl : CMSAbstractDataFilterControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Sets up the inner child controls.
/// </summary>
private void SetupControl()
{
// Hides the filter if StopProcessing is enabled
if (this.StopProcessing)
{
this.Visible = false;
}
// Initializes only if the current request is NOT a postback
else if (!RequestHelper.IsPostBack())
{
// Loads product departments as filtering options
InitializeClientSectors();
}
}
/// <summary>
/// Loads all existing product departments as filtering options into the department drop-down list.
/// </summary>
private void InitializeClientSectors()
{
// Adds the default '(all)' option
this.drpSector.Items.Insert(0, new ListItem("(all)", "##ALL##"));
var clientSectors = DocumentHelper.GetDocuments("BBUS.Sector")
.Path("/Sector/", PathTypeEnum.Children)
.OnSite("Balfour-dev.allata.com");
if (!DataHelper.DataSourceIsEmpty(clientSectors))
{
int count = 1;
foreach (var clientSector in clientSectors)
{
var ClientSectorID = clientSector.GetValue("SectorID").ToString();
this.drpSector.Items.Insert(count++, new ListItem(clientSector.DocumentName, ClientSectorID));
}
}
}
/// <summary>
/// Generates a WHERE condition and ORDER BY clause based on the current filtering selection.
/// </summary>
private void SetFilter()
{
string where = null;
// Generates a WHERE condition based on the selected product department
if (this.drpSector.SelectedIndex > 0 && this.drpSector.SelectedValue != null)
{
//where = string.Format("clientSector = {0}", this.drpClientSector.SelectedValue);
where = string.Format(
"sector LIKE '%|{0}|%' " +
"OR sector LIKE '{0}|%' " +
"OR sector LIKE '%|{0}' " +
"OR sector = '{0}'", this.drpSector.SelectedValue);
}
if (where != null)
{
// Sets the Where condition
this.WhereCondition = where;
}
// Raises the filter changed event
this.RaiseOnFilterChanged();
}
/// <summary>
/// Init event handler.
/// </summary>
protected override void OnInit(EventArgs e)
{
// Creates the child controls
SetupControl();
base.OnInit(e);
}
/// <summary>
/// PreRender event handler
/// </summary>
protected override void OnPreRender(EventArgs e)
{
var ClientSectorID = HttpContext.Current.Request.QueryString.Get("SectorID");
// Checks if the current request is a postback
if (RequestHelper.IsPostBack())
{
// Applies the filter to the displayed data
SetFilter();
}
else if (!string.IsNullOrEmpty(ClientSectorID))
{
this.drpSector.SelectedIndex = this.drpSector.Items.IndexOf(this.drpSector.Items.FindByValue(ClientSectorID));
SetFilter();
}
base.OnPreRender(e);
}
protected void btnFilter_Click(object sender, EventArgs e)
{
// Remove Query Strings
string url = Request.RawUrl.Split(new[] { '?' })[0];
// Add ClientSectorID Query String
string updatedQueryString = "?" + "SectorID=" + this.drpSector.SelectedValue;
Response.Redirect(url + updatedQueryString);
}
}

Sadly this is a little bit of a limitation, you can only have 1 filter per Repeater/Data Source (except Smart Search it seems, which can handle multiple).
You will most likely need to combine both of your filters into 1 filter, and combine the logics into one where condition.
https://docs.kentico.com/k10/developing-websites/loading-and-displaying-data-on-websites/filtering-and-paging-data
Would be great to allow multiple filters though!

Related

C# Prism Сommunication between AvalonDock and MainApplicationView

I'm trying to figure out how to work with AvalonDock in Prism. My MainApplciationView Xaml code:
<Window x:Class="WPFTestLab.Views.ApplicationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fa="http://schemas.fontawesome.com/icons/"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:core="clr-namespace:WPFTestLab.Core;assembly=WPFTestLab.Core"
xmlns:avalonDock="https://github.com/Dirkster99/AvalonDock"
Title="{Binding Title}" Width="1280" Height="720" WindowStartupLocation="CenterScreen">
<Grid>
<avalonDock:DockingManager prism:RegionManager.RegionName="{x:Static core:RegionNames.MainRegion}">
<avalonDock:LayoutRoot>
<avalonDock:LayoutPanel Orientation="Horizontal">
<avalonDock:LayoutDocumentPaneGroup DockWidth="100" Orientation="Vertical">
<avalonDock:LayoutDocumentPane>
</avalonDock:LayoutDocumentPane>
</avalonDock:LayoutDocumentPaneGroup>
</avalonDock:LayoutPanel>
</avalonDock:LayoutRoot>
</avalonDock:DockingManager>
</Grid>
And I create docking manager region adapter:
public class AvalonDockingRegionAdapter : RegionAdapterBase<DockingManager>
{
private bool _updatingActiveViewsInManagerActiveContentChanged;
#region Constructor
public AvalonDockingRegionAdapter(IRegionBehaviorFactory factory)
: base(factory)
{
_updatingActiveViewsInManagerActiveContentChanged = false;
}
#endregion //Constructor
#region Overrides
protected override IRegion CreateRegion()
{
return new SingleActiveRegion();
}
protected override void Adapt(IRegion region, DockingManager regionTarget)
{
regionTarget.ActiveContentChanged += delegate (
object sender, EventArgs e)
{
this.ManagerActiveContentChanged(sender, e, region, regionTarget);
};
region.Views.CollectionChanged += delegate (
Object sender, NotifyCollectionChangedEventArgs e)
{
this.OnViewsCollectionChanged(sender, e, region, regionTarget);
};
region.Views.CollectionChanged += delegate (
Object sender, NotifyCollectionChangedEventArgs e)
{
this.OnActiveViewsCollectionChanged(sender, e, region, regionTarget);
};
regionTarget.DocumentClosed += delegate (
Object sender, DocumentClosedEventArgs e)
{
this.OnDocumentClosedEventArgs(sender, e, region);
};
}
private void OnActiveViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, IRegion region, DockingManager regionTarget)
{
if (_updatingActiveViewsInManagerActiveContentChanged) return;
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (regionTarget.ActiveContent != null && regionTarget.ActiveContent != e.NewItems[0] &&
region.ActiveViews.Contains(regionTarget.ActiveContent))
region.Deactivate(regionTarget.ActiveContent);
regionTarget.ActiveContent = e.NewItems[0];
}
else if (e.Action == NotifyCollectionChangedAction.Remove &&
e.OldItems.Contains(regionTarget.ActiveContent))
{
regionTarget.ActiveContent = null;
}
}
private void ManagerActiveContentChanged(object sender, EventArgs e, IRegion region, DockingManager regionTarget)
{
try
{
_updatingActiveViewsInManagerActiveContentChanged = true;
if (regionTarget == sender)
{
var activeContent = regionTarget.ActiveContent;
if (activeContent != null)
{
foreach (var item in region.ActiveViews.Where(it => it != activeContent))
if (region.Views.Contains(item))
region.Deactivate(item);
if (region.Views.Contains(activeContent) && !region.ActiveViews.Contains(activeContent))
region.Activate(activeContent);
}
}
}
finally
{
_updatingActiveViewsInManagerActiveContentChanged = false;
}
}
#endregion //Overrides
#region Event Handlers
/// <summary>
/// Handles the NotifyCollectionChangedEventArgs event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event.</param>
/// <param name="region">The region.</param>
/// <param name="regionTarget">The region target.</param>
void OnViewsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e, IRegion region, DockingManager regionTarget)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (FrameworkElement item in e.NewItems)
{
UIElement view = item as UIElement;
if (view != null)
{
//Create a new layout document to be included in the LayoutDocuemntPane (defined in xaml)
LayoutDocument newLayoutDocument = new LayoutDocument();
newLayoutDocument.Content = item;
PaneViewModel viewModel = (PaneViewModel)item.DataContext;
if (viewModel != null)
newLayoutDocument.Title = viewModel.Title;
//Store all LayoutDocuments already pertaining to the LayoutDocumentPane (defined in xaml)
List<LayoutDocument> oldLayoutDocuments = new List<LayoutDocument>();
//Get the current ILayoutDocumentPane ... Depending on the arrangement of the views this can be either
//a simple LayoutDocumentPane or a LayoutDocumentPaneGroup
ILayoutDocumentPane currentILayoutDocumentPane = (ILayoutDocumentPane)regionTarget.Layout.RootPanel.Children[0];
if (currentILayoutDocumentPane.GetType() == typeof(LayoutDocumentPaneGroup))
{
//If the current ILayoutDocumentPane turns out to be a group
//Get the children (LayoutDocuments) of the first pane
LayoutDocumentPane oldLayoutDocumentPane = (LayoutDocumentPane)currentILayoutDocumentPane.Children.ToList()[0];
foreach (LayoutDocument child in oldLayoutDocumentPane.Children)
{
oldLayoutDocuments.Insert(0, child);
}
}
else if (currentILayoutDocumentPane.GetType() == typeof(LayoutDocumentPane))
{
//If the current ILayoutDocumentPane turns out to be a simple pane
//Get the children (LayoutDocuments) of the single existing pane.
foreach (LayoutDocument child in currentILayoutDocumentPane.Children)
{
oldLayoutDocuments.Insert(0, child);
}
}
//Create a new LayoutDocumentPane and inserts your new LayoutDocument
LayoutDocumentPane newLayoutDocumentPane = new LayoutDocumentPane();
newLayoutDocumentPane.InsertChildAt(0, newLayoutDocument);
//Append to the new LayoutDocumentPane the old LayoutDocuments
foreach (LayoutDocument doc in oldLayoutDocuments)
{
newLayoutDocumentPane.InsertChildAt(0, doc);
}
//Traverse the visual tree of the xaml and replace the LayoutDocumentPane (or LayoutDocumentPaneGroup) in xaml
//with your new LayoutDocumentPane (or LayoutDocumentPaneGroup)
if (currentILayoutDocumentPane.GetType() == typeof(LayoutDocumentPane))
regionTarget.Layout.RootPanel.ReplaceChildAt(0, newLayoutDocumentPane);
else if (currentILayoutDocumentPane.GetType() == typeof(LayoutDocumentPaneGroup))
{
currentILayoutDocumentPane.ReplaceChild(currentILayoutDocumentPane.Children.ToList()[0], newLayoutDocumentPane);
regionTarget.Layout.RootPanel.ReplaceChildAt(0, currentILayoutDocumentPane);
}
newLayoutDocument.IsActive = true;
}
}
}
}
/// <summary>
/// Handles the DocumentClosedEventArgs event raised by the DockingNanager when
/// one of the LayoutContent it hosts is closed.
/// </summary>
/// <param name="sender">The sender</param>
/// <param name="e">The event.</param>
/// <param name="region">The region.</param>
void OnDocumentClosedEventArgs(object sender, DocumentClosedEventArgs e, IRegion region)
{
region.Remove(e.Document.Content);
}
#endregion
}
My question is how I can manipulate docking manager from MainApplicationViewModel. For example, I want to catch the active document content change and disable button in MainApplicationView. Or clicking the button in MainApplicationView should change the text in current active document view.
Hopefully, the DockingManager has properties and commands that you can bind to stuff on your ApplicationWindowViewModel.
If not, you have to resort to EventToCommand and attached behaviours.
From there on, everything's standard. Document get's active, document view model's IsActive property becomes true, a service is notified, another view model listening to the service becomes aware of the change, the text is changed...

Getting public variable of popped page

I'm instancing another page, and I assign a value to one of its public properties ("SomeValue") like this:
_btnGotoOtherPage.Clicked += async (sender, e) =>
{
OtherPage _otherpage = new OtherPage;
_otherpage.SomeValue = 1033;
await Navigation.PushAsync(_otherpage);
return;
};
Within this "_otherpage", the user can modified this value.
When "_otherpage" is popped, I would like to have a look at the "SomeValue" variable and do something with it.
MessagingSystem wouldn't be what I need because I don't want to be notified on the value change. I only want to know what this value is when "_otherpage" is popped.
I would also like to not use Binding (if possible!) because I feel it's hard to organize when I'm dealing with many of such variables.
Is it possible to do this with an event perhaps?
My dream solution would be (pseudo code):
private void OnPagePopped()
{
int iNewValue = PoppedPage.SomeValue;
}
Thank you.
If you are looking for an ideal solution, I would suggest following the MVVM pattern and moving a lot of your code from your page behind to the view model.
I use an MVVM framework called FreshMvvm. This allows me to perform view model to view model navigation and to pass parameters between them like this:
await CoreMethods.PushPageModel<BPageModel>(myParameter, true);
This passes myParameter to the BPage which I can access in the Init method of the BPage View Model.
When I pop the B page (via the view model) I can pass a parameter back to the A Page
await CoreMethods.PopPageModel(myReturnParam, true);
which I can access in the ReverseInit method of APageViewModel.
Most MVVM frameworks have similar functionality.
Here are more details about FreshMvvm
This is how I do it with my popups, but it can be used with the page style in the same way.
Heres my little example, expecting MainPage = new NavigationPage(new Page1());
It is basically about having a Task on the Page where the public property sits. This task can return the value of the public property. The task will be completed in the OnDisappearing override and return the public property.
To get the value back, you push the page and await the Task page.PagePoppedTask
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace MVVMTests
{
/// <summary>
/// A base implementation for a page, that holds a task,
/// which can be completed and returns a value of type of the generic T
/// </summary>
/// <typeparam name="T"></typeparam>
public class ResultContentPage<T> : ContentPage
{
public Task<T> PagePoppedTask { get { return tcs.Task; } }
private TaskCompletionSource<T> tcs;
public ResultContentPage()
{
tcs = new TaskCompletionSource<T>();
}
/// <summary>
/// Completes the task and sets it result
/// </summary>
/// <param name="result"></param>
protected /* virtual */ void SetPopupResult(T result)
{
if (PagePoppedTask.IsCompleted == false)
tcs.SetResult(result);
}
}
/// <summary>
/// Page1 exists of one button, that creates the Page2, assigns a value to a prop on the Page2
/// and then awaits the PagePoppedTask of Page2
/// </summary>
public class Page1 : ContentPage
{
public Page1()
{
var button = new Button()
{
Text = "Go to page 2"
};
button.Clicked += Button_Clicked;
Content = button;
}
private async void Button_Clicked(object sender, EventArgs e)
{
//Push the page
var newPage = new Page2() { CoolInt = 123 };
await App.Current.MainPage.Navigation.PushAsync(newPage);
//Await the result
int result = await newPage.PagePoppedTask;
System.Diagnostics.Debug.WriteLine("Page result: " + result.ToString());
}
}
/// <summary>
/// Inherits from the ResultContentPage and sets the PagePoppedTask as soon as its Disappearing
/// </summary>
public class Page2 : ResultContentPage<int>
{
public int CoolInt { get; set; } //Your property on the page
public Page2()
{
var button = new Button()
{
Text = "Go back to page 1"
};
button.Clicked += Button_Clicked;
Content = button;
}
private async void Button_Clicked(object sender, EventArgs e)
{
CoolInt = 321; //assign dummy value to CoolInt prop and pop the page
await App.Current.MainPage.Navigation.PopAsync(); //pop the page
}
protected override void OnDisappearing()
{
base.OnDisappearing();
SetPopupResult(CoolInt); //set the result of the task (in ResultContentPage<T>)
}
}
}
In Page A:
MessagingCenter.Subscribe<string>(this, "SomeMessage", (s) => HandleMessage(s));
In Page B:
MessagingCenter.Send<string>("SenderOrAnythingElse", "SomeMessage");
In PageB there is public variable:
public int SomeValue { get; set; }
Now we show PageB:
PageB nPageB = new PageB;
await Navigation.PushAsync(nPageB);
In nPageB, the user can now change PageB's public variable
//we track the "Disappearing" event of the page.
//When it occurs, we get the value
nPageB.Disappearing += async (sender1, e1) =>
{
Debug.WriteLine("New value is: " + nPage.SomeValue.ToString());
};

OdataClient SaveChanges doesn't work and results an empty Microsoft.OData.Client.DataServiceResponse

I encapsulated the odata container because of some extensions:
public class Connector
{
public string apiKey{get;set;}
public Connector(string apiKey)
{
this.apiKey = apiKey;
}
public Default.Container Get()
{
Default.Container container = new Default.Container(new Uri("http://documents.omnisoftonline.be/odata"));
container.BuildingRequest += container_BuildingRequest;
return container;
}
/// <summary>
/// When fetching a single Document, you can enable this method to including fetch the binary data
/// </summary>
/// <param name="container"></param>
/// <returns></returns>
internal bool doIO = false;
internal void container_BuildingRequest(object sender, BuildingRequestEventArgs e)
{
e.Headers.Add("X-ApiKey", apiKey);
if (doIO && e.RequestUri.ToString().Contains("/Documents"))
{
e.RequestUri = new Uri(e.RequestUri.ToString() + (e.RequestUri.Query.Contains("?") ? "&" : "?") + "doIO=true");
}
this.container_BuildingRequest(sender, e);
}
}
When i use my .dll ( using the Connector class), i have an empty result ( statuscode = -1, no headers, ...)
This is how i call the DLL
documentsConnector.Get().AddToDocuments(docToCreate);
var serviceResponse = documentsConnector.Get().SaveChanges(Microsoft.OData.Client.SaveChangesOptions.None); //tried several options
foreach(var operationResponse in serviceResponse)
{
Console.WriteLine("Response: {0}", operationResponse.StatusCode); //no operationResponses, so this isn't executed
}
It could be because my object isn't valid. But it's weird that i don't see any validation happening...
Any thoughts on how to propagate the SaveChanges() or pre-validate ( before submit) the Entity? The post isn't happening ( checked with Fiddler)
My wrapper class created a new container every time, so the entities got deleted from the Default.Container

How can I make CRMSvcUtil.exe generate unduplicated, error-free early-bound option sets?

I use Erik Pool's implementation of ICodeWriterFilterService and Manny Grewal's GenerateOption function as a model to filter out unwanted entities in the file that CRMSvcUtil generates. While Erik recommends returning true for the GenerateOptionSet method to generate enums for option sets, doing so duplicates any of the global option sets that are used by any particular entity (as mentioned in one of the comments on that post).
To address this, I check to see if the option set has been already generated, and if so, I return the default option (presumably false for most cases) as in the below.
//list of generated option sets, instantiated in the constructor
private List<string> GeneratedOptionSets;
public bool GenerateOptionSet
(OptionSetMetadataBase optionSetMetadata, IServiceProvider services)
{
if (!GeneratedOptionSets.Contains(optionSetMetadata.Name))
{
GeneratedOptionSets.Add(optionSetMetadata.Name);
return true;
}
return _defaultService.GenerateOptionSet(optionSetMetadata, services);
}
But when incorporating the generated file in my CRM projects, the compilation error
Cannot convert type 'Microsoft.Xrm.Sdk.OptionSetValue' to 'int'
is always thrown by every line of code that looks like
this.SetAttributeValue
("address1_shippingmethodcode", new Microsoft.Xrm.Sdk.OptionSetValue(((int)(value))));
.
As a workaround, I use a separate project where I filter the entities I need, run CRMSvcUtil with the arguments Erik suggests, replace the troublesome part of the code (int)(value) (where value is an OptionSetValue) with value.Value after the file is generated, and then resave the file, and all issues go away.
My question is this: do I need to do something differently that will fix this compilation error with the default CRMSvcUtil generated file without doing something so hackish as altering that generated file?
You can use the ICustomizeCodeDomService interface to rewrite the SetAttributeValue method for the optionSets. Snippet below:
namespace The.NameSpace
{
using System;
using System.CodeDom;
using System.Diagnostics;
using System.Linq;
using Microsoft.Crm.Services.Utility;
using Microsoft.Xrm.Sdk.Metadata;
/// <summary>
/// The customize code dom service.
/// </summary>
public sealed class CustomizeCodeDomService : ICustomizeCodeDomService
{
#region Constants and Fields
/// <summary>
/// The metadata.
/// </summary>
private IOrganizationMetadata metadata;
#endregion
#region Properties
#endregion
#region Public Methods
/// <summary>
/// The customize code dom.
/// </summary>
/// <param name="codeCompileUnit">
/// The code compile unit.
/// </param>
/// <param name="services">
/// The services.
/// </param>
public void CustomizeCodeDom(CodeCompileUnit codeCompileUnit, IServiceProvider services)
{
// Locate the namespace to use
CodeNamespace codeNamespace = codeCompileUnit.Namespaces[0];
var metadataProviderService = (IMetadataProviderService)services.GetService(typeof(IMetadataProviderService));
var filterService = (ICodeWriterFilterService)services.GetService(typeof(ICodeWriterFilterService));
this.metadata = metadataProviderService.LoadMetadata();
foreach (EntityMetadata entityMetadata in this.metadata.Entities)
{
if (filterService.GenerateEntity(entityMetadata, services))
{
CodeTypeDeclaration entityClass =
codeNamespace.Types.Cast<CodeTypeDeclaration>().First(codeType => codeType.Name.ToUpper() == entityMetadata.SchemaName.ToUpper());
UpdateEnumSetter(entityClass, entityMetadata);
}
}
}
#endregion
#region Private Methods
private static void UpdateEnumSetter(
CodeTypeDeclaration entityClass, EntityMetadata entity)
{
foreach (var attributeMetadata in entity.Attributes.Where(attributeMetadata => String.IsNullOrWhiteSpace(attributeMetadata.AttributeOf)))
{
//Match the respective field Name.
AttributeMetadata metadata1 = attributeMetadata;
foreach (
CodeTypeMember codeMembers in
entityClass.Members.Cast<CodeTypeMember>().Where(
codeMembers => codeMembers.Name == metadata1.SchemaName))
{
var codeProperty = (CodeMemberProperty)codeMembers;
if (codeProperty.HasSet)
{
if (attributeMetadata.AttributeType != null && attributeMetadata.AttributeType.Value == AttributeTypeCode.Picklist)
{
((CodeConditionStatement)codeProperty.SetStatements[1]).FalseStatements[0] =
new CodeSnippetStatement
{
Value =
String.Format(
"this.SetAttributeValue(\"{0}\", new Microsoft.Xrm.Sdk.OptionSetValue(value.Value));",
attributeMetadata.LogicalName)
};
Debug.WriteLine(String.Format("{0}.{1}", entity.LogicalName, attributeMetadata.LogicalName));
}
}
}
}
}
#endregion
}
}
Some changes to the UpdateEnumSetter method:
private static void UpdateEnumSetter(CodeTypeDeclaration entityClass, EntityMetadata entity)
{
foreach (var attributeMetadata in entity.Attributes.Where(attributeMetadata => String.IsNullOrWhiteSpace(attributeMetadata.AttributeOf)))
{
AttributeMetadata currentMetadata = attributeMetadata;
foreach (CodeTypeMember codeMembers in entityClass.Members.Cast<CodeTypeMember>().Where(codeMembers => codeMembers.Name == currentMetadata.SchemaName))
{
CodeMemberProperty codeProperty = (CodeMemberProperty)codeMembers;
if (codeProperty.HasSet)
{
if (attributeMetadata.AttributeType != null && (attributeMetadata.AttributeType.Value == AttributeTypeCode.Picklist || attributeMetadata.AttributeType.Value == AttributeTypeCode.Status))
{
if (codeProperty.SetStatements[1].GetType() == typeof(CodeConditionStatement))
{
((CodeConditionStatement)codeProperty.SetStatements[1]).FalseStatements[0] = new CodeSnippetStatement
{
Value = String.Format("this.SetAttributeValue(\"{0}\", new Microsoft.Xrm.Sdk.OptionSetValue(value.Value));", attributeMetadata.LogicalName)
};
}
else
{
codeProperty.SetStatements[1] = new CodeSnippetStatement(String.Format("this.SetAttributeValue(\"{0}\", new Microsoft.Xrm.Sdk.OptionSetValue(value.Value));", attributeMetadata.LogicalName));
}
}
}
}
}
}
I'm betting that Guarav's answer is the real way to go, but in the absence of documentation surrounding CRMSvcUtil, I'm forced to use my workaround. (I use a separate project where I filter the entities I need, run CRMSvcUtil with the arguments Erik suggests, replace the troublesome part of the code(int)(value) (where value is an OptionSetValue) with value.Value after the file is generated, and then resave the file.)
Not a perfect solution, but it's been working on the few samples I've worked with so far.
It turns out that this fault is to do with the code attempting to make optionsets that look like the code below when the types are available for use. Note the only difference is the correct type being chose for the return type and the cast.
It should be possible to update the codegen stuff to fix this bug, but it might be better to get microsoft to fix the damn thing properly, I would make a solution but I don't really have time to implement it right now because we have a mostly working solution even if we have to deal with the optionsetvalue class.
public enum entityname_optionsetname
{
Value = 200
}
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("myprefix_fieldname")]
public entityname_optionsetname myprefix_FieldName
{
get
{
Microsoft.Xrm.Sdk.OptionSetValue optionSet = this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("myprefix_fieldname");
if ((optionSet != null))
{
return ((entityname_optionsetname)(System.Enum.ToObject(typeof(Microsoft.Xrm.Sdk.OptionSetValue), optionSet.Value)));
}
else
{
return null;
}
}
set
{
this.OnPropertyChanging("myprefix_FieldName");
if ((value == null))
{
this.SetAttributeValue("myprefix_fieldname", null);
}
else
{
this.SetAttributeValue("myprefix_fieldname", new Microsoft.Xrm.Sdk.OptionSetValue(((int)(value))));
}
this.OnPropertyChanged("myprefix_FieldName");
}
}
I finally am able to generate early bound class with a filtered set of entities and error free option set. I found the bulk of my answer through this thread, so thanks guys. The problem though is it's difficult to compile all of the various suggestions into something that actually... compiles. So I thought I'd post my final solution for the benefit of others, here's what worked for me.
I used Erik Pool's, Manny Grewal's, and Peter Majeed's solution for outputting only distinct enums with proper values, then combined that with Gaurav Dalal's solution (updated by JFK007 to fix the cast error) to re-write the SetAttributeValue that caused the (int)(value) error. And as an added bonus, I used the same solution for filtering distinct option sets to also filter for distinct option set values (which was an issue in my org).
The result is a class library containing CodeWriterFilter and CustomizeCodeDomService, the cmd batch file to run CrmSvcUtil.exe, and the filter.xml to filter the entities.
In your class library add references to CrmSvcUtil.exe, Microsoft.Xrm.Sdk, and System.Runtime.Serialization then compile the dll and copy it to the same the folder as your CrmSvcUtil.exe. Use the command I've included to reference your new assembly and build the early bound class file.
CodeWriterFilter:
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Microsoft.Crm.Services.Utility;
using Microsoft.Xrm.Sdk.Metadata;
using System.Text.RegularExpressions;
using Microsoft.Xrm.Sdk;
namespace SvcUtilFilter
{
/// <summary>
/// CodeWriterFilter for CrmSvcUtil that reads list of entities from an xml file to
/// determine whether or not the entity class should be generated.
/// </summary>
public class CodeWriterFilter : ICodeWriterFilterService
{
//list of entity names to generate classes for.
private HashSet<string> _validEntities = new HashSet<string>();
//reference to the default service.
private ICodeWriterFilterService _defaultService = null;
//list of generated option sets, instantiated in the constructor
private List<string> GeneratedOptionSets;
//list of generated options, instantiated in the constructor
private List<string> GeneratedOptions;
/// <summary>
/// constructor
/// </summary>
/// <param name="defaultService">default implementation</param>
public CodeWriterFilter(ICodeWriterFilterService defaultService)
{
this._defaultService = defaultService;
this.GeneratedOptionSets = new List<string>();
this.GeneratedOptions = new List<string>();
LoadFilterData();
}
/// <summary>
/// loads the entity filter data from the filter.xml file
/// </summary>
private void LoadFilterData()
{
XElement xml = XElement.Load("filter.xml");
XElement entitiesElement = xml.Element("entities");
foreach (XElement entityElement in entitiesElement.Elements("entity")) {
_validEntities.Add(entityElement.Value.ToLowerInvariant());
}
}
/// <summary>
/// /Use filter entity list to determine if the entity class should be generated.
/// </summary>
public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
{
return (_validEntities.Contains(entityMetadata.LogicalName.ToLowerInvariant()));
}
//All other methods just use default implementation:
public bool GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
{
return _defaultService.GenerateAttribute(attributeMetadata, services);
}
public bool GenerateOption(OptionMetadata optionMetadata, IServiceProvider services)
{
//return _defaultService.GenerateOption(optionMetadata, services);
string label = optionMetadata.Label.UserLocalizedLabel.Label;
//remove spaces and special characters
label = Regex.Replace(label, #"[^a-zA-Z0-9]", string.Empty);
if (label.Length > 0 && !char.IsLetter(label, 0)) {
label = "Number_" + label;
}
else if (label.Length == 0) {
label = "empty";
}
if (!GeneratedOptions.Exists(l=>l.Equals(label))) {
GeneratedOptions.Add(label);
optionMetadata.Label = new Label(label, 1033);
return _defaultService.GenerateOption(optionMetadata, services);
}
else { return false; }
}
public bool GenerateOptionSet(OptionSetMetadataBase optionSetMetadata, IServiceProvider services)
{
//return _defaultService.GenerateOptionSet(optionSetMetadata, services);
if (!GeneratedOptionSets.Contains(optionSetMetadata.Name)) {
GeneratedOptionSets.Add(optionSetMetadata.Name);
return true;
}
return _defaultService.GenerateOptionSet(optionSetMetadata, services);
}
public bool GenerateRelationship(RelationshipMetadataBase relationshipMetadata, EntityMetadata otherEntityMetadata, IServiceProvider services)
{
return _defaultService.GenerateRelationship(relationshipMetadata, otherEntityMetadata, services);
}
public bool GenerateServiceContext(IServiceProvider services)
{
return _defaultService.GenerateServiceContext(services);
}
}
}
CustomizeCodeDomService:
using System;
using System.CodeDom;
using System.Diagnostics;
using System.Linq;
using Microsoft.Crm.Services.Utility;
using Microsoft.Xrm.Sdk.Metadata;
namespace SvcUtilFilter
{
/// <summary>
/// The customize code dom service.
/// </summary>
public sealed class CustomizeCodeDomService : ICustomizeCodeDomService
{
#region Constants and Fields
/// <summary>
/// The metadata.
/// </summary>
private IOrganizationMetadata metadata;
#endregion
#region Properties
#endregion
#region Public Methods
/// <summary>
/// The customize code dom.
/// </summary>
/// <param name="codeCompileUnit">
/// The code compile unit.
/// </param>
/// <param name="services">
/// The services.
/// </param>
public void CustomizeCodeDom(CodeCompileUnit codeCompileUnit, IServiceProvider services)
{
// Locate the namespace to use
CodeNamespace codeNamespace = codeCompileUnit.Namespaces[0];
var metadataProviderService = (IMetadataProviderService)services.GetService(typeof(IMetadataProviderService));
var filterService = (ICodeWriterFilterService)services.GetService(typeof(ICodeWriterFilterService));
this.metadata = metadataProviderService.LoadMetadata();
foreach (EntityMetadata entityMetadata in this.metadata.Entities) {
if (filterService.GenerateEntity(entityMetadata, services)) {
CodeTypeDeclaration entityClass =
codeNamespace.Types.Cast<CodeTypeDeclaration>().First(codeType => codeType.Name.ToUpper() == entityMetadata.SchemaName.ToUpper());
UpdateEnumSetter(entityClass, entityMetadata);
}
}
}
#endregion
#region Private Methods
private static void UpdateEnumSetter(CodeTypeDeclaration entityClass, EntityMetadata entity)
{
foreach (var attributeMetadata in entity.Attributes.Where(attributeMetadata => String.IsNullOrWhiteSpace(attributeMetadata.AttributeOf))) {
AttributeMetadata currentMetadata = attributeMetadata;
foreach (CodeTypeMember codeMembers in entityClass.Members.Cast<CodeTypeMember>().Where(codeMembers => codeMembers.Name == currentMetadata.SchemaName)) {
CodeMemberProperty codeProperty = (CodeMemberProperty)codeMembers;
if (codeProperty.HasSet) {
if (attributeMetadata.AttributeType != null && (attributeMetadata.AttributeType.Value == AttributeTypeCode.Picklist || attributeMetadata.AttributeType.Value == AttributeTypeCode.Status)) {
if (codeProperty.SetStatements[1].GetType() == typeof(CodeConditionStatement)) {
((CodeConditionStatement)codeProperty.SetStatements[1]).FalseStatements[0] = new CodeSnippetStatement {
Value = String.Format("this.SetAttributeValue(\"{0}\", new Microsoft.Xrm.Sdk.OptionSetValue(value.Value));", attributeMetadata.LogicalName)
};
}
else {
codeProperty.SetStatements[1] = new CodeSnippetStatement(String.Format("this.SetAttributeValue(\"{0}\", new Microsoft.Xrm.Sdk.OptionSetValue(value.Value));", attributeMetadata.LogicalName));
}
}
}
}
}
}
#endregion
}
}
CrmSvcUtil_run.cmd Command Batch File:
#echo off
set url=https://[organization].api.crm.dynamics.com/XRMServices/2011/Organization.svc
echo.
echo Generating CrmSvcUtil Proxy class in output folder
echo.
CrmSvcUtil.exe /metadataproviderservice:"MetadataProvider.IfdMetadataProviderService,
MetadataProvider"
/url:https://[organization].api.crm.dynamics.com/XRMServices/2011/Organization.svc /out:Xrm.cs
/namespace:Xrm /serviceContextName:XrmServiceContext /serviceContextPrefix:Xrm
/u:[username] /p:[password]
/codewriterfilter:SvcUtilFilter.CodeWriterFilter,SvcUtilFilter
/codecustomization:SvcUtilFilter.CustomizeCodeDomService,SvcUtilFilter
echo.
pause
filter.xml
<filter>
<entities>
<entity>systemuser</entity>
<entity>team</entity>
<entity>role</entity>
<entity>businessunit</entity>
<entity>account</entity>
<entity>product</entity>
<entity>transactioncurrency</entity>
</entities>
</filter>
It looks like there was a bug in the crmsrvcutil that has since been fixed. My code for OptionSet properties now looks like this:
[Microsoft.Xrm.Sdk.AttributeLogicalNameAttribute("prioritycode")]
public Microsoft.Xrm.Sdk.OptionSetValue PriorityCode
{
get
{
return this.GetAttributeValue<Microsoft.Xrm.Sdk.OptionSetValue>("prioritycode");
}
set
{
this.OnPropertyChanging("PriorityCode");
this.SetAttributeValue("prioritycode", value);
this.OnPropertyChanged("PriorityCode");
}
}
And I get no error setting the OptionSetValue...

Issue with Updating Changes in LINQ

I'm having an issue with updating the database. The app shows the updated value, but the database does not. No errors returned. My table has a PK. Using DotConnect for Oracle, but the LINQ syntax is the same.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataContext DB =
new DataContext("User Id=...Password=...;Server=...;");
DB.Log = Console.Out;
Console.WriteLine();
IEnumerable<Foodorder> fo = from f in DB.Foodorders
where f.Orderid == 10210
select f;
foreach (Foodorder food in fo)
{
Console.WriteLine(food.Orderid + " " + food.Externalref
+ "\r\n " + food.Orderremarks);
}
Console.Read();
//Try a new connection (eliminate caching)
DB.Dispose();
DataContext DB2 = new DataContext("User Id=...Password=...;Server=...;");
Foodorder fo2 = DFunc.GetFoodOrder(10198);
fo2.Orderremarks = "This should save now.";
fo2.Orderqty = 9999;
DB.SubmitChanges();
//Retrieves a single order: DFunc.GetFoodOrder(pk)
Console.WriteLine(DFunc.GetFoodOrder(10198).Orderremarks);
Console.Read();
}
}
}
The console reads the correct updated values, but the DB Does Not Update.
The data function, DFunc.GetFoodOrder(Oid) and data context functions are below. The are both in another assembly as the LINQ DAL:
#region Data Context
private static CommoDTContext cdtDataContext = new CommoDTContext(connectionSTringHere);
/// <summary>
/// This property gets the DevArt Oracle DotConnect data context,
/// providing LINQ to Oracle, and direct ORM capabilities.
/// </summary>
public static CommoDTContext DB
{
get
{
return cdtDataContext;
}
set
{
cdtDataContext = value;
}
}
#endregion Data Context
/// <summary>
/// Get food order by specifying the order ID.
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
public static Foodorder GetFoodOrder(decimal orderId)
{ //left out validation/try-catch for brevity.
if (orderId == 0) return null;
var food =
from fo in DB.Foodorders
where fo.Orderid == orderId
select fo;
if (food.FirstOrDefault() == null)
return null;
else
return food.FirstOrDefault();
}
You don't show how DFunc.GetFoodOrder(10198) is implemented. However, it doesn't seem to have access to the data context. You also submit changes against DB after disposing. I think you meant to submit against DB2.
In order for LINQ to do updates, the record has to be "attached" to the data context. If you query via the DataContext, the record will be attached and LINQ will track changes. Try...
FoodOrder fo2 = DB2.Foodorders.Single(x => x.Orderid == 10198);
fo2.Orderremarks = ...
DB2.SubmitChanges();
Here's how I modified my program based on Rob's answer.
class Program
{
static void Main(string[] args)
{
string outFmt = "{0} {1} of {2}\r\n {3}";
CommoDT.Context.CommoDTContext DB =
new CommoDT.Context.CommoDTContext(PutOracleConnectionStringHere);
DB.Log = Console.Out;
Console.WriteLine();
Foodorder fo2 = DB.Foodorders.Single(x => x.Orderid == 10198);
fo2.Orderremarks = "These are the first comments.";
fo2.Orderqty = 1000;
DB.SubmitChanges();
Console.WriteLine(outFmt,
fo2.Orderid.ToString(), fo2.Orderqty.ToString(), fo2.Externalref, fo2.Orderremarks);
Console.Read();
Foodorder fo3 = DFunc.GetFoodOrder(ref DB, 10198);
fo3.Orderremarks += " And these are the second comments for the order.";
fo3.Orderqty = 2000;
DB.SubmitChanges();
Console.WriteLine(outFmt,
fo3.Orderid.ToString(), fo3.Orderqty.ToString(), fo3.Externalref, fo3.Orderremarks);
Console.Read();
DB.Dispose();
}
}
/// <summary>
/// Get food order by specifying the order ID.
/// </summary>
/// <param name="orderId"></param>
/// <returns>Food</returns>
public static Foodorder GetFoodOrder(ref CommoDT.Context.CommoDTContext DB, decimal orderId)
{
if (orderId == 0) return null;
var food =
from fo in DB.Foodorders
where fo.Orderid == orderId
select fo;
if (food.FirstOrDefault() == null)
return null;
else
return food.FirstOrDefault();
}

Resources