Обновление корпоративных настраиваемых полей, связанных с корпоративными ресурсами, в PWA Online - PullRequest
0 голосов
/ 03 сентября 2018

У меня есть требование обновлять корпоративные ресурсы в Project Online на основе условия, а также необходимо обновить настраиваемые пользовательские поля Entity='Resource', связанные с ним. Я не могу найти связь между ними.

Что я сделал до сих пор:

Извлеченные корпоративные ресурсы из PWA

.

using (ProjectContext projContext = new ProjectContext(projectUrl))
{
       SecureString securePassword = new SecureString();
       foreach (char character in password.ToCharArray())
                securePassword.AppendChar(character);

 projContext.Credentials = new SharePointOnlineCredentials(username, securePassword);
 enterpriseResources = projContext.LoadQuery(projContext.EnterpriseResources);
 projContext.ExecuteQuery();


       if (enterpriseResources.Count() > 0)
       {
                    foreach (EnterpriseResource resource in enterpriseResources)
                    {
                        if (!string.IsNullOrEmpty(resource.Email))
                        {
                          var enterpriseResource = projContext.EnterpriseResources.GetByGuid(resource.Id);
                         //var enterpriseResourceCustomFields = enterpriseResource.CustomFields.GetByGuid(resource.Id);

                            enterpriseResource.Email = "sahil@test.com";    //Email   
                            enterpriseResource.Name = "Sahil Sharma";      //FullName
                            //enterpriseResource.Id = "";                  //EmployeeID  [Can't update, readonly]


                            //These below mentioned items are Enterprise Custom Fields related to Enterprise Resources but I'm not able to get it
                            //Office
                            //Level
                            //SupervisorID
                            //Division
                            //Status
                            //Supervisor
                            //DepartmentNumber
                            //LaborCategory
                            //Function

                            projContext.EnterpriseResources.Update();
                            projContext.ExecuteQuery();

                        }

                    }
                }
            }

Что не сделали:

Не удалось получить пользовательские поля Enterprise, связанные с ним:

Я пробовал этот запрос, но не смог получить точные поля, упомянутые выше:

foreach (EnterpriseResource resource in enterpriseResources)
                    {
                        if (!string.IsNullOrEmpty(resource.Email))
                        {
                            var pId = Guid.Parse("a5c8801f-d505-e811-a745-b0359f8878e9");
                            var customProject = projContext.LoadQuery(projContext.Projects.Where(p => p.Id == pId).Include(
                                p => p.Id,
                                p => p.Name,
                                p => p.IncludeCustomFields,
                                p => p.IncludeCustomFields.CustomFields,
                                P => P.IncludeCustomFields.CustomFields.IncludeWithDefaultProperties(
                                lu => lu.LookupTable,
                                lu => lu.LookupEntries
                                ))
                            );

                            projContext.ExecuteQuery();

                            foreach (PublishedProject pubProj in customProject)
                            {
                                var projECFs = pubProj.IncludeCustomFields.CustomFields;
                                Dictionary<string, object> ECFValues = pubProj.IncludeCustomFields.FieldValues;

                            }
                        }
                    }

Любая идея поможет найти связь между ними и получить.

1 Ответ

0 голосов
/ 06 декабря 2018

Вам нужно:
1. Получить пользовательские поля из projContext.
2. Получите сопоставление «Отображаемое имя - Внутреннее имя».
3. Получите значение настраиваемого поля из FieldValues ​​по внутреннему имени настраиваемого поля.
4. Задайте значение настраиваемого поля для корпоративного ресурса.

Я думаю, ваш код должен быть таким:

projContext.Load(projContext.CustomFields);
projContext.ExecuteQuery();
var projectCustomFieldDisplayName = "My Project Custom Field Display Name";
var projectCustomField = projContext.CustomFields.FirstOrDefault(cf => cf.Name == projectCustomFieldDisplayName);
object projectCustomFieldValue = "";
var resourceCustomFieldDisplayName = "My Enterprise Resource Custom Field Display Name";
var resourceCustomField = projContext.CustomFields.FirstOrDefault(cf => cf.Name == resourceCustomFieldDisplayName);

foreach (EnterpriseResource resource in enterpriseResources)
{
    if (!string.IsNullOrEmpty(resource.Email))
    {
        var pId = Guid.Parse("a5c8801f-d505-e811-a745-b0359f8878e9");
        var customProject = projContext.LoadQuery(projContext.Projects.Where(p => p.Id == pId).Include(
            p => p.Id,
            p => p.Name,
            p => p.IncludeCustomFields,
            p => p.IncludeCustomFields.CustomFields,
            P => P.IncludeCustomFields.CustomFields.IncludeWithDefaultProperties(
            lu => lu.LookupTable,
            lu => lu.LookupEntries
            ))
        );

        projContext.ExecuteQuery();

        foreach (PublishedProject pubProj in customProject)
        {
            var projECFs = pubProj.IncludeCustomFields.CustomFields;
            Dictionary<string, object> ECFValues = pubProj.IncludeCustomFields.FieldValues;

            if (projectCustomField != null)
            {
                projectCustomFieldValue = ECFValues.Where(pair => pair.Key == projectCustomField.InternalName).FirstOrDefault().Value;
            }
        }

        // Update enterprise resource custom field
        if (resourceCustomField != null)
        {
            resource.SetCustomFieldValue(resourceCustomField.InternalName, projectCustomFieldValue);
            enterpriseResources.Update();
        }
    }
}
...