Robert V
Robert V

Reputation: 41

How do I set a custom enterprise field in an OnCreating event receiver within Project Server?

I am trying to set a project enterprise custom field within the OnCreating event of an event receiver within Project Server 2013 on premise. When I debug the code, everything looks to be set correctly but the field is always empty once the project is created.

I have found how to set the enterprise custom field from some other posts. I tried using other events like OnCreated but the project is checked out so I can't update the project.

        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);
     }

The expected result is the project is created with the enterprise custom field populated but the enterprise field is empty.

Upvotes: 2

Views: 85

Answers (0)

Related Questions