PublishedProject.FieldValues 屬性
取得已發佈的專案自訂欄位值。
命名空間: Microsoft.ProjectServer.Client
組件: Microsoft.ProjectServer.Client (在 Microsoft.ProjectServer.Client.dll 中)
語法
'宣告
Public ReadOnly Property FieldValues As Dictionary(Of String, Object)
Get
'用途
Dim instance As PublishedProject
Dim value As Dictionary(Of String, Object)
value = instance.FieldValues
public Dictionary<string, Object> FieldValues { get; }
屬性值
類型:System.Collections.Generic.Dictionary<String, Object>
一組的成對的自訂欄位值,其中索引鍵是內部的自訂欄位的名稱,而值為自訂欄位值或查閱表格項的內部名稱機碼和值。
範例
下列程式碼範例會查詢的自訂欄位其集合的每個已發佈的企業專案。它會列出 GUID 和專案的名稱和每個欄位的值的名稱和專案的每個自訂欄位的內部名稱作為機碼值配對中的索引鍵。如果自訂欄位使用查閱表格, FieldValues項目包含內部查閱表格項目的名稱。如果自訂欄位是多重值文字欄位,範例會列出每個查閱表格項目的專案中的自訂欄位的內部名稱與值。如果自訂欄位不使用查閱表格,此值為只是自訂欄位值。
自訂欄位的內部名稱為字串值"Custom_ [GUID] 」,例如Custom_9d77d62aa92e4d40adc8446c90eb7456。查閱表格項的內部名稱為字串值"Entry_ [GUID] 」,例如Entry_4a2750309d91e21193f90021704e28a0。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ProjectServer.Client;
namespace GetProjectFieldValues
{
class Program
{
private const string pwaPath = "https://ServerName/pwa"; // Change the path for Project Web App.
// Set the Project Server client context.
private static ProjectContext projContext;
static void Main(string[] args)
{
projContext = new ProjectContext(pwaPath);
// Get the list of published projects in Project Web App.
projContext.Load(projContext.Projects);
projContext.ExecuteQuery();
Console.WriteLine("\t\tProject ID\t :\tProject name");
if (projContext.Projects.Count > 0)
{
for (int i = 0; i < projContext.Projects.Count; i++)
{
var pubProj = projContext.Projects[i].IncludeCustomFields;
projContext.Load(pubProj);
projContext.Load(pubProj.CustomFields);
projContext.ExecuteQuery();
Console.WriteLine("\n{0} :\t{1}", pubProj.Id.ToString(), pubProj.Name);
int numCFs = pubProj.CustomFields.Count;
Console.WriteLine("Number of custom fields: {0}", numCFs.ToString());
Dictionary<string, object> projDict = pubProj.FieldValues;
// Variables to compare with custom field types:
string textValue = ""; // TEXT - no lookup table
String[] lutTextValues = { "" }; // TEXT - lookup table
DateTime dateValue = DateTime.MinValue; // DATE - no lookup table
foreach (KeyValuePair<string, object> kvp in projDict)
{
object oVal = kvp.Value;
string keyStr = kvp.Key;
var projCFs = projContext.LoadQuery(pubProj.CustomFields
.Where(cf => cf.InternalName == keyStr));
projContext.ExecuteQuery();
Console.WriteLine("\n Custom field: '{0}'; FieldType = {1};\n\tInternalName = {2}",
projCFs.First().Name, projCFs.First().FieldType.ToString(), keyStr);
// Check whether the custom field is a simple text field.
if (object.ReferenceEquals(oVal.GetType(), textValue.GetType()))
{
textValue = (string)oVal;
Console.WriteLine("\tSingle-line string value = {0}", textValue.ToString());
}
// Check whether the custom field is a text (or multivalue text) field from a lookup table.
else if (object.ReferenceEquals(oVal.GetType(), lutTextValues.GetType()))
{
projContext.Load(projCFs.First().LookupTable);
projContext.ExecuteQuery();
string lutName = projCFs.First().LookupTable.Name;
Console.WriteLine("\tLookup table: {0}", lutName);
var luts = projContext.LoadQuery(projContext.LookupTables
.Where(lut => lut.Name == lutName));
projContext.ExecuteQuery();
lutTextValues = (string[])oVal;
for (var j = 0; j < lutTextValues.Count(); j++)
{
var entries = projContext.LoadQuery(luts.First().Entries
.Where(e => e.InternalName == lutTextValues[j]));
projContext.ExecuteQuery();
Console.WriteLine("\t Entry({0}): String value = {1};\n\t\t InternalName = {2}",
j, entries.First().FullValue, lutTextValues[j]);
}
}
// Check whether the custom field is a date.
else if (object.ReferenceEquals(oVal.GetType(), dateValue.GetType()))
{
dateValue = (DateTime)oVal;
Console.WriteLine("\tDate value = {0}", dateValue.ToString());
}
// Add other cases for cost, duration, flag, and number custom fields,
// with and without lookup tables.
}
}
}
Console.Write("\nPress any key to exit: ");
Console.ReadKey(false);
}
}
}
例如,名為 TestProject9 的專案擁有專案部門自訂欄位值、 三個查閱表格項目值、 單純的文字自訂欄位,與日期自訂欄位的多值文字自訂欄位。由於專案部門內建的自訂欄位,專案部門自訂欄位的 GUID 以預設Project Web App執行個體是9d77d62a-a92e-4d40-adc8-446c90eb7456。以下是GetProjectFieldValues範例應用程式的輸出:
b846e947-29e0-43eb-b5c6-5ddeaf08d5c0 : TestProject9
Number of custom fields: 4
Custom field: 'Project Departments'; FieldType = TEXT;
InternalName = Custom_9d77d62aa92e4d40adc8446c90eb7456
Lookup table: Department
Entry(0): String value = Test Dept 1;
InternalName = Entry_bbc07ff5b06de21193f40021704e28a0
Custom field: 'ProjectMVText'; FieldType = TEXT;
InternalName = Custom_9295a8759d91e21193f90021704e28a0
Lookup table: TestTextMV
Entry(0): String value = First.001;
InternalName = Entry_4a2750309d91e21193f90021704e28a0
Entry(1): String value = Second.002;
InternalName = Entry_4d2750309d91e21193f90021704e28a0
Entry(2): String value = Third;
InternalName = Entry_4f2750309d91e21193f90021704e28a0
Custom field: 'Test Project Date'; FieldType = DATE;
InternalName = Custom_37f61601a991e21193f90021704e28a0
Date value = 3/29/2013 8:00:00 AM
Custom field: 'Test project simple text'; FieldType = TEXT;
InternalName = Custom_8bf7eed5cc94e21193f90021704e28a0
Single-line string value = This is a line of text