using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.CSharp;
using System.Threading;
using SilverlightApplication2.ServiceReference1;
using System.Data.Services.Client;
namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
_syncContext = SynchronizationContext.Current;
InitializeCRMServiceContext();
LoadForecast();
}
private SynchronizationContext _syncContext;
private CRM2013VanillaContext _context;
private String _serverURL;
private void Button_Click_1(object sender, RoutedEventArgs e)
{
InitializeCRMServiceContext();
LoadForecast();
}
private void InitializeCRMServiceContext()
{
// Get the ServerUrl (ServerUrl is formatted differently OnPremise than
// OnLine).
_serverURL = ServerUtility.GetServerUrl();
if (!String.IsNullOrEmpty(_serverURL))
{
// Setup context.
_context = new CRM2013VanillaContext(
new Uri(String.Format("{0}/xrmservices/2011/organizationdata.svc/",
_serverURL), UriKind.Absolute));
//This is important because if the entity has new
//attributes added the code will fail.
_context.IgnoreMissingProperties = true;
_context.MergeOption = MergeOption.PreserveChanges;
//Set defaults;
}
else
{
//lblError.Content = "unable to connect to crm server.";
}
}
private void LoadForecast()
{
//Get Engagement Record ID
string recordID = ((string)HtmlPage.Window.Eval("Xrm.Page.data.entity.getId()")).Replace("{", "").Replace("}", "");
//
MessageBox.Show(recordID);
DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)_context.new_testSet.AddQueryOption("$select", "new_name,new_Age");
// DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)_context.new_testSet.AddQueryOption("$select", "new_name eq 'ddddddd'");
//DataServiceQuery<new_forecast> query = (DataServiceQuery<new_forecast>)_context.new_forecastSet;
/// DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)_context.new_testSet.Where(a => a.new_testId == new Guid(recordID)
//).Select(a=>new{rt=a.new_Age,tf=a.new_name});
// DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)_context.new_testSet.AddQueryOption("$select", "new_name,new_name");
// DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)
//_context.new_testSet.AddQueryOption("$filter", "new_testId eq guid'" + recordID + "'");
// // queryContact.BeginExecute(RetrieveContactHandler, queryContact);
query.BeginExecute(this.LoadForecastCallback, query);
}
#region Callbacks
private void LoadForecastCallback(IAsyncResult asyncResult)
{
// DataServiceQuery<new_test> query1 = (DataServiceQuery<new_test>)_context.new_testSet.AddQueryOption("$select", "new_name,new_Age").Where<new_test>(a => (a.new_name == "ddddddd"));
//Label.Content = "Message: successful call back";
DataServiceQuery<new_test> query = asyncResult.AsyncState as DataServiceQuery<new_test>;
List<new_test> r = new DataServiceCollection<new_test>(query.EndExecute(asyncResult)).ToList();
// MessageBox.Show(r.Count.ToString());
//var list = (query.EndExecute(asyncResult)).ToList();
demo.ItemsSource = (from e in r select new { name = e.new_name, age = e.new_Age }).ToList();
//r.Select(p=>p.new_name,new_ag);
// silvergrid.DataContext = list;
}
private void SaveChangesCallback(IAsyncResult asyncResult)
{
_context = asyncResult.AsyncState as CRM2013VanillaContext;
var response = _context.EndSaveChanges(asyncResult);
}
#endregion
private void demo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.CSharp;
using System.Threading;
using SilverlightApplication2.ServiceReference1;
using System.Data.Services.Client;
namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
_syncContext = SynchronizationContext.Current;
InitializeCRMServiceContext();
LoadForecast();
}
private SynchronizationContext _syncContext;
private CRM2013VanillaContext _context;
private String _serverURL;
private void Button_Click_1(object sender, RoutedEventArgs e)
{
InitializeCRMServiceContext();
LoadForecast();
}
private void InitializeCRMServiceContext()
{
// Get the ServerUrl (ServerUrl is formatted differently OnPremise than
// OnLine).
_serverURL = ServerUtility.GetServerUrl();
if (!String.IsNullOrEmpty(_serverURL))
{
// Setup context.
_context = new CRM2013VanillaContext(
new Uri(String.Format("{0}/xrmservices/2011/organizationdata.svc/",
_serverURL), UriKind.Absolute));
//This is important because if the entity has new
//attributes added the code will fail.
_context.IgnoreMissingProperties = true;
_context.MergeOption = MergeOption.PreserveChanges;
//Set defaults;
}
else
{
//lblError.Content = "unable to connect to crm server.";
}
}
private void LoadForecast()
{
//Get Engagement Record ID
string recordID = ((string)HtmlPage.Window.Eval("Xrm.Page.data.entity.getId()")).Replace("{", "").Replace("}", "");
//
MessageBox.Show(recordID);
DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)_context.new_testSet.AddQueryOption("$select", "new_name,new_Age");
// DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)_context.new_testSet.AddQueryOption("$select", "new_name eq 'ddddddd'");
//DataServiceQuery<new_forecast> query = (DataServiceQuery<new_forecast>)_context.new_forecastSet;
/// DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)_context.new_testSet.Where(a => a.new_testId == new Guid(recordID)
//).Select(a=>new{rt=a.new_Age,tf=a.new_name});
// DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)_context.new_testSet.AddQueryOption("$select", "new_name,new_name");
// DataServiceQuery<new_test> query = (DataServiceQuery<new_test>)
//_context.new_testSet.AddQueryOption("$filter", "new_testId eq guid'" + recordID + "'");
// // queryContact.BeginExecute(RetrieveContactHandler, queryContact);
query.BeginExecute(this.LoadForecastCallback, query);
}
#region Callbacks
private void LoadForecastCallback(IAsyncResult asyncResult)
{
// DataServiceQuery<new_test> query1 = (DataServiceQuery<new_test>)_context.new_testSet.AddQueryOption("$select", "new_name,new_Age").Where<new_test>(a => (a.new_name == "ddddddd"));
//Label.Content = "Message: successful call back";
DataServiceQuery<new_test> query = asyncResult.AsyncState as DataServiceQuery<new_test>;
List<new_test> r = new DataServiceCollection<new_test>(query.EndExecute(asyncResult)).ToList();
// MessageBox.Show(r.Count.ToString());
//var list = (query.EndExecute(asyncResult)).ToList();
demo.ItemsSource = (from e in r select new { name = e.new_name, age = e.new_Age }).ToList();
//r.Select(p=>p.new_name,new_ag);
// silvergrid.DataContext = list;
}
private void SaveChangesCallback(IAsyncResult asyncResult)
{
_context = asyncResult.AsyncState as CRM2013VanillaContext;
var response = _context.EndSaveChanges(asyncResult);
}
#endregion
private void demo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
/////////////////////////////////////////
using System;
using System.Windows.Browser;
public static class ServerUtility
{
/// <summary>
/// Returns the ServerUrl from Microsoft Dynamics CRM
/// </summary>
/// <returns>String representing the ServerUrl or String.Empty if not found.</returns>
public static String GetServerUrl()
{
String serverUrl = String.Empty;
//Try to get the ServerUrl from the Xrm.Page object
serverUrl = GetServerUrlFromContext();
return serverUrl;
}
/// <summary>
/// Attempts to retrieve the ServerUrl from the Xrm.Page object
/// </summary>
/// <returns></returns>
private static String GetServerUrlFromContext()
{
try
{
// Silverlight hosted within HTML, this will get the server url
ScriptObject parent = (ScriptObject)HtmlPage.Window.GetProperty("parent");
ScriptObject xrm = (ScriptObject)parent.GetProperty("Xrm");
ScriptObject page = (ScriptObject)xrm.GetProperty("Page");
ScriptObject pageContext = (ScriptObject)page.GetProperty("context");
String serverUrl = (String)pageContext.Invoke("getServerUrl");
//The trailing forward slash character from CRM Online needs to be removed.
if (serverUrl.EndsWith("/"))
{
serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
}
return serverUrl;
}
catch
{
return String.Empty;
}
}
}
//</snippetServerUtility>