简体   繁体   English

Dynamics CRM 2016(在线)-检索约会

[英]Dynamics CRM 2016 (Online) - Retrieve appointments

I'm trying to retrieve all the appointments from our CRM online environment using a LINQ query (I'm a newbie with programming). 我正在尝试使用LINQ查询从CRM在线环境中检索所有约会(我是编程的新手)。 Getting the appointment data is easily done but I also want to retrieve the Required Attendees for the appointments (this could be an account, contact eg) and fetch some Metadata (like the Name, E-mailadress for example) from the attendees. 获取约会数据很容易,但是我也想检索约会的必需与会者(这可以是一个帐户,例如,联系人),并从与会者那里获取一些元数据(例如姓名,电子邮件地址)。 Unfortunately it seems impossible to get this done and was hoping someone could help me with this. 不幸的是,似乎无法做到这一点,并希望有人可以帮助我。

public AppointmentData[] RetrieveActivities(bool persistChange)
    {
        var appointmentData = new List<AppointmentData>();

        using (var context = new FmServiceContext(_service))
        {
            var appointments = (from app in context.AppointmentSet
                join a in context.AccountSet on app.Account_Appointments.AccountId equals a.AccountId
                where app.StateCode != 0
                select new {app, a});


            foreach (var apappointments in appointments)
            {
                appointmentData.Add(new AppointmentData
                {
                    //this should be the list of required attendees
                    RequiredAttendees = new ActivityParty[]
                    {
                        Attendeename = apappointments.a.Name

                    },
                    //Appointment data
                    AppointmentType = apappointments.app.fm_Typeafspraak == null ? null : DataHelper.GetOptionSetValueLabel(apappointments.app.LogicalName, "fm_typeafspraak", apappointments.app.fm_Typeafspraak.Value, _service),
                    Subject = apappointments.app.Subject,
                    StartDate = apappointments.app.ScheduledStart,
                    EndDate = apappointments.app.ScheduledEnd,
                    Duration = apappointments.app.ScheduledDurationMinutes,
                    Location = apappointments.app.Location,
                    Description = apappointments.app.Description,
                    Priority = apappointments.app.PriorityCode == null ? null : DataHelper.GetOptionSetValueLabel(apappointments.app.LogicalName, "prioritycode", apappointments.app.PriorityCode.Value, _service),
                    Status = apappointments.app.StateCode.ToString()
                });
            }

        }
        return appointmentData.ToArray();
    }

I don't think you need the join as the activity party ids are already there in your query.. You may be hitting a limitation in join functionality here. 我认为您不需要加入,因为您的查询中已经有活动方ID。您可能在这里遇到了加入功能的限制。 Here's an approach: 这是一种方法:

foreach(var app in appointments)
{
    var requiredAttendees = app.RequiredAttendees.ToList();

    foreach(var ra in requiredAttendees)
    {
         // do whatever you want with each required attendee here, perform a separate retrieve for more details

    }
}

Also I recognize that this is an extra step. 我也意识到这是一个额外的步骤。 If you want to try to get your join to work I'd recommend going against the partyid instead. 如果您想尝试加入工作,建议您改用partyid。 If you're going to go that route you may need a nested query or more complex join since the relationship to ActivityParty is 1:N. 如果要走那条路线,您可能需要嵌套查询或更复杂的联接,因为与ActivityParty的关系是1:N。 Check out this link if you only care about the first required attendee: https://www.periscopedata.com/blog/4-ways-to-join-only-the-first-row-in-sql.html 如果您只关心第一个必需的与会者,请查看此链接: https : //www.periscopedata.com/blog/4-ways-to-join-only-the-first-row-in-sql.html

Solved it! 解决了! I did use Brendon's approach to do get it done. 我确实使用布伦登的方法来完成它。 First I queried all the appointments, 首先,我查询了所有约会,
Here's my code: 这是我的代码:

 public AppointmentData[] RetrieveAppointments(bool persistChange)
    {
        var appointmentData = new List<AppointmentData>();


        using (var context = new FmServiceContext(_service))
        {
            //First we get all relevant appointments
            var appointments = (from app in context.AppointmentSet
                                where app.StateCode != 0
                                select new { app});


            foreach (var appointment in appointments)
            {
               //we loop all the returned attendees of the appointments
                var attendees = new List<Attendee>();
                foreach (var attendee in appointment.app.RequiredAttendees)
                {
                    if (attendee.PartyId != null)
                    {
                        //if an attendee is an account
                        if (attendee.PartyId.LogicalName == "account")
                        {
                            var account = (from acc in context.AccountSet
                                where acc.AccountId == attendee.PartyId.Id
                                select new {acc});
                        }

                         //add the attendee
                          {
                            attendees.Add(new Attendee
                            {

                                // get additional metadata of the attendee
                            });
                        }
                      appointmentData.Add(new AppointmentData
                {
                    Attendees = attendees,
                    // get additional metadata of the appointment
                });
                    }
                }
             }
             return appointmentData.ToArray();
        }
    }
}

The result: a list of appointments with the a list of required attendees for that appointment. 结果是:约会列表以及该约会所需的与会者列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM