Iterate through all the properties of MSProject.Task

A

Aj Singh

Hi Rod,
I am trying to iterate through the MSProject.Tasks for every MSProject.Task
object, i am using System.Reflection to get the value of each property and
its value.
Below is the code that i am using to fetch the values for MSProject.Task


Requirement:
1) Every user would configure what "property" he wishes to see, which would
be fetched from database.
2) Hence hard coding the property names is not possible, therefore,
System.Reflection is used to keep it dynamic.

Problem
Not able to iterate through the MSProject.Task object in active project.

The same code works for MSProject.Project object. Using C# as the language,
VS 2008, MSProject 2003, PIA 2003

Approch 1:
/*******************code starts here**************************/
MSProject.Project _objActiveProject;
///_objActiveProject is the object which is obtained by openning the file
through the interop of MSProject 2003.

MSProject.Tasks _objTasks = _objActiveProject.Tasks;

///Type to get the Type for the MSProject.Task object.
Type _objTaskType;

///_objPropertyInfo would contain each property for the _objTaskType object
and its value can be fetched later.
PropertyInfo _objPropertyInfo;

for (int _intCounter = 1; _intCounter < _objTasks.Count; _intCounter++)
{
_objTaskType = _objTasks[_intCounter].GetType();


for (int _intNewCounter = 0; _intNewCounter <
_objProperties.Rows.Count; _intNewCounter++)
{

/// _objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString() This
contains the property names
/// for the task object, which can be obtained if i access the
property directly. If i try to make it dynamic
/// it does not return the instance of that object.

///I have also tried: _objTaskType.GetProperty("ActualDuration"),
however _objPropertyInfo remains null, hence i don't get the value.
_objPropertyInfo =
_objTaskType.GetProperty(_objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString());

string _strName = "ActualDuration";

_objPropertyInfo = _objTaskType.GetProperty(_strName);

if (_objPropertyInfo != null)
{
_objstrProjectTable.Append("<tr
style='text-align:left'><td>" + _objPropertyInfo.Name + "</td><td>" +
_objPropertyInfo.GetValue(_objTasks[_intCounter], null).ToString() +
"</td><tr>");
}
}
}
/*******************code ends here**************************/

Approach 2:
I have also tried writing code using:
foreach task in tasks
//try to get the type of task object
//get the property name and its value,
//write to output media (response, string, file , db etc)
loop
however even here the type does not seem to get property for the task object
however the same thing work for MSProject.Project

Any help or pointer is deeply appreciated as i have already quite some time
on this, looks simple and logical however it doesn't seem to work.

Thanks & Regards,
Ajay Singh
 
S

Stephen Sanderlin [MVP]

I'm not an expert on COM Interop by any means, but I think it has to do with
the way Microsoft decorated the interfaces in the interop assembly...
running Reflector against the interop assembly should show you what I mean.

As a workaround, have you considered using the Task.GetField() method
instead of reflection? That way you can just pass in a PjField enum (which
you could first parse from a string if you had to).

--
Stephen Sanderlin, Project MVP
VP of Technology
MSProjectExperts

For Project Server Consulting: http://www.msprojectexperts.com
For Project Server Training: http://www.projectservertraining.com

Read our blog at: http://www.projectserverhelp.com

Learn | Connect | Grow @ The Microsoft Project Conference Phoenix, AZ -
September 14-17, 2009

Aj Singh said:
Hi Rod,
I am trying to iterate through the MSProject.Tasks for every
MSProject.Task
object, i am using System.Reflection to get the value of each property and
its value.
Below is the code that i am using to fetch the values for MSProject.Task


Requirement:
1) Every user would configure what "property" he wishes to see, which
would
be fetched from database.
2) Hence hard coding the property names is not possible, therefore,
System.Reflection is used to keep it dynamic.

Problem
Not able to iterate through the MSProject.Task object in active project.

The same code works for MSProject.Project object. Using C# as the
language,
VS 2008, MSProject 2003, PIA 2003

Approch 1:
/*******************code starts here**************************/
MSProject.Project _objActiveProject;
///_objActiveProject is the object which is obtained by openning the file
through the interop of MSProject 2003.

MSProject.Tasks _objTasks = _objActiveProject.Tasks;

///Type to get the Type for the MSProject.Task object.
Type _objTaskType;

///_objPropertyInfo would contain each property for the _objTaskType
object
and its value can be fetched later.
PropertyInfo _objPropertyInfo;

for (int _intCounter = 1; _intCounter < _objTasks.Count; _intCounter++)
{
_objTaskType = _objTasks[_intCounter].GetType();


for (int _intNewCounter = 0; _intNewCounter <
_objProperties.Rows.Count; _intNewCounter++)
{

/// _objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString() This
contains the property names
/// for the task object, which can be obtained if i access the
property directly. If i try to make it dynamic
/// it does not return the instance of that object.

///I have also tried: _objTaskType.GetProperty("ActualDuration"),
however _objPropertyInfo remains null, hence i don't get the value.
_objPropertyInfo =
_objTaskType.GetProperty(_objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString());

string _strName = "ActualDuration";

_objPropertyInfo = _objTaskType.GetProperty(_strName);

if (_objPropertyInfo != null)
{
_objstrProjectTable.Append("<tr
style='text-align:left'><td>" + _objPropertyInfo.Name + "</td><td>" +
_objPropertyInfo.GetValue(_objTasks[_intCounter], null).ToString() +
"</td><tr>");
}
}
}
/*******************code ends here**************************/

Approach 2:
I have also tried writing code using:
foreach task in tasks
//try to get the type of task object
//get the property name and its value,
//write to output media (response, string, file , db etc)
loop
however even here the type does not seem to get property for the task
object
however the same thing work for MSProject.Project

Any help or pointer is deeply appreciated as i have already quite some
time
on this, looks simple and logical however it doesn't seem to work.

Thanks & Regards,
Ajay Singh
 
A

Aj Singh

Hi Stephen,
Thanks for help and guidance, i noticed that may i asked it in a wrong forum,
however my question is answered now, it indeed had to do with the COM, here
is the solution:

http://social.msdn.microsoft.com/Fo...06e5a89/#c5e5249e-fe13-4af0-bca4-f5b2b035eaaa

I would also keep the Task.GetField() in my to check it out.
Thanks again.

Regards,
Ajay Singh


Stephen Sanderlin said:
I'm not an expert on COM Interop by any means, but I think it has to do with
the way Microsoft decorated the interfaces in the interop assembly...
running Reflector against the interop assembly should show you what I mean.

As a workaround, have you considered using the Task.GetField() method
instead of reflection? That way you can just pass in a PjField enum (which
you could first parse from a string if you had to).

--
Stephen Sanderlin, Project MVP
VP of Technology
MSProjectExperts

For Project Server Consulting: http://www.msprojectexperts.com
For Project Server Training: http://www.projectservertraining.com

Read our blog at: http://www.projectserverhelp.com

Learn | Connect | Grow @ The Microsoft Project Conference Phoenix, AZ -
September 14-17, 2009

Aj Singh said:
Hi Rod,
I am trying to iterate through the MSProject.Tasks for every
MSProject.Task
object, i am using System.Reflection to get the value of each property and
its value.
Below is the code that i am using to fetch the values for MSProject.Task


Requirement:
1) Every user would configure what "property" he wishes to see, which
would
be fetched from database.
2) Hence hard coding the property names is not possible, therefore,
System.Reflection is used to keep it dynamic.

Problem
Not able to iterate through the MSProject.Task object in active project.

The same code works for MSProject.Project object. Using C# as the
language,
VS 2008, MSProject 2003, PIA 2003

Approch 1:
/*******************code starts here**************************/
MSProject.Project _objActiveProject;
///_objActiveProject is the object which is obtained by openning the file
through the interop of MSProject 2003.

MSProject.Tasks _objTasks = _objActiveProject.Tasks;

///Type to get the Type for the MSProject.Task object.
Type _objTaskType;

///_objPropertyInfo would contain each property for the _objTaskType
object
and its value can be fetched later.
PropertyInfo _objPropertyInfo;

for (int _intCounter = 1; _intCounter < _objTasks.Count; _intCounter++)
{
_objTaskType = _objTasks[_intCounter].GetType();


for (int _intNewCounter = 0; _intNewCounter <
_objProperties.Rows.Count; _intNewCounter++)
{

/// _objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString() This
contains the property names
/// for the task object, which can be obtained if i access the
property directly. If i try to make it dynamic
/// it does not return the instance of that object.

///I have also tried: _objTaskType.GetProperty("ActualDuration"),
however _objPropertyInfo remains null, hence i don't get the value.
_objPropertyInfo =
_objTaskType.GetProperty(_objProperties.Rows[_intNewCounter]["PROPERTY_NAME"].ToString());

string _strName = "ActualDuration";

_objPropertyInfo = _objTaskType.GetProperty(_strName);

if (_objPropertyInfo != null)
{
_objstrProjectTable.Append("<tr
style='text-align:left'><td>" + _objPropertyInfo.Name + "</td><td>" +
_objPropertyInfo.GetValue(_objTasks[_intCounter], null).ToString() +
"</td><tr>");
}
}
}
/*******************code ends here**************************/

Approach 2:
I have also tried writing code using:
foreach task in tasks
//try to get the type of task object
//get the property name and its value,
//write to output media (response, string, file , db etc)
loop
however even here the type does not seem to get property for the task
object
however the same thing work for MSProject.Project

Any help or pointer is deeply appreciated as i have already quite some
time
on this, looks simple and logical however it doesn't seem to work.

Thanks & Regards,
Ajay Singh
 

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