Finding or filtering resources, tasks and projects can be done in several ways. What you need is the internal name of the custom field. We can retrieve the objectsAssuming you want to make your application compatible with multiple PWA’s so you can reuse your code, it might be a good idea to use the CustomField name first to retrieve the InternalName property of the CustomField object. We will take the Microsoft.ProjectServer.Client.CustomField class. There are three properties you must remember:
Property | Description | Object Type | Example |
---|---|---|---|
Name | This is the name you have given to your custom field. | string | Resource_Alias |
Id | This is the unique identifier for the custom field. | Guid | 554b6e30-b1d4-46c2-b85a-98fd21d3f425 |
InternalName | This is a string in the format ‘Custom_’ with the dashes removed. | string | Custom_554b6e30b1d446c2b85a98fd21d3f425 |
In case you want to look up the ‘InternalName‘ property of the custom field, you can do that with the code below. Note that depending on which method you use ‘Single‘, ‘Find‘ or ‘First‘ it will affect the lookup speed.
Now if we pass the parameter ‘Resource_Alias‘ to the method ‘GetInternalName‘, we will get an exception if more than one result is found, which is impossible since you cannot define a custom field with the same name twice. But if the custom field is found once, we will get a string value in the format: ‘Custom_554b6e30b1d446c2b85a98fd21d3f425‘. This is basically ‘_Custom__‘ with a guid string appended to it. Now what we need to do is retrieve all the resources from ProjectServer, and filter based on the value we just retrieved.
This method will work for every object type that uses custom fields in Project Server. Also not that instead of:
You can also use:
Now in the large block of code above, I highlighted a small part:
It appears that this can crash your application with a ‘PropertyNotInitializedException‘ if the custom field is empty for a certain task, resource or project. One way to solve this is by writing your code like this: