Dynamically Loading Project Online Properties via CSOM

不问归期 提交于 2019-12-24 14:40:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!