К сожалению все таки не работает DefaultCredentials. Я что-то упустил и подумал что настройки конфига сработали. На самом деле как работало через crmService.Credentials = new System.Net.NetworkCredential("administrator", "pass@word1"); так и работает. Есть какой-нить другой способ передать информацию о пользователе св-ву crmService.Credentials? Почему у всех людей работает а у меня нет? Конфиг приложил......
Код сохранения
Код:
protected void btnSave_Click(object sender, Coolite.Ext.Web.AjaxEventArgs e)
{
if (ValidateActivityDate())
{
using (new CrmImpersonator())
{
string sOrgname = string.Empty;
sOrgname = Request.Params["orgname"];
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = sOrgname;
token.AuthenticationType = 0;
//Create the Service
CrmService crmService = new CrmService();
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
//crmService.Credentials = new System.Net.NetworkCredential("administrator", "pass@word1");
//crmService.Credentials = WindowsIdentity.GetCurrent();
crmService.PreAuthenticate = false;
crmService.CrmAuthenticationTokenValue = token;
crmService.Url = (string)(Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\MSCRM").GetValue("ServerUrl")) + "/2007/crmservice.asmx";
//
string sType = string.Empty;
string sLeadId = string.Empty;
if (Request.Params["typename"] != null && Request.Params["id"] != null)
{
sType = Request.Params["typename"];
sLeadId = Request.Params["id"];
Microsoft.Crm.Sdk.Query.ColumnSet cols = new Microsoft.Crm.Sdk.Query.ColumnSet();
cols.Attributes.Add("subject");
Guid gLeadId = new Guid(sLeadId);
lead oLead = (lead)crmService.Retrieve(sType, gLeadId, cols);
//
Guid userid = new Guid();
WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse currentUser = (WhoAmIResponse)crmService.Execute(userRequest);
userid = currentUser.UserId;
task task = new task();
// Set the task subject.
task.subject = txtNotes.Text;
task.scheduledstart = new CrmDateTime();
task.scheduledstart.Value = txtActivity.SelectedDate.ToString("s");
task.actualdurationminutes = new CrmNumber();
task.actualdurationminutes.Value = Convert.ToInt32(ddDuration.SelectedItem.Value);
//Set the regardingobject
task.regardingobjectid = new Lookup();
task.regardingobjectid.Value = oLead.leadid.Value;
task.regardingobjectid.type = EntityName.lead.ToString();
//Set the user
task.ownerid = new Owner();
task.ownerid.Value = userid;
task.ownerid.type = EntityName.systemuser.ToString();
//set created by
task.createdby = new Lookup();
task.createdby.Value = userid;
task.createdby.type = EntityName.systemuser.ToString();
Guid gTSKId = new Guid();
try
{
gTSKId = crmService.Create(task);
lblError.Text = string.Empty;
lblInfo.Text = "\"" + task.subject + "\" Task Added.";
}
catch (System.Web.Services.Protocols.SoapException ex)
{
lblInfo.Text = string.Empty;
lblError.Text += ex.Detail.InnerText + userid.ToString() + " " + sOrgname;
}
}
}
}
}