问题
I'm looking at trying to load our projects from project online into a .NET application, while i can load the projects one thing i'm having trouble with is loading all the columns i would like. I know i can specify the columns i want to load using include but i wanted to be able to use a list of columns that could be generated dynamically.
It's part of an ETL program that I would like to allow the users to configure the list of columns being brought over into the cache database. Below is what I have so far
static void Main(string[] args)
{
ProjectContext pc = getProjCtxt();
pc.Load(pc.Projects);
pc.ExecuteQuery();
ColumnNames fldList = new ColumnNames();
var enumerator = pc.Projects.GetEnumerator();
while (enumerator.MoveNext()) {
var p2 = enumerator.Current;
pc.Load(p2);
pc.ExecuteQuery();
Console.WriteLine(p2.FinishDate);
}
foreach (PublishedProject p in pc.Projects)
{
var pubProj = p.IncludeCustomFields;
pc.Load(pubProj);
pc.ExecuteQuery();
//Dictionary<string, object> projDict = pubProj.FieldValues;
var type = p.GetType();
foreach(ColumnNames.colInfo ci in fldList.impFields)
{
if (type.GetProperty(ci.FieldName) != null)
{
Console.WriteLine(p.FinishDate);
Console.WriteLine(type.GetProperty(ci.FieldName).GetValue(p, null));
}
}
}
}
I get an error on the FinishDate because it hasn't been initialized well how to i initialize all the properties of a task/project so i can work with them if the program doesn't know ahead of time what columns it is looking for.
Is there a way to build up a string and pass it to the project online to tell it what properties to initialize.?
回答1:
So i found an answer buried eventually. https://sharepoint.stackexchange.com/questions/89634/syntax-for-including-fields-dynamically-in-csom-query
Managed to also simplify the vode and clean it up. as well as make sure that i'm only including the non Custom Fields in the explicit properties list since the IncludeCustomFields option brings all the custom fields down with it.
static void Main(string[] args)
{
ProjectContext pc = getProjCtxt();
ColumnNames fldList = new ColumnNames();
var q = from ColumnNames.colInfo fld in fldList.impFields
where fld.CustomField == false
select fld;
Console.WriteLine(q.Count());
foreach(ColumnNames.colInfo dynField in q){
pc.Load(pc.Projects, p => p.Include(pj => pj[dynField.FieldName]));
}
pc.Load(pc.Projects, p => p.Include(pj => pj.IncludeCustomFields));
pc.ExecuteQuery();
}
来源:https://stackoverflow.com/questions/33291022/dynamically-loading-project-online-properties-via-csom