在PWA Online中更新与企业资源相关的企业自定义字段

时间:2018-09-03 13:16:34

标签: c# sharepoint csom ms-project

我需要根据条件在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();

                        }

                    }
                }
            }

尚未完成的事情:

  

无法获取与其相关的企业自定义字段:

我已经尝试过此查询,但无法获取上面提到的确切字段:

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 :(得分:0)

您需要:
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();
        }
    }
}