2013-04-15 53 views
0

我使用CRM 4和SDK搶的情況下,像這樣:寫事件在C#

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#改變給定的情況下的狀態和標題及其incidentid。

我看了一下SDK文檔,找不到這樣的例子。

以前有人用過這個嗎?

感謝

簡單地說,上面的代碼讀取事件。我能舉一個寫一個事件字段的例子,只有一個。例如:我怎樣才能改變事件的標題。

+2

你有什麼具體問題嗎? –

+0

@Robert我只需要知道如何寫一個字段,說標題,一個事件給定它的事件ID從C# – jmasterx

回答

1

您可以調用CrmService上的Update方法。這裏是SDK article

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

更改您使用setstaterequest的實體的狀態。如果你想做到動態實體有一個描述in this blog