写入C#中的事件

时间:2013-04-15 18:27:50

标签: c# asp.net sdk dynamics-crm dynamics-crm-4

我正在使用CRM 4和SDK来抓住这样的案例:

public List<Case> GetCases()
    {
        List<Case> cases = new List<Case>();


            #region Retrieve Resolved Cases
        try
        {

            InitSession();

            RetrieveMultipleRequest req = new RetrieveMultipleRequest();
            req.ReturnDynamicEntities = true;

            //QueryExpression says what entity to retrieve from, what columns we want back and what criteria we use for selection
            QueryExpression qe = new QueryExpression();
            qe.EntityName = EntityName.incident.ToString();
            List<string> attributes = new string[] { 
            "incidentid","title" ,"description", "ticketnumber", "statuscode",
            "kez_allocatedhours",
            "customerid",
            "casetypecode"

            }.ToList();

            //columns to retireve 
            ColumnSet AvailabilityColumnSet = new ColumnSet();
            AvailabilityColumnSet.Attributes = attributes.ToArray();
            qe.ColumnSet = AvailabilityColumnSet;

            //filter
            FilterExpression fe = new FilterExpression();
            fe.FilterOperator = LogicalOperator.And;

            //condtion for filter
            ConditionExpression isResolved = new ConditionExpression();
            isResolved.AttributeName = "statuscode";
            isResolved.Operator = ConditionOperator.NotEqual;
            isResolved.Values = new string[] { "5" };

            fe.Conditions = new ConditionExpression[] { isResolved }; //Add the conditions to the filter
           qe.Criteria = fe; //Tell the query what our filters are
            req.Query = qe; //Tell the request the query we want to use

            //retrieve entities
            RetrieveMultipleResponse resp = svc.Execute(req) as RetrieveMultipleResponse;
            if (resp != null)
            {
                BusinessEntity[] rawResults = resp.BusinessEntityCollection.BusinessEntities;
                List<DynamicEntity> castedResults = rawResults.Select(r => r as DynamicEntity).ToList();

                foreach (DynamicEntity result in castedResults)
                {
                    string id = GetProperty(result, "incidentid");
                    string title = GetProperty(result, "title");
                    string description = GetProperty(result, "description");
                    string ticket = GetProperty(result, "ticketnumber");
                    string customer = GetProperty(result, "customerid");
                    int statuscode = -1;
                    string statusname = "";
                    double estHours = 0.0;
                    string casetype = "";
                    int casetypecode = -1;

                    Property prop = result.Properties.Where(p => p.Name == "statuscode").FirstOrDefault();
                    if (prop != null)
                    {
                        StatusProperty status = prop as StatusProperty;

                        if (status != null)
                        {
                            statuscode = status.Value.Value;
                            statusname = status.Value.name;
                        }
                    }

                    prop = result.Properties.Where(p => p.Name == "kez_allocatedhours").FirstOrDefault();

                    if (prop != null)
                    {
                        CrmFloatProperty fl = prop as CrmFloatProperty;

                        if (fl != null)
                        {
                            estHours = fl.Value.Value;
                        }
                    }

                    prop = result.Properties.Where(p => p.Name == "casetypecode").FirstOrDefault();

                    if (prop != null)
                    {
                        PicklistProperty fl = prop as PicklistProperty;

                        if (fl != null)
                        {
                            casetype = fl.Value.name;
                            casetypecode = fl.Value.Value;
                        }
                    }

                    Case c = new Case();
                    c.ID = id;
                    c.Title = title;
                    c.Description = description;
                    c.StatusCode = statuscode;
                    c.StatusName = statusname;
                    c.TicketNumber = ticket;
                    c.CustomerName = customer;
                    c.EstimatedHours = estHours;
                    c.Type = casetype;
                    c.TypeCode = casetypecode;

                    bool allowedThroughStat = true;
                    bool allowedThroughType = true;
                    var userStatuses = SettingsManager.Get("CRMUserStatusReasons").Split(';').ToList().Where(p => p.Length > 0).ToList();
                    var userTypes = SettingsManager.Get("CRMUserCaseTypes").Split(';').ToList().Where(p => p.Length > 0).ToList();

                    if(userStatuses.Count > 0 && !userStatuses.Contains(c.StatusCode.ToString()))
                    {
                        allowedThroughStat = false;
                    }

                    if (userTypes.Count > 0 && !userTypes.Contains(c.TypeCode.ToString()))
                    {
                        allowedThroughType = false;
                    }

                    if(allowedThroughStat && allowedThroughType)
                    cases.Add(c);
                }

            }

        }// end try
        catch (Exception)
        {
            return null;
            // The variable 'e' can access the exception's information.
            // return "Error Message: " + e.Message.ToString() + " | Stack Trace: " + e.StackTrace.ToString();
        }

        return cases;

            #endregion
    }

但是,现在我需要能够在C#的情况下更改案件的状态和标题。

我看过SDK文档但找不到这个例子。

之前有人使用过吗?

由于

简单地说,上面是阅读事件的代码。我可以举一个编写事件字段的例子吗?例如:我怎样才能更改事件的标题。

2 个答案:

答案 0 :(得分:1)

您可以在CrmService上调用Update方法。这是SDK article

Case c = new Case();
c.ID = id;
c.Title = title;
svc.Update(c); 

答案 1 :(得分:0)

要更改实体的状态,请使用setstaterequest。如果您想对动态实体执行此操作,则会有描述in this blog