我正在尝试在本地 Project Server 2013 中的事件接收器的 OnCreating 事件中设置项目企业自定义字段。当我调试代码时,一切看起来都设置正确,但一旦创建项目,该字段始终为空。
我从其他一些帖子中找到了如何设置企业自定义字段。我尝试使用其他事件,如 OnCreated,但项目已签出,因此我无法更新项目。
public override void OnCreating(PSContextInfo contextInfo, ProjectPreEventArgs e)
{
string projectName = e.ProjectName;
Guid projectUid = e.ProjectGuid;
PROJECT_SERVER_URI = (new SPSite(contextInfo.SiteGuid)).Url;
ClientContext ctx = new ClientContext(PROJECT_SERVER_URI);
//retrieve and increment the number
int newProjectID = GetNextProjectID(ctx);
// get the project data set
ProjectDataSet projectDS = e.ProjectDataSet;
// set the id of the enterprise custom field
var projectIDCol = new Guid("a644279e-5c62-e711-943c-00155d00d20a");
bool fieldNotFound = true;
// look in each custom field for the enterprise field
foreach (ProjectCustomFieldsRow row in projectDS.ProjectCustomFields)
{
if (row.MD_PROP_UID.Equals(projectIDCol))
{
// update the field with my value
row.NUM_VALUE = newProjectID;
fieldNotFound = false;
break;
}
}
if (fieldNotFound)
{
// the field was not found so manually add it
var newRow = projectDS.ProjectCustomFields.NewProjectCustomFieldsRow();
// clear the values
newRow.SetDATE_VALUENull();
newRow.SetTEXT_VALUENull();
newRow.SetNUM_VALUENull();
// set the field properties
newRow.CUSTOM_FIELD_UID = Guid.NewGuid();
newRow.MD_PROP_UID = projectIDCol;
newRow.PROJ_UID = projectUid;
newRow.FIELD_TYPE_ENUM = 15;
newRow.NUM_VALUE = newProjectID;
// add the custom field to the custom fields
projectDS.ProjectCustomFields.AddProjectCustomFieldsRow(newRow);
}
e.Cancel = false;
// pass the objects to the base implementation
base.OnCreating(contextInfo, e);
}
预期结果是创建项目时填充了企业自定义字段,但企业字段为空。