20. Mai 2015 12:02
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Samples;
using System.ServiceModel;
namespace OPD.Crm2011.Gutschrift
{
public class GutschriftnummernVergabe : IPlugin
{
private OrganizationServiceProxy _serviceProxy;
private IOrganizationService _service;
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
try
{
ServerConnection serverConnect = new ServerConnection();
ServerConnection.Configuration serverConfig = serverConnect.GetServerConfiguration();
// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
// This statement is required to enable early-bound type support.
_serviceProxy.EnableProxyTypes();
_service = (IOrganizationService)_serviceProxy;
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName == "opd_gutschrift")
{
// Query using the paging cookie.
// Define the paging attributes.
// The number of records per page to retrieve.
int fetchCount = 1;
// Initialize the page number.
int pageNumber = 1;
// Define the condition expression for retrieving records.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "OPD_gutschrift_zaehler";
condition.Operator = ConditionOperator.NotNull;
// Define the order expression to retrieve the records.
OrderExpression order = new OrderExpression();
order.AttributeName = "OPD_gutschrift_zaehler";
order.OrderType = OrderType.Descending;
QueryExpression query = new QueryExpression();
query.EntityName = "opd_gutschrift";
query.Criteria.AddCondition(condition);
query.Orders.Add(order);
query.ColumnSet.AddColumns("name", "address1_stateorprovince", "emailaddress1", "accountid");
// Assign the pageinfo properties to the query expression.
query.PageInfo = new PagingInfo();
query.PageInfo.Count = fetchCount;
query.PageInfo.PageNumber = pageNumber;
// The current paging cookie. When retrieving the first page,
// pagingCookie should be null.
query.PageInfo.PagingCookie = null;
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;
int gutschriftnummer = 1000;
if (entityCollection.Count != 0)
{
foreach (OPD_gutschrift gutschrift in entityCollection)
{
gutschriftnummer = gutschrift.OPD_gutschrift_zaehler.Value + 1;
}
}
String gutschriftid = "GUT-01000-A7B809";
String legalCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String randomString = "";
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < 6; i++)
{
ch = legalCharacters[random.Next(0, legalCharacters.Length)];
builder.Append(ch);
}
randomString = builder.ToString();
if (gutschriftnummer < 10000)
{
gutschriftid = "GUT-0" + gutschriftnummer + "-" + randomString;
}
else
{
gutschriftid = "GUT-" + gutschriftnummer + "-" + randomString;
}
entity.Attributes.Add("OPD_gutschrift_zaehler", gutschriftnummer);
entity.Attributes.Add("OPD_Gutschrift_nummer", gutschriftid);
}
}
}
// Catch any service fault exceptions that Microsoft Dynamics CRM throws.
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
{
// You can handle an exception here or pass it back to the calling method.
throw;
}
}
}
}
}
20. Mai 2015 14:22
20. Mai 2015 14:30
20. Mai 2015 14:34
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
<ErrorCode>-2147220891</ErrorCode>
<ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<KeyValuePairOfstringanyType>
<d2p1:key>OperationStatus</d2p1:key>
<d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.</Message>
<Timestamp>2015-05-20T12:31:54.701961Z</Timestamp>
<InnerFault i:nil="true" />
<TraceText>
[OPD.Crm2011.Gutschrift: OPD.Crm2011.Gutschrift.GutschriftnummernVergabe]
[9599d077-ebfe-e411-9414-005056a7075f: OPD.Crm2011.Gutschrift.GutschriftnummernVergabe: Create of opd_gutschrift]
</TraceText>
</OrganizationServiceFault>
21. Mai 2015 08:51
21. Mai 2015 09:46
/// <summary>
/// Obtains the name and port of the server running the Microsoft Dynamics CRM
/// Discovery service.
/// </summary>
/// <returns>The server's network name and optional TCP/IP port.</returns>
protected virtual String GetServerAddress(out bool ssl)
{
ssl = false;
Console.Write("Enter a CRM server name and port [crm.dynamics.com]: ");
String server = Console.ReadLine();
if (server.EndsWith(".dynamics.com") || String.IsNullOrWhiteSpace(server))
{
ssl = true;
}
else
{
Console.Write("Is this server configured for Secure Socket Layer (https) (y/n) [n]: ");
String answer = Console.ReadLine();
if (answer == "y" || answer == "Y")
ssl = true;
}
return server;
}
21. Mai 2015 12:21
21. Mai 2015 13:03
26. Mai 2015 14:19
A proxy type with the name account has been defined by another assembly. Current type: Account, OPD.Crm2011.Gutschrift, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e4f917cabfaa499, Existing type: Account, OPD.Crm2011.Gutschrift, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e4f917cabfaa499
_orgProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior(Assembly.GetExecutingAssembly()));
Der Wert konnte der Sammlung nicht hinzugefügt werden, da die Sammlung bereits ein Objekt vom gleichen Typ enthält: "Microsoft.Xrm.Sdk.Client.ProxyTypesBehavior". Diese Instanz unterstützt nur jeweils eine Instanz von jedem Typ.
Parametername: item
26. Mai 2015 14:54