写Plugin时执行如下代码时报错,不知问题出在何处,请高手解决!
TargetCreateDynamic targetCreate = new TargetCreateDynamic(); //used to Create entity
CreateRequest createRequest = new CreateRequest(); //request to update entity
targetCreate.Entity = ShangjiEntity;
createRequest.Target = targetCreate;
server.Execute(createRequest);
谢谢了!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
using System.Collections;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
namespace CreateShangji
{
public class Shangji : IPlugin
{
public DataSet ConvertXMLToDataSet(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;
DataSet xmlDS = new DataSet();
try
{
stream = new StringReader(xmlData);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null)
reader.Close();
}
return xmlDS;
}
#region IPlugin 成员
//修改图书时创建销售商机,匹配对应的条件
public void Execute(IPluginExecutionContext context)
{
if (context.InputParameters.Properties.Contains("Target") && context.InputParameters.Properties["Target"] is DynamicEntity)
{
ICrmService server = context.CreateCrmService(true);
DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];
if (entity.Name == "ol_book")
{
string fetchXml = string.Empty;
if (entity.Properties.Contains("ol_barea") && entity.Properties["ol_sarea"] == null && entity.Properties["ol_school"] == null)
{
fetchXml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\"><entity name=\"ol_school\"><attribute name=\"ol_type\"/><attribute name=\"ol_difficulty\"/><attribute name=\"ol_schoolid\"/><attribute name=\"ol_area\"/><attribute name=\"ol_smallarea\"/><attribute name=\"ol_name\"/><order attribute=\"ol_type\" descending=\"false\"/><filter type=\"and\"><condition attribute=\"ol_area\" operator=\"eq\" value=\"" + ((Microsoft.Crm.Sdk.CrmReference)(entity.Properties["ol_barea"])).Value + "\"/></filter></entity></fetch>";
}
if (entity.Properties.Contains("ol_sarea") && entity.Properties["ol_sarea"] != null && entity.Properties["ol_school"] == null)
{
fetchXml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\"><entity name=\"ol_school\"><attribute name=\"ol_type\"/><attribute name=\"ol_difficulty\"/><attribute name=\"ol_schoolid\"/><attribute name=\"ol_area\"/><attribute name=\"ol_smallarea\"/><attribute name=\"ol_name\"/><order attribute=\"ol_type\" descending=\"false\"/><filter type=\"and\"><condition attribute=\"ol_smallarea\" operator=\"eq\" value=\"" + ((Microsoft.Crm.Sdk.CrmReference)(entity.Properties["ol_sarea"])).Value + "\"/></filter></entity></fetch>";
}
if (entity.Properties.Contains("ol_school") && entity.Properties["ol_school"] != null)
{
fetchXml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\"><entity name=\"ol_school\"><attribute name=\"ol_type\"/><attribute name=\"ol_difficulty\"/><attribute name=\"ol_schoolid\"/><attribute name=\"ol_area\"/><attribute name=\"ol_smallarea\"/><attribute name=\"ol_name\"/><order attribute=\"ol_type\" descending=\"false\"/><filter type=\"and\"><condition attribute=\"ol_schoolid\" operator=\"eq\" value=\"" + ((Microsoft.Crm.Sdk.CrmReference)(entity.Properties["ol_school"])).Value + "\"/></filter></entity></fetch>";
}
string result = server.Fetch(fetchXml);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(result);
//组合新XML
string NewXml = "<resultset>";
//获取resultset节点的所有子节点
XmlNodeList nodeList = xmlDoc.SelectSingleNode("resultset").ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
NewXml = NewXml + "<result>";
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)xn;
//ID
if (xe.SelectSingleNode("ol_schoolid") != null)
{
NewXml = NewXml + "<ol_schoolid>" + xe.SelectSingleNode("ol_schoolid").InnerText.ToString() + "</ol_schoolid>";
}
if (xe.SelectSingleNode("ol_type") != null)
{
NewXml = NewXml + "<ol_type>" + xe.SelectSingleNode("ol_type").InnerText.ToString() + "</ol_type>";
}
if (xe.SelectSingleNode("ol_difficulty") != null)
{
NewXml = NewXml + "<ol_difficulty>" + xe.SelectSingleNode("ol_difficulty").InnerText.ToString() + "</ol_difficulty>";
}
if (xe.SelectSingleNode("ol_area") != null)
{
NewXml = NewXml + "<ol_area>" + xe.SelectSingleNode("ol_area").InnerText.ToString() + "</ol_area>";
}
if (xe.SelectSingleNode("ol_smallarea") != null)
{
NewXml = NewXml + "<ol_smallarea>" + xe.SelectSingleNode("ol_smallarea").InnerText.ToString() + "</ol_smallarea>";
}
if (xe.SelectSingleNode("ol_name") != null)
{
NewXml = NewXml + "<ol_name>" + xe.SelectSingleNode("ol_name").InnerText.ToString() + "</ol_name>";
}
NewXml = NewXml + "</result>";
}
NewXml = NewXml + "</resultset>";
//将组合成的新XML转化为DS
DataSet ds = this.ConvertXMLToDataSet(NewXml);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string ShoolID = ds.Tables[0].Rows[i]["ol_schoolid"].ToString(); //学校
string SubjectFetchxml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\"><entity name=\"ol_subject\"><attribute name=\"ol_grade\"/><attribute name=\"ol_subject\"/><attribute name=\"ol_version\"/><attribute name=\"ol_subjectid\"/><attribute name=\"ol_teacherposting\"/><attribute name=\"ol_teacherbooking\"/><order attribute=\"ol_grade\" descending=\"false\"/><filter type=\"and\"><condition attribute=\"ol_school\" operator=\"eq\" value=\"" + ShoolID + "\"/></filter></entity></fetch>";
string Subjectresult = server.Fetch(SubjectFetchxml);
XmlDocument SubjectxmlDoc = new XmlDocument();
SubjectxmlDoc.LoadXml(Subjectresult);
//组合新XML
string SNewXml = "<resultset>";
//获取resultset节点的所有子节点
XmlNodeList SnodeList = SubjectxmlDoc.SelectSingleNode("resultset").ChildNodes;
//遍历所有子节点
foreach (XmlNode sxn in SnodeList)
{
SNewXml = SNewXml + "<result>";
//将子节点类型转换为XmlElement类型
XmlElement sxe = (XmlElement)sxn;
//ID
if (sxe.SelectSingleNode("ol_grade") != null)
{
SNewXml = SNewXml + "<ol_grade>" + sxe.SelectSingleNode("ol_grade").InnerText.ToString() + "</ol_grade>";
}
if (sxe.SelectSingleNode("ol_subject") != null)
{
SNewXml = SNewXml + "<ol_subject>" + sxe.SelectSingleNode("ol_subject").InnerText.ToString() + "</ol_subject>";
}
if (sxe.SelectSingleNode("ol_version") != null)
{
SNewXml = SNewXml + "<ol_version>" + sxe.SelectSingleNode("ol_version").InnerText.ToString() + "</ol_version>";
}
if (sxe.SelectSingleNode("ol_subjectid") != null)
{
SNewXml = SNewXml + "<ol_subjectid>" + sxe.SelectSingleNode("ol_subjectid").InnerText.ToString() + "</ol_subjectid>";
}
if (sxe.SelectSingleNode("ol_teacherposting") != null)
{
SNewXml = SNewXml + "<ol_teacherposting>" + sxe.SelectSingleNode("ol_teacherposting").InnerText.ToString() + "</ol_teacherposting>";
}
if (sxe.SelectSingleNode("ol_teacherbooking") != null)
{
SNewXml = SNewXml + "<ol_teacherbooking>" + sxe.SelectSingleNode("ol_teacherbooking").InnerText.ToString() + "</ol_teacherbooking>";
}
SNewXml = SNewXml + "</result>";
}
SNewXml = SNewXml + "</resultset>";
DataSet SubjectDS = this.ConvertXMLToDataSet(SNewXml);
if (SubjectDS.Tables[0].Rows.Count > 0)
{
for (int j = 0; j < SubjectDS.Tables[0].Rows.Count; j++)
{
string ol_subjectid = SubjectDS.Tables[0].Rows[j]["ol_subjectid"].ToString(); //
string ol_subject = SubjectDS.Tables[0].Rows[j]["ol_subject"].ToString(); //科目
string ol_grade = SubjectDS.Tables[0].Rows[j]["ol_grade"].ToString(); //年级
string ol_version = SubjectDS.Tables[0].Rows[j]["ol_version"].ToString(); //配套教材
string ol_teacherposting = SubjectDS.Tables[0].Rows[j]["ol_teacherposting"].ToString(); //邮寄对象
string ol_teacherbooking = SubjectDS.Tables[0].Rows[j]["ol_teacherbooking"].ToString(); //订书人
string ol_smallarea = ds.Tables[0].Rows[i]["ol_smallarea"].ToString(); //小区域
string ol_area = ds.Tables[0].Rows[i]["ol_area"].ToString(); //大区域
string ol_difficulty = ds.Tables[0].Rows[i]["ol_difficulty"].ToString(); //难易程度
string ol_type = ds.Tables[0].Rows[i]["ol_type"].ToString(); //学校类型
string shoolName = ds.Tables[0].Rows[i]["ol_name"].ToString(); //学校名称
bool typeiseq = false;
if (ol_type.Length > 0 && entity.Properties["ol_suitableschool"] != null)
{
int typeint = int.Parse(ol_type);
if (((Microsoft.Crm.Sdk.Picklist)(entity.Properties["ol_suitableschool"])).Value.ToString() == "1")
{
if (typeint >= 1 && typeint < 8)
{
typeiseq = true;
}
else
{
typeiseq = false;
}
}
if (((Microsoft.Crm.Sdk.Picklist)(entity.Properties["ol_suitableschool"])).Value.ToString() == "2")
{
if (typeint == 8)
{
typeiseq = true;
}
else
{
typeiseq = false;
}
}
if (((Microsoft.Crm.Sdk.Picklist)(entity.Properties["ol_suitableschool"])).Value.ToString() == "3")
{
if (typeint == 9)
{
typeiseq = true;
}
else
{
typeiseq = false;
}
}
}
bool barea = false;
if (entity.Properties["ol_barea"] != null && ol_area.Length > 0)
{
if (((Microsoft.Crm.Sdk.CrmReference)(entity.Properties["ol_barea"])).Value == new Guid(ol_area))
{
barea = true;
}
else
{
barea = false;
}
}
bool sarea = false;
if (entity.Properties["ol_sarea"] != null && ol_smallarea.Length > 0)
{
if (((Microsoft.Crm.Sdk.CrmReference)(entity.Properties["ol_sarea"])).Value == new Guid(ol_smallarea))
{
sarea = true;
}
else
{
sarea = false;
}
}
bool school1 = false;
if (entity.Properties["ol_school"] != null && ShoolID.Length > 0)
{
if (((Microsoft.Crm.Sdk.CrmReference)(entity.Properties["ol_school"])).Value == new Guid(ShoolID))
{
school1 = true;
}
else
{
school1 = false;
}
}
bool grade = false;
if (entity.Properties["ol_grade"] != null && ol_grade.Length > 0)
{
if (((Microsoft.Crm.Sdk.Picklist)(entity.Properties["ol_grade"])).Value.ToString() == ol_grade.ToString())
{
grade = true;
}
else
{
grade = false;
}
}
bool subject1 = false;
if (entity.Properties["ol_subject"] != null && ol_subject.Length > 0)
{
if (((Microsoft.Crm.Sdk.Picklist)(entity.Properties["ol_subject"])).Value.ToString() == ol_subject.ToString())
{
subject1 = true;
}
else
{
subject1 = false;
}
}
bool version = false;
if (entity.Properties["ol_version"] != null && ol_version.Length > 0)
{
if (((Microsoft.Crm.Sdk.Picklist)(entity.Properties["ol_version"])).Value.ToString() == ol_version.ToString())
{
version = true;
}
else
{
version = false;
}
}
bool difficulty = false;
if (entity.Properties["ol_difficulty"] != null && ol_difficulty.Length > 0)
{
if (((Microsoft.Crm.Sdk.Picklist)(entity.Properties["ol_difficulty"])).Value.ToString() == ol_difficulty.ToString())
{
difficulty = true;
}
else
{
difficulty = false;
}
}
if (typeiseq == true && barea == true && sarea == true && school1 == true && grade == true && difficulty == true && version == true && subject1 == true)
{
DynamicEntity ShangjiEntity = new DynamicEntity();
ShangjiEntity.Name = "ol_opportunity";
Guid ShangGuid = Guid.NewGuid();
KeyProperty shangjiID = new KeyProperty();
shangjiID.Name = "ol_opportunityid";
shangjiID.Value = new Key();
shangjiID.Value.Value = ShangGuid;
StringProperty name = new StringProperty();
name.Name = "ol_name";
name.Value = entity.Properties["ol_name"].ToString() + "在" + shoolName + "的销售商机";
//获取当前责任人,可以获取,不知如何添加到ShangjiEntity,因为它不是自定义的属性.
//OwnerProperty owner = new OwnerProperty();
//owner.Name = "ownerid";
//owner.Value = new Owner();
//owner.Value.Value = context.UserId;
LookupProperty book = new LookupProperty();
book.Name = "ol_book";
book.Value = new Lookup();
book.Value.Value = ((Microsoft.Crm.Sdk.Key)(entity.Properties["ol_bookid"])).Value;
LookupProperty school = new LookupProperty();
school.Name = "ol_school";
school.Value = new Lookup();
school.Value.Value = new Guid(ShoolID);
LookupProperty subject = new LookupProperty();
subject.Name = "ol_subject";
subject.Value = new Lookup();
subject.Value.Value = new Guid(ol_subjectid);
LookupProperty teacherposting = new LookupProperty();
teacherposting.Name = "ol_teacherposting";
teacherposting.Value = new Lookup();
teacherposting.Value.Value = new Guid(ol_teacherposting);
LookupProperty teacherbooking = new LookupProperty();
teacherbooking.Name = "ol_teacherbooking";
teacherbooking.Value = new Lookup();
teacherbooking.Value.Value = new Guid(ol_teacherbooking);
Microsoft.Crm.Sdk.CrmBooleanProperty sample = new CrmBooleanProperty();
sample.Name = "ol_sample";
sample.Value = new CrmBoolean();
sample.Value.Value = bool.Parse("false");
CrmBooleanProperty drawsample = new CrmBooleanProperty();
drawsample.Name = "ol_drawsample";
drawsample.Value = new CrmBoolean();
drawsample.Value.Value = bool.Parse("false");
CrmBooleanProperty postsample = new CrmBooleanProperty();
postsample.Name = "ol_postsample";
postsample.Value = new CrmBoolean();
postsample.Value.Value = bool.Parse("false");
CrmDecimalProperty possibility = new CrmDecimalProperty();
possibility.Name = "ol_possibility";
possibility.Value = new CrmDecimal();
possibility.Value.Value = 0;
PicklistProperty judgement = new PicklistProperty();
judgement.Name = "ol_judgement";
judgement.Value = new Picklist();
judgement.Value.Value = 3;
CrmMoneyProperty possibleincome = new CrmMoneyProperty();
possibleincome.Name = "ol_possibleincome";
possibleincome.Value = new CrmMoney();
possibleincome.Value.Value = 0;
ShangjiEntity.Properties = new Microsoft.Crm.Sdk.PropertyCollection();
ShangjiEntity.Properties.Add(name);
//ShangjiEntity.Properties.Add(owner);
ShangjiEntity.Properties.Add(book);
ShangjiEntity.Properties.Add(school);
ShangjiEntity.Properties.Add(subject);
ShangjiEntity.Properties.Add(teacherposting);
ShangjiEntity.Properties.Add(teacherbooking);
ShangjiEntity.Properties.Add(sample);
ShangjiEntity.Properties.Add(drawsample);
ShangjiEntity.Properties.Add(postsample);
ShangjiEntity.Properties.Add(possibility);
ShangjiEntity.Properties.Add(judgement);
ShangjiEntity.Properties.Add(possibleincome);
ShangjiEntity.Properties.Add(shangjiID);
TargetCreateDynamic targetCreate = new TargetCreateDynamic(); //used to Create entity
CreateRequest createRequest = new CreateRequest(); //request to update entity
targetCreate.Entity = ShangjiEntity;
createRequest.Target = targetCreate;
server.Execute(createRequest);
// .Properties = new Microsoft.Crm.Sdk.PropertyCollection();
}
}
}
}
}
}
#endregion
}
}
}
}
上面这个plugin是update操作!调试时执行 server.Execute(createRequest);报错!请MVP帮帮忙!