使用Webservice查询检索Dynamics CRM自定义字段

时间:2009-06-30 09:31:13

标签: c# asp.net dynamics-crm

我正在尝试从CRM安装中提取信息,到目前为止,这对于使用默认字段来说很好。但是我在检索自定义字段时遇到了困难,例如,Contacts有一个名为web_username的自定义字段。

我目前的代码是

        QueryExpression query = new QueryExpression();
        query.EntityName = "contact";
        ColumnSet cols = new ColumnSet();
        cols.Attributes = new string[] { "firstname", "lastname" };
        query.ColumnSet = cols;

        BusinessEntityCollection beReturned = tomService.RetrieveMultiple(query);
        foreach (contact _contact in beReturned.BusinessEntities)
        {
            DataRow dr = dt.NewRow();
            dr["firstname"] = _contact.firstname;
            dr["lastname"] = _contact.lastname;
            dt.Rows.Add(dr);
        }

如何在查询中包含自定义字段?我已经尝试过搜索但没有运气但我可能会错误地搜索,因为我不熟悉CRM术语。

提前干杯!

2 个答案:

答案 0 :(得分:6)

我已经能够解决这个问题了。如果它对任何人都有用,这就是我所做的。该查询设置如前,除了我已将自定义字段添加到ColumnSet。

cols.Attributes = new string[] { "firstname", "lastname", "new_web_username" };

然后使用RetrieveMultipleResponse和Request并将ReturnDynamicEntities设置为true

        RetrieveMultipleResponse retrived = new RetrieveMultipleResponse();

        RetrieveMultipleRequest retrive = new RetrieveMultipleRequest();
        retrive.Query = query;
        retrive.ReturnDynamicEntities = true;

        retrived = (RetrieveMultipleResponse)tomService.Execute(retrive);

如果我有更好的方法,请继续评论。

修改 如果您投射到联系人,请使用原始问题中的示例

contact myContact = (contact)myService.Retrieve(EntityName.contact.ToString(), userID, cols);

然后,您可以访问对象的属性

                phone = myContact.telephone1;
            password = myContact.new_password;

如果您更新了您在CRM中添加的CRM网络参考自定义字段,则可以使用

答案 1 :(得分:0)

private void thumb_MouseEnter(object sender, MouseEventArgs e)
{
    /*if (e.LeftButton == MouseButtonState.Pressed && e.MouseDevice.Captured == null)
    {
        MouseButtonEventArgs args = new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left);
        args.RoutedEvent = MouseLeftButtonDownEvent;
        (sender as Thumb).RaiseEvent(args);
    }*/
}

Point pStart;
bool isDragging;
private bool shiftDown;
private double startValue;

private void CMiXSlider_DragStarted(object sender, DragStartedEventArgs e)
{
    isDragging = true;
    pStart = Mouse.GetPosition(CMiXSlider);
    shiftDown = Keyboard.IsKeyDown(Key.LeftShift);
    startValue = Value;
}

private void CMiXSlider_DragDelta(object sender, DragDeltaEventArgs e)
{
    bool newShiftDown = Keyboard.IsKeyDown(Key.LeftShift);

    double scale = newShiftDown ? 0.5 : 1;

    Point pCurrent = Mouse.GetPosition(CMiXSlider);

    if (newShiftDown != shiftDown)
    {
        shiftDown = newShiftDown;
        pStart = pCurrent;
        startValue = Value;
    }

    Point pDelta = new Point(pCurrent.X - pStart.X, pCurrent.Y - pStart.Y);

    Value = startValue + (pDelta.X / CMiXSlider.ActualWidth) * scale;

    if (Value >= 1.0)
    {
        Value = 1.0;
    }
    else if (Value <= 0.0)
    {
        Value = 0.0;
    }
}

private void CMiXSlider_DragCompleted(object sender, DragCompletedEventArgs e)
{
    isDragging = false;
}

private void CMiXSlider_KeyDown(object sender, KeyEventArgs e)
{

    /*if (Keyboard.IsKeyDown(Key.LeftShift) && isDragging == true)
    {
        pStart = Mouse.GetPosition(CMiXSlider);
    }*/
}

//这里我提到了另一个功能:

    public List<Entity> GetEntitiesCollection(IOrganizationService service, string entityName, ColumnSet col)
  {
                        try
                        {
                            QueryExpression query = new QueryExpression
                            {
                                EntityName = entityName,
                                ColumnSet = col
                            };
                            var testResult = service.RetrieveMultiple(query);
                            var testResultSorted = testResult.Entities.OrderBy(x => x.LogicalName).ToList();

             foreach (Entity res in testResultSorted)
                        {
                            var keySorted = res.Attributes.OrderBy(x => x.Key).ToList();
                            DataRow dr = null;
                            dr = dt.NewRow();
                            foreach (var attribute in keySorted)
                            {
                                try
                                {
                                    if (attribute.Value.ToString() == "Microsoft.Xrm.Sdk.OptionSetValue")
                                    {
                                        var valueofattribute = GetoptionsetText(entityName, attribute.Key, ((Microsoft.Xrm.Sdk.OptionSetValue)attribute.Value).Value, _service);
dr[attribute.Key] = valueofattribute;


                                        }
                                        else if (attribute.Value.ToString() == "Microsoft.Xrm.Sdk.EntityReference")
                                        {
                                            dr[attribute.Key] = ((Microsoft.Xrm.Sdk.EntityReference)attribute.Value).Name;
                                        }
                                        else
                                        {
                                            dr[attribute.Key] = attribute.Value;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Response.Write("<br/>optionset Error is :" + ex.Message);
                                    }
                                }
                                dt.Rows.Add(dr);
                            }
                 return testResultSorted;
                 }
                        catch (Exception ex)
                        {
                            Response.Write("<br/> Error Message : " + ex.Message);
                            return null;
                        }
            }

//此函数的定义如下:

 var valueofattribute = GetoptionsetText(entityName, attribute.Key, ((Microsoft.Xrm.Sdk.OptionSetValue)attribute.Value).Value, _service);