Dynamic CRM 4.0 SOAP exception - soap

When I try to develop CRM stuff, using the code below:
public static CrmService GetCrmService()
{
//Standard CRM setup
var authenticationToken = new CrmAuthenticationToken();
//Using the active directory
authenticationToken.AuthenticationType = 0;
authenticationToken.OrganizationName = "myorganizationName";
var crmService = new CrmService();
crmService.PreAuthenticate = true;
crmService.UseDefaultCredentials = false;
crmService.CrmAuthenticationTokenValue = authenticationToken;
crmService.Credentials = new NetworkCredential("username", "password", "domain");
crmService.Url = "http://<ourserver>/mscrmservices/2007/CrmServiceWsdl.aspx";
return crmService;
}
public void RetrieveAccount()
{
var accountGuid = new Guid("088FFC38-8285-4AF5-8E36-84BAD6B268ED");
var crmService = GetCrmService();
//Set the column to return
var returnedColumns = new ColumnSet();
returnedColumns.Attributes = new string[] { "name", "telephone1", "customertypecode" };
try
{
var receivedAccount = crmService.Retrieve(EntityName.account.ToString(), accountGuid, returnedColumns) as account;
if (receivedAccount != null)
{
Debug.WriteLine(string.Format("Name: {0}, Telephone: {1}", receivedAccount.name, receivedAccount.telephone1));
}
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
}
}
It comes up with the following exception:
"Possible SOAP version mismatch:
Envelope namespace
http://schemas.xmlsoap.org/wsdl/ was
unexpected. Expecting
http://schemas.xmlsoap.org/soap/envelope/."
Is there any solution for this problem ?
Thanks in advance.

After spending several hours, I have figured out that the problem was occured because of using the wrong service address. Instead of connecting to the actual address, I have connected to a redirected address.
So in the above code, I used the redirected address:
https://servername/MSCrmServices/2007/CrmServiceWsdl.aspx
I should have been used the following address:
https://servername/mscrmservices/2007/crmservice.asmx
Cheers.

Related

Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication. in Entity Framework

getting {"Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication."} please give any suggetion why this error is throwing.
public int Find(string AccountNumber, DateTime DepositedDT)
{
IsPigmySync pigmySync = new IsPigmySync();
pigmySync.AccountNumber = AccountNumber;
pigmySync.DepositedDT = DepositedDT;
SqlParameter issynced = new SqlParameter("#p2", System.Data.SqlDbType.Int);
issynced.Direction = ParameterDirection.Output;
try
{
var sql = "exec Pigmy_GetPigmyItems #p0,#p1,#p2 OUT";
// var result = _context.Database.ExecuteSqlInterpolated(sqlQuery);
var result = _context.Database.ExecuteSqlCommand(sql, pigmySync.AccountNumber, pigmySync.DepositedDT, issynced);
int ab = result;
pigmySync.IsSynced = (int)issynced.Value;
return pigmySync.IsSynced;
}
catch (Exception ex)
{
return 0;
}
above is my code snippet. using entity framework,xamarin forms

Working on pre-operation plug-in to update "Modified By" field in MSCRM -- Need help fixing code

I am trying to update the "Modified By" field based on a text field called "Prepared By", which contains the name of a user. I've created a pre-operation plug-in to do this and believe I am close to done. However, the "Modified By" field is still not successfully getting updated. I am relatively new to coding and CRM, and could use some help modifying the code and figuring out how I can get this to work.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Linq;
namespace TimClassLibrary1.Plugins
{
public class CreateUpdateContact : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);
tracingService.Trace("Start plugin");
tracingService.Trace("Validate Target");
if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
return;
tracingService.Trace("Retrieve Target");
var target = (Entity)context.InputParameters["Target"];
String message = context.MessageName.ToLower();
SetCreatedByAndModifiedBy(tracingService, service, target, message);
}
private void SetCreatedByAndModifiedBy(ITracingService tracingService, IOrganizationService service, Entity target, string message)
{
tracingService.Trace("Start SetPriceList");
tracingService.Trace("Validate Message is Create or Update");
if (!message.Equals("create", StringComparison.OrdinalIgnoreCase) && !message.Equals("update", StringComparison.OrdinalIgnoreCase))
return;
tracingService.Trace("Retrieve Attributes");
var createdByReference = target.GetAttributeValue<EntityReference>("new_createdby");
var modifiedByReference = target.GetAttributeValue<EntityReference>("new_modifiedby");
tracingService.Trace("Retrieve And Set User for Created By");
RetrieveAndSetUser(tracingService, service, target, createdByReference, "createdby");
tracingService.Trace("Retrieve And Set User for Modified By");
RetrieveAndSetUser(tracingService, service, target, modifiedByReference, "modifiedby");
}
private void RetrieveAndSetUser(ITracingService tracingService, IOrganizationService service, Entity target, EntityReference reference, string targetAttribute)
{
tracingService.Trace("Validating Reference");
if (reference == null)
return;
tracingService.Trace("Retrieving and Validating User");
var user = RetrieveUserByName(service, reference.Name, new ColumnSet(false));
if (user == null)
return;
tracingService.Trace("Setting Target Attribute");
target[targetAttribute] = user.ToEntityReference();
}
private Entity RetrieveUserByName(IOrganizationService service, string name, ColumnSet columns)
{
var query = new QueryExpression
{
EntityName = "systemuser",
ColumnSet = columns,
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "fullname",
Operator = ConditionOperator.Equal,
Values = { name }
}
}
}
};
var retrieveResponse = service.RetrieveMultiple(query);
if (retrieveResponse.Entities.Count == 1)
{
return retrieveResponse.Entities.FirstOrDefault();
}
else
{
return null;
}
}
}
}
If you do get use from method Retreiveusernyname then you have to use below code
target[“modifiedby”] = new EntityRefrence(user.logicalname,user.id);
I don't see anything obviously wrong with your update, however you are taking a complicated and unnecessary step with your RetrieveUserByName() method. You already have EntityReference objects from your new_createdby and new_modifiedby fields, you can simply assign those to the target:
if (message.Equals("create", StringComparison.OrdinalIgnoreCase))
{
target["createdby"] = target["new_createdby];
}
else if (message.Equals("update", StringComparison.OrdinalIgnoreCase))
{
target["modifiedby"] = target["new_modifiedby];
}
If new_createdby and new_modifiedby are not entity references, then that would explain why your existing code does not work, if they are, then use my approach.

Autodiscover cannot process the given e-mail address. Only mailboxes and contacts are allowed

While sending email using ExchangeService is throwing Exception as Message is: "The Autodiscover service returned an error." and "Autodiscover cannot process the given e-mail address.
Only mailboxes and contacts are allowed".
public void SendEmail(string to, string cc, string subject, string body, string emailType)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
service.Credentials = new WebCredentials("test#outlook.com", "test");
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl("test#outlook.com", RedirectionUrlValidationCallback);
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add(to);
email.CcRecipients.Add(cc);
email.Subject = subject;
email.Body = new MessageBody(body);
email.Send();
}
private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
bool result = false;
Uri redirectionUri = new Uri(redirectionUrl);
if (redirectionUri.Scheme == "https")
{
result = true;
}
return true;
}
the above code is working fine with "...#microsoft.com" but not working with "...outlook.com"
can anyone help to resolve issue.

Spring MVC bulk message using MimeMessagePreparator

I'm trying to send bulk emails using Spring MVC. Here is my code:
if(customerClients != null){
int count = 0;
boolean sent = false;
List<MimeMessagePreparator> preparators = new ArrayList<MimeMessagePreparator>();
for (BulkEmail client : customerClients) {
if(customerID==-1)
customerID = client.getCustomerID();
CustomerAccount customerAccount = service.getCustomerAccount(client.getCustomerID());
if (instantMessage.isEmailChecked() && customerAccount.getBalance()>0) {
try{
final String receiverID = client.getEmail();
instantMessage.setIpersonOrGroupValue(receiverID);
preparators.add(new MimeMessagePreparator()
{
public void prepare(MimeMessage mimeMessage) throws Exception
{
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setSubject(instantMessage.getSubject());
message.setTo(receiverID);
message.setFrom(from);
message.setText(instantMessage.getMessage(), true);
}
});
sent = false;
}
catch(Exception ex){
ex.printStackTrace();
}
}
++count;
if(count%100==0){
springMail.send(preparators);
preparators = new ArrayList<MimeMessagePreparator>();
sent = true;
}
}
if(!sent)
springMail.send(preparators);
Here is the code that uses JavaMailSender for sending the preparators:
public void send(List<MimeMessagePreparator> preparatorsList) {
MimeMessagePreparator[] preparators = preparatorsList.toArray(new MimeMessagePreparator[preparatorsList.size()]);
mailSender.send(preparators);
}
The problem with this code is it takes around 1 second per email address. This means 300 emails in 5 minutes.
I want to know if this is normal or there is something I can do to improve.
Thanks

put the contact list of account to the cc field in email

i have a mission to make a plug-in in crm 4 which should 1. put in the subject field of an email the name of the account and then 2. put the contact list of account to the cc field of the email.
the first thing i did and it work, but the second... not so much...
i have seen some samples but none of them was close to halp me...
i would like to have help and explain of how to find the list of the contact that belong to the account and then put the list in the cc field.
here is the begining...:
namespace mail
{
public class Class1 : IPlugin
{
public void Execute(IPluginExecutionContext context)
{
DynamicEntity entity = null;
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is DynamicEntity)
{
entity = (DynamicEntity)context.InputParameters.Properties["Target"];
if (entity.Name != EntityName.account.ToString())
{
return;
}
}
else
{
return;
}
try
{
// updating the subject of the email
ICrmService service = context.CreateCrmService(true);
account accountRecord = (account)service.Retrieve("account", ((Key)entity.Properties["accountid"]).Value, new ColumnSet(new string[] { "name" }));
String str = String.Empty;
str = accountRecord.name.ToString();
DynamicEntity followup = new DynamicEntity();
followup.Name = EntityName.email.ToString();
followup.Properties = new PropertyCollection();
followup.Properties.Add(new StringProperty("subject", str));
//updating the CC of the email
TargetCreateDynamic targetCreate = new TargetCreateDynamic();
targetCreate.Entity = followup;
CreateRequest create = new CreateRequest();
create.Target = targetCreate;
CreateResponse created = (CreateResponse)service.Execute(create);
}
catch
{
throw new InvalidPluginExecutionException(
"An error occurred in the AccountUpdateHandler plug-in.");
}
}
}
}
You can try something like this,
List<BusinessEntity> contactList = GetNeededContacts(crmService, sourceEntity);
email targetEmail = GetTargetEmail(crmService, emailid);
foreach (DynamicEntity contact in contactList)
{
activityparty contActParty = new activityparty() { partyid = new Lookup("contact", contact.contactid.Value };
List<activityparty> tmpList = targetEmail.cc.ToList();
tmpList.Add(contActParty);
targetEmail.cc = tmpList.ToArray();
}
crmService.Update(targetEmail);
You only have to develop the function to get the contact, user or account references.