Показать сообщение отдельно
Старый 08.05.2009, 11:59   #20  
Elka is offline
Elka
Участник
Аватар для Elka
 
431 / 22 (1) +++
Регистрация: 02.12.2008
Адрес: г. Ростов-на-Дону
"для отладки - необходимо атачиться к w3p процессу." А это как? (стыдно, но правда не знаю.. )


Код плагина:

using System;
using System.Collections.Generic;
using Microsoft.Win32;
using Microsoft.Crm.Sdk.Query;
// Microsoft Dynamics CRM namespaces
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using System.Text;
namespace Crm.Plugins
{
publicclassMyPlugin : IPlugin
{
// Configuration information that can be passed to a plug-in at run-time.
privatestring _secureInformation;
privatestring _unsecureInformation;
// Note: Due to caching, Microsoft Dynamics CRM does not invoke the plug-in
// contructor every time the plug-in is executed.
// Related SDK topic: Writing the Plug-in Constructor
public MyPlugin(string unsecureInfo, string secureInfo)
{
_secureInformation = secureInfo;
_unsecureInformation = unsecureInfo;
}
// Related SDK topic: Writing a Plug-in
publicvoid Execute(IPluginExecutionContext context)
{
DynamicEntity entity = null;
// Check if the InputParameters property bag contains a target
// of the current operation and that target is of type DynamicEntity.
if (context.InputParameters.Properties.Contains(ParameterName.Target) &&
context.InputParameters.Properties[ParameterName.Target] isDynamicEntity)
{
// Obtain the target business entity from the input parmameters.
entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
// TODO Test for an entity type and message supported by your plug-in.
// if (entity.Name != EntityName.account.ToString()) { return; }
// if (context.MessageName != MessageName.Create.ToString()) { return; }
}
else
{
return;
}
try
{
// Create a Microsoft Dynamics CRM Web service proxy.
// TODO Uncomment or comment out the appropriate statement.
// For a plug-in running in the child pipeline, use this statement.
// CrmService crmService = CreateCrmService(context, true);
// For a plug-in running in the parent pipeline, use this statement.
ICrmService crmService = context.CreateCrmService(true);
//Їлв обм Ї®«гзЁвм §­ 祭ЁҐ Ё§ ¬®ҐЈ® Ї®«п new_esum ( вЁЇ money)---
//
decimal sum = ((CrmMoney)entity.Properties["new_esum"]).Value;
String new_productname = "";

//бзЁвлў о §­ 祭ЁҐ Їа®¤гЄв
if (entity.Properties.Contains("productid"))
{
ColumnSet cs = newColumnSet();
Lookup regarding = (Lookup)entity.Properties["productid"];
cs.Attributes.Add("name");
product _product = (product)crmService.Retrieve(EntityName.product.ToString(), regarding.Value, cs);
if (_product == null || _product.name == null)
{
return;
}
else
{
new_productname = _product.name.ToString();
}
}
//Їлв обм ­ ©вЁ ­г¦­го Ї®§ЁжЁо Їа ©б «Ёбв
CrmService service = newCrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Create the query object.
QueryByAttribute query = newQueryByAttribute();
query.ColumnSet = newAllColumns();
query.EntityName = EntityName.productpricelevel.ToString();
// The query will retrieve all productpricelevel whose productidname is new_productname.
query.Attributes = newstring[] { "productidname" };
query.Values = newstring[] { new_productname };
// Execute the retrieval.
// п в Є Ї®­Ё¬ о Ї®«гзЁ« ўбҐ Ї®§ЁжЁЁ Їа ©б «Ёбв б Їа®¤гЄв ¬Ё new_productname
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);
// е®зг ®Ў­®ўЁвм §­ 祭ЁҐ ў Їа ©-«ЁбвҐ--- §¤Ґбм в®з­® ­Ґ в Є..­Ґ §­ о Є Є ЇҐаҐЎа вм §­ 祭Ёп...
// ((((


foreach (DynamicEntity product in retrieved.BusinessEntities)
{
product["amount"] = newCrmDecimal(sum);
service.Update(product);
}







// TODO Plug-in business logic goes here.
}
catch (System.Web.Services.Protocols.SoapException ex)
{
thrownewInvalidPluginExecutionException(
String.Format("An error occurred in the {0} plug-in.",
this.GetType().ToString()),
ex);
}
}
#region Private methods
///<summary>
/// Creates a CrmService proxy for plug-ins that execute in the child pipeline.
///</summary>
///<param name="context">The execution context that was passed to the plug-ins Execute method.</param>
///<param name="flag">Set to True to use impersontation.</param>
///<returns>A CrmServce instance.</returns>
privateCrmService CreateCrmService(IPluginExecutionContext context, Boolean flag)
{
CrmAuthenticationToken authToken = newCrmAuthenticationToken();
authToken.AuthenticationType = 0;
authToken.OrganizationName = context.OrganizationName;
// Include support for impersonation.
if (flag)
authToken.CallerId = context.UserId;
else
authToken.CallerId = context.InitiatingUserId;
CrmService service = newCrmService();
service.CrmAuthenticationTokenValue = authToken;
service.UseDefaultCredentials = true;
// Include support for infinite loop detection.
CorrelationToken corToken = newCorrelationToken();
corToken.CorrelationId = context.CorrelationId;
corToken.CorrelationUpdatedTime = context.CorrelationUpdatedTime;
corToken.Depth = context.Depth;
RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");
service.Url = String.Concat(regkey.GetValue("ServerUrl").ToString(), "/2007/crmservice.asmx");
service.CorrelationTokenValue = corToken;
return service;
}
///<summary>
/// Creates a MetadataService proxy for plug-ins that execute in the child pipeline.
///</summary>
///<param name="context">The execution context that was passed to the plug-ins Execute method.</param>
///<param name="flag">Set to True to use impersontation.</param>
///<returns>A MetadataServce instance.</returns>
privateMetadataService CreateMetadataService(IPluginExecutionContext context, Boolean flag)
{
CrmAuthenticationToken authToken = newCrmAuthenticationToken();
authToken.AuthenticationType = 0;
authToken.OrganizationName = context.OrganizationName;
// Include support for impersonation.
if (flag)
authToken.CallerId = context.UserId;
else
authToken.CallerId = context.InitiatingUserId;
MetadataService service = newMetadataService();
service.CrmAuthenticationTokenValue = authToken;
service.UseDefaultCredentials = true;
RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");
service.Url = String.Concat(regkey.GetValue("ServerUrl").ToString(), "/2007/metadataservice.asmx");
return service;
}
#endregion Private Methods
}
}

Последний раз редактировалось Elka; 08.05.2009 в 12:09.