Reading Assignment Enterprise Custom Field Values with VSTO/C#

S

Smugliy

Yes, I am using Project 2007 and trying to do it using VSTO add-in .

Any suggestions ?
 
S

Stephen Sanderlin

S

Sreerenj Nair

Hi,

The Assignment Enterprise Custom Fields cannot be accessed directly since it
is not a part of the other field lists. In VBA, we can access it directly by
specifying the name of the custom field. This will be evaluated at run time
through late binding.

In C#, we can call the InvokeMember method of Type object to get the value
of custom field. The sample code is given below:

// This code will get the value of the custom field (testcustom) of first
Assignment
// of first Task.

object objAssignment;

// we can explicitly mention the type of Assignment object as:
// Microsoft.Office.Interop.MSProject.Assignment objAssignment;

// Assign the first assignment to the object
objAssignment =
Globals.ThisAddin.Application.ActiveProject.Tasks[1].Assignments[1];

// Use InvokeMember to call the 'testcustom' custom field
MessageBox.Show(objAssignment.GetType().InvokeMember("testcustom",
BindingFlags.GetProperty | BindingFlags.InvokeMethod, null, objAssignment,
null).ToString());

If you are using VB.NET there is another way to get the value of custom
field which is similar to the one we use in VBA. The code goes as below:

Dim objAssignment as Object
' Here the assignment object should be explicitly declared as object not as
an
' Assignment object. Otherwise it will not work.

' Now assign the assignment of the task to the object
objAssignment =
Globals.ThisAddin.Application.ActiveProject.Tasks[1].Assignment[1]

' Get the value of the custom field by directly giving the custom field name
Msgbox(objAssignment.testcustom)


This method will not work in C# because it is a strongly typed language.

Please let me know if this helps you.
 
S

Sreerenj Nair

For setting the value to the Assignment Enterprise Custom Field, the
following code can be used:

// This code will set the value of the custom field

// Declare the Assignment Object
Microsoft.Office.Interop.MSProject.Assignment objAssignment;

// Set the first Assignment object of the first Task
objAssignment =
Globals.ThisAddin.Application.ActiveProject.Tasks[1].Assignment[1];

// Create the parameter object to set the value
object[] parameters = new object[1];
parameters[0] = "somevalue";

// Set the value of the custom field
objAssignment.GetType().InvokeMember("testcustom",
BindingFlags.PutDispProperty, null, objAssignment, parameters);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top