Tuesday, 7 July 2020

Helping blog url

Set Up PnP Core Component CSOM Library To Work With SharePoint 2016




IMPORTING FILES TO PROJECT ONLINE WITH VBA







JS Link


How to Use Security Categories to Grant Edit Access To


Angular $q.all() Performing two simultaneous ajax requests with AngularJS.


How to create Excel file in C#





Sharepoint 2013 Check If File Already Exists In Document Library Using CSOM





Generate Random Number And Random String In C#



Date Range validation 



Enable or disable macros in Office files



JQGrid Excel like feature


Copy SharePoint list item to another list.






File Upload using CSOM with SubFolders structure



Copy file from one location to other location in sharepoint using client object model in C#

https://social.technet.microsoft.com/Forums/ie/en-US/a0e0e509-af10-4270-ae80-7ccca2fc3a5f/copy-file-from-one-location-to-other-location-in-sharepoint-using-client-object-model-in-c?forum=sharepointgeneral












Restricting or blocking Office 2016/2019 macros with Group Policy

Use SharePoint people pickers on Project Server or Online PDPs


Read complete project data (Project level + Task level info) with a single request using CSOM for Project Server or Project Online



get month start date and end date in javascript

embed.plnkr.co/tO4vTm/preview
https://stackoverflow.com/questions/38141651/how-to-create-new-ept-by-using-csom

http://findnerd.com/list/view/How-to-create--RisksIssues-and-Deliverables-using-CSOM-in-SharePoint/15874/

avaScript: Get the month start date

https://www.w3resource.com/javascript-exercises/javascript-date-exercise-52.php

http://embed.plnkr.co/P05l1a/preview

JavaScript: Get the week end date

https://www.w3resource.com/javascript-exercises/javascript-date-exercise-51.php

https://jsfiddle.net/klarstil/mvL6eu7c/

jQuery DataTables: How to expand/collapse all child rows

https://www.gyrocode.com/articles/jquery-datatables-how-to-expand-collapse-all-child-rows/


How to format a number as a currency value in JavaScript


https://flaviocopes.com/how-to-format-number-as-currency-javascript/

Friday, 3 July 2020

Project Online REST Api List

Accessing Office 365 Project Web App Objects using REST API

This article describes accessing the objects present on the Office 365 project web app site collection using REST API.

PWA abbreviated as Project Web App, is available on Office 365, and it used for easily and efficiently planning projects, track status, and collaborate with other virtually. It is an application built on top of SharePoint, which has collaborating features. Any PWA site collection will have the project web app settings feature enabled on the site collection. To know more about PWA, refer to the official documentation available on the MSDN site.

Let us see the high level REST API (service end points), which can be used for accessing objects available on Office 365 PWA site collections.

Note: Replace the Office 365 PWA url with your respective URL.

To access the project server and list of available objects,
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer

The REST APIs used for accessing objects available on the project servers are explained below.


Custom Fields:

The enterprise custom fields are created on the project plans for managing additional project attributes. The custom field can be at project or task level. The following API is used to access all the enterprise custom fields.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields

One specific field can be accessed using the field GUID:
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')


The lookup tables associated to the custom fields can be accessed using the following REST API.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/LookupTable

The lookup table entries associated to the custom fields can be accessed by the following REST API.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/LookupEntries


Lookup Tables:

These enterprise custom lookup tables are associated with the enterprise custom fields on project web apps. Any field can have lookup tables for easily accessing the data. The lookup tables are accessed using the REST API,
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/LookupTables

The specific lookup table can be accessed by using its GUID.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/LookupTables('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')

The lookup table associated to a custom field can be accessed by any of the following APIs.
  • https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/LookupTables('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
  • https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/CustomFields('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/LookupTable

The lookup table entries can be accessed by
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/LookupTables('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/entries


Project Types:

To get the enterprise project types.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/EnterpriseProjectTypes

The specific project type is accessed with the help of GUID.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/EnterpriseProjectTypes('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')

The project detail pages within the project types are access by,
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/EnterpriseProjectTypes('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/ProjectDetailPages


Resources:

To get the enterprise resources.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/EnterpriseResources

The specific resource is accessed with the help of GUID.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/EnterpriseResources('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')

Under any specific resource, the objects like assignments, basecalendars, customfields, engagements, userpermissions, etc present. All these objects have relative REST API, which are listed in the above URL.


Entity Types:

To get the project server entity types. There are four entity types, and they are assignment, project, resource and task.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/EntityTypes

The four entity types are accessed by,
  • Assignment Entity: /_api/ProjectServer/EntityTypes/AssignmentEntity
  • Project Entity: /_api/ProjectServer/EntityTypes/ProjectEntity
  • Resource Entity: /_api/ProjectServer/EntityTypes/ResourceEntity
  • Task Entity: /_api/ProjectServer/EntityTypes/TaskEntity


Projects:

The projects available on the PWA can be accessed by,
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/Projects

Any project and project information can be accessed with GUID.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')

The other major properties of the project are accessed using REST APIs.
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/CheckedOutBy
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/CustomFields
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/Engagements
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/EnterpriseProjectType
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/UserPermissions
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/Phase
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/ProjectSummaryTask
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/QueueJobs
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/Stage
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/Assignments
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/Calendar
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/Draft
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/EntityLinks
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/IncludeCustomFields
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/TaskLinks
  • /_api/ProjectServer/Projects('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')/Tasks



Project Detail Pages: 

These pages are used to create, edit, and view the projects available on PWA. To get the project detail pages:
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/ProjectDetailPages


Calendars:

To get the list of calendars available on the PWA:
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/Calendars

The specific calendar can be accessed with the help of its GUID.
https://nakkeerann.sharepoint.com/sites/pwa/_api/ProjectServer/Calendars('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')

MPP Import and Export CSOM code

///AbstractBar.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectImportConsoleApp
{
    public abstract class AbstractBar
    {
        public AbstractBar()
        {

        }

        /// <summary>
        /// Prints a simple message 
        /// </summary>
        /// <param name="msg">Message to print</param>
        public void PrintMessage(string msg)
        {
            Console.WriteLine(msg);
        }

        public abstract void Step();
    }
    class AnimatedBar : AbstractBar
    {
        List<string> animation;
        int counter;
        public AnimatedBar() : base()
        {
            this.animation = new List<string> { "/""-"@"\""|" };
            this.counter = 0;
        }

        /// <summary>
        /// prints the character found in the animation according to the current index
        /// </summary>
        public override void Step()
        {
            Console.Write(this.animation[this.counter] + "\b");
            this.counter++;
            if (this.counter == this.animation.Count)
                this.counter = 0;
        }
    }

    class SwayBar : AbstractBar
    {
        string bar;
        string pointer;
        string blankPointer;
        int counter;
        direction currdir;
        enum direction { right, left };
        public SwayBar() : base()
        {
            this.bar = "|                         |";
            this.pointer = "***";
            this.blankPointer = this.BlankPointer();
            this.currdir = direction.right;
            this.counter = 1;
        }

        /// <summary>
        /// sets the atribute blankPointer with a empty string the same length that the pointer
        /// </summary>
        /// <returns>A string filled with space characters</returns>
        private string BlankPointer()
        {
            StringBuilder blank = new StringBuilder();
            for (int cont = 0; cont < this.pointer.Length; cont++)
                blank.Append(" ");
            return blank.ToString();
        }

        /// <summary>
        /// reset the bar to its original state
        /// </summary>
        private void ClearBar()
        {
            this.bar = this.bar.Replace(this.pointer, this.blankPointer);
        }

        /// <summary>
        /// remove the previous pointer and place it in a new possition
        /// </summary>
        /// <param name="start">start index</param>
        /// <param name="end">end index</param>
        private void PlacePointer(int start, int end)
        {
            this.ClearBar();
            this.bar = this.bar.Remove(start, end);
            this.bar = this.bar.Insert(start, this.pointer);
        }

        /// <summary>
        /// prints the progress bar acorrding to pointers and current direction
        /// </summary>
        public override void Step()
        {
            if (this.currdir == direction.right)
            {
                this.PlacePointer(counter, this.pointer.Length);
                this.counter++;
                if (this.counter + this.pointer.Length == this.bar.Length)
                    this.currdir = direction.left;
            }
            else
            {
                this.PlacePointer(counter - this.pointer.Length, this.pointer.Length);
                this.counter--;
                if (this.counter == this.pointer.Length)
                    this.currdir = direction.right;
            }
            Console.Write(this.bar + "\r");
        }
    }
}
/// App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup> 
  <appSettings>
    <add key="queryReadProjects" value="_api/ProjectData/Projects()?$select=ProjectName,EnterpriseProjectTypeName"></add>
    <!--<add key="pwaSourceSite" value="https://projectonlinepremium.sharepoint.com/sites/pwa/"></add>-->
    <add key="pwaSourceSite" value="https://projectonlinepremium.sharepoint.com/sites/Training/"></add>
    <add key="pwaDestinationSite" value="https://projectonlinepremium.sharepoint.com/sites/pwa-developer2/"></add>
    <add key="MPPExportlocation" value="D:\ExportedMPP\"></add>    
    <add key="logFilelocation" value="D:\ExportedMPPLogs\"></add>    
    <add key="pwaUserId" value="projectonlinepremium@projectonlinepremium.onmicrosoft.com"></add>
    <add key="pwaPassword" value="password321@"></add>
    <add key="pwaDestUserId" value="projectonlinepremium@projectonlinepremium.onmicrosoft.com"></add>
    <add key="pwaDestPassword" value="password321@"></add>
  </appSettings>
</configuration>




/////////////////

using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace ProjectImportConsoleApp
{
    public class appSettings
    {

        private string _query;
        private string _sourceSite;
        private string _destinationSite;
        private string _userId;
        private string _password;
        private string _destuserId;
        private string _destpassword;
        private string _exportDirlocation;
        private string _logFilelocation;
        public string queryReadProjects
        {
            get { return _query = GetConfigurationValue(appAllkeys.queryKey); }
        }
        public string SourceSite
        {
            get { return _sourceSite = GetConfigurationValue(appAllkeys.SourceSite); }
        }
        public string DestinationSite
        {
            get { return _destinationSite = GetConfigurationValue(appAllkeys.DestinationSite); }
        }
        public string UserId
        {
            get { return _userId = GetConfigurationValue(appAllkeys.UserId); }
        }    
        public string Password
        {
            get
            {
                return _password = GetConfigurationValue(appAllkeys.Password);
            }
        }

        public string DestUserId
        {
            get { return _destuserId = GetConfigurationValue(appAllkeys.DestUserId); }
        }
        public string DestPassword
        {
            get { return _destpassword = GetConfigurationValue(appAllkeys.DestPassword); }
        }
        public string ExportDirlocation
        {
            get { return _exportDirlocation = GetConfigurationValue(appAllkeys.ExportDirlocation); }
        }
        public string logFilelocation
        {
            get { return _logFilelocation = GetConfigurationValue(appAllkeys.logFilelocation); }
        }
        private static string GetConfigurationValue(string Key)
        {
            foreach (var appkey in ConfigurationManager.AppSettings.AllKeys)
            {
                if (appkey == Key)
                {
                    return ConfigurationManager.AppSettings[Key];
                }
            }
            return string.Empty;
        }
        private static void GetConfigurationUsingSection()
        {
            var applicationSettings = ConfigurationManager.GetSection("ApplicationSettings"as NameValueCollection;
            if (applicationSettings.Count == 0)
            {
                Console.WriteLine("Application Settings are not defined");
            }
            else
            {
                foreach (var key in applicationSettings.AllKeys)
                {
                    Console.WriteLine(key + " = " + applicationSettings[key]);
                }
            }
        }                       
    }

    public static class appAllkeys
    {
        public const string queryKey = "queryReadProjects";
        public const string SourceSite = "pwaSourceSite";
        public const string DestinationSite = "pwaDestinationSite";
        public const string UserId = "pwaUserId";
        public const string Password = "pwaPassword";        
        public const string ExportDirlocation = "MPPExportlocation";
        public const string logFilelocation = "logFilelocation";
        public const string DestUserId = "pwaDestUserId";
        public const string DestPassword = "pwaDestPassword";
    }
    public class pwaInstance
    {
        appSettings settings = new appSettings();
        UnicodeEncoding ByteConverter = new UnicodeEncoding();
        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        byte[] plaintext;
        byte[] encryptedtext;
        
        /// <summary>
        /// Runs a query against Sharepoint Rest API
        /// </summary>
        /// <param name="queryString"></param>
        /// <returns>Rest API response</returns>
        public string RunQuery(string queryString)
        {
            string jsonResult = null;
            KeyValuePair<stringSecureString> Credentials = GetProjectOnlineCredentials();
            var pwaCredentials = new SharePointOnlineCredentials(Credentials.Key, Credentials.Value);
            string SourceSiteUrl = settings.SourceSite;
            string pwaUrl = SourceSiteUrl.EndsWith("/") ? SourceSiteUrl : SourceSiteUrl + "/";
            try
            {
                string url = pwaUrl + queryString;
                var req = (HttpWebRequest)WebRequest.Create(url);
                //req.Timeout = 1000000;//to increase timeout
                req.Credentials = pwaCredentials;
                req.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";


                using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                {
                    var receiveStream = resp.GetResponseStream();
                    var readStream = new StreamReader(receiveStream, Encoding.UTF8);
                    string data = readStream.ReadToEnd();

                    data = data.Replace("xml:space=\"preserve\"""");
                    data = data.Replace("m:null=\"true\"""");

                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(data);

                    jsonResult = JsonConvert.SerializeXmlNode(doc);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(DateTime.Now + " : Exception - " + ex.Message);
            }
            return jsonResult;
        }
        public KeyValuePair<stringSecureString> GetProjectOnlineCredentials()
        {            
            KeyValuePair<stringSecureString> pwaCredentials = default(KeyValuePair<stringSecureString>);
            try
            {
                SecureString securePassword = new SecureString();
                foreach (char c in settings.Password.ToCharArray()) securePassword.AppendChar(c);
                pwaCredentials = new KeyValuePair<stringSecureString>(settings.UserId, securePassword);
            }
            catch (Exception ex)
            {
                Console.WriteLine(DateTime.Now + " : Exception - " + ex.Message);                
            }
            return pwaCredentials;
        }

        void ActionEncrypt()
        {
            #region Encription Code
            plaintext = ByteConverter.GetBytes("Password");
            encryptedtext = RSACryptoService.Encryption(plaintext, RSA.ExportParameters(false), false);
            string txtencrypt = ByteConverter.GetString(encryptedtext);
            #endregion
        }
        void ActionDecrypt()
        {
            #region Encription Code
            byte[] decryptedtex = RSACryptoService.Decryption(encryptedtext,
            RSA.ExportParameters(true), false);
            string txtdecrypt = ByteConverter.GetString(decryptedtex);
            #endregion
        }
    }
    #region Print table Source definition
    public class PrintTable
    {
        static int tableWidth = 77;

        static void PrintLine()
        {
            Console.WriteLine(new string('-', tableWidth));
        }

        public static void PrintRow(params string[] columns)
        {
            int width = (tableWidth - columns.Length) / columns.Length;
            string row = "|";

            foreach (string column in columns)
            {
                row += AlignCentre(column, width) + "|";
            }

            Console.WriteLine(row);
        }
        public static void PrintRow(params Properties[] columns)
        {
            int width = (tableWidth - columns.Length) / columns.Length;
            string row = "|";

            foreach (var column in columns)
            {
                row += AlignCentre(column.ProjectName, width) + "|";
                row += AlignCentre(column.EnterpriseProjectTypeName, width) + "|";
            }

            Console.WriteLine(row);
        }
        static string AlignCentre(string text, int width)
        {
            text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

            if (string.IsNullOrEmpty(text))
            {
                return new string(' ', width);
            }
            else
            {
                return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
            }
        }
    }
    #endregion
    #region Json Response Properties Definition
    public class RootObjectResponse
    {
        [JsonProperty("?xml")]
        public XmlDesc XmlDesc { getset; }

        [JsonProperty("feed")]
        public Projectsfeed Feed { getset; }
    }
    public class XmlDesc
    {
        [JsonProperty("@version")]
        public string Version { getset; }


        [JsonProperty("@encoding")]
        public string Encoding { getset; }
    }
    public class Projectsfeed
    {
        [JsonProperty("@base")]
        public string Base { getset; }

        [JsonProperty("id")]
        public string Id { getset; }

        //[JsonProperty("title")]
        //public string Title { get; set; }

        [JsonProperty("updated")]
        public string Updated { getset; }

        [JsonProperty("link")]
        public link Link { getset; }

        [JsonProperty("entry")]
        public List<ProjectsEntry> ProjectsEntry { getset; }
    }
    public class ProjectsEntry
    {
        [JsonProperty("id")]
        public string Id { getset; }


        [JsonProperty("category")]
        public category Category { getset; }


        [JsonProperty("link")]
        public link Link { getset; }

        [JsonProperty("title")]
        public string Title { getset; }

        [JsonProperty("updated")]
        public string Updated { getset; }

        [JsonProperty("author")]
        public author Author { getset; }

        [JsonProperty("content")]
        public content Content { getset; }
    }
    public class category
    {
        [JsonProperty("@term")]
        public string term { getset; }


        [JsonProperty("@scheme")]
        public string scheme { getset; }
    }
    public class link
    {
        [JsonProperty("@rel")]
        public string term { getset; }


        [JsonProperty("@title")]
        public string title { getset; }

        [JsonProperty("@href")]
        public string href { getset; }
    }
    public class author
    {
        [JsonProperty("name")]
        public string name { getset; }


        [JsonProperty("#text")]
        public string Text { getset; }
    }
    public class content
    {
        [JsonProperty("@type")]
        public string type { getset; }

        [JsonProperty("m:properties")]
        public Properties Properties { getset; }
        
    }
    public class Properties
    {
        [JsonProperty("d:EnterpriseProjectTypeName")]
        public string EnterpriseProjectTypeName { getset; }


        [JsonProperty("d:ProjectName")]
        public string ProjectName { getset; }
    }

    public class PropetiesView
    {
        public int SRNo { getset; }
        public string ProjectName { getset; }
        public string EnterpriseProjectTypeName { getset; }               
    }
    #endregion

    //"content":{"@type":"application/xml","m:properties":{"d:EnterpriseProjectTypeName":"TrainingEPT_Second","d:ProjectName":"DemoProject_Second"}}}
}


////////////

using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;

using csom = Microsoft.ProjectServer.Client;

namespace ProjectImportConsoleApp
{
    internal class common
    {
        private static int DEFAULTTIMEOUTSECONDS = 100;
        public string UserName { getset; }
        public string Password { getset; }
        public string PWAUrl { getset; }
        public static string OutPutKey { getset; }
        public static int timeoutSeconds = 30;
        public static string LogMessage { getset; }
        public static ProjectContext SourceProjContext { getset; }
        public static ProjectContext DestinationProjContext { getset; }

        public KeyValuePair<stringProjectContext> Auth(String uname, String pwd, string siteURL)
        {
            ProjectContext Projcontext = new ProjectContext(siteURL);
            Web web = Projcontext.Web;
            SecureString passWord = new SecureString();
            foreach (char c in pwd.ToCharArray()) passWord.AppendChar(c);
            Projcontext.Credentials = new SharePointOnlineCredentials(uname, passWord);
            try
            {
                var projects = Projcontext.Projects;
                Projcontext.Load(projects,
                    ps => ps.IncludeWithDefaultProperties(p => p.Name, p => p.Description, p => p.FinishDate, p => p.EnterpriseProjectType, p => p.EnterpriseProjectType.Name,
                    p => p.StartDate, p => p.Phase, p => p.Stage, p => p.IsCheckedOut, p => p.Draft));
                Projcontext.ExecuteQuery();
                //var securePassword = new SecureString();
                //string password = pwd;
                //foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
                //var cred = new SharePointOnlineCredentials(uname, securePassword);
                //string url = siteURL + "/_api/ProjectData/TimesheetLineActualDataSet()?`$Filter=TimeByDay%20ge%20datetime%27$startDate%27%20and%20TimeByDay%20le%20datetime%27$finishDate%27&`$Select=ResourceName,TimeByDay,ActualWorkBillable,ActualOvertimeWorkBillable?format=json";
                //var req = (HttpWebRequest)WebRequest.Create(url);
                //req.Credentials = cred;
                //req.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";
                //var resp = (HttpWebResponse)req.GetResponse();
                //var receiveStream = resp.GetResponseStream();
                //var readStream = new StreamReader(receiveStream, Encoding.UTF8);
                //var data = readStream.ReadToEnd();
                //XmlDocument doc = new XmlDocument();
                //doc.LoadXml(data);
                //string json = JsonConvert.SerializeXmlNode(doc);
                return new KeyValuePair<stringProjectContext>("True", Projcontext);
            }
            catch (Exception e)
            {
                return new KeyValuePair<stringProjectContext>("False"null);
            }
        }

        /// <summary>
        /// Get Publish project by name
        /// </summary>
        /// <param name="name">the name of the project</param>
        /// <param name="context">csom context</param>
        /// <returns></returns>
        private static csom.PublishedProject GetProjectByName(string name, csom.ProjectContext context)
        {
            IEnumerable<csom.PublishedProject> projs = context.LoadQuery(context.Projects.Include(p => p.Name,
                p => p.Description,
                p => p.FinishDate,
                p => p.EnterpriseProjectType,
                p => p.EnterpriseProjectType.Name,
                p => p.StartDate,
                p => p.Phase,
                p => p.Stage,
                p => p.Owner,
                p => p.IsCheckedOut,
                p => p.Draft,
                p => p.CustomFields,                
                p => p.IncludeCustomFields.CustomFields.Include(cf => cf.Name,
                cf => cf.InternalName,
                cf => cf.LookupTable,
                cf => cf.LookupEntries,
                cf => cf.LookupAllowMultiSelect),
                p => p.Tasks,
                p => p.Draft.Tasks,
                p => p.Draft.Tasks.Include(t => t.Name,
                        t => t.PercentComplete,
                        t => t.OutlinePosition,
                        t => t.OutlineLevel,
                        t => t.Parent,
                        t => t.Successors,
                        t => t.Predecessors),
                p => p.Tasks.Include(
                        t => t.Name,
                        t => t.PercentComplete,
                        t=>t.OutlinePosition,
                        t => t.OutlineLevel,
                        t => t.Parent,
                        t => t.Successors,
                        t => t.Predecessors,
                        t => t.CustomFields,
                        t => t.CustomFields.Include(tcf => tcf.Name,
                        tcf => tcf.InternalName,
                        tcf => tcf.LookupTable,
                        tcf => tcf.LookupEntries,
                        tcf => tcf.LookupAllowMultiSelect)
                      ),
                p => p.Assignments,
                p => p.ProjectResources, p => p.IsEnterpriseProject).Where(p => p.Name == name));
            //context.RequestTimeout = 100000;
            context.ExecuteQuery();

            if (!projs.Any())       // no project found
            {
                return null;
            }
            return projs.FirstOrDefault();
        }

        public static PublishedProject GetSourceProjectByName(string name)
        {
            IEnumerable<PublishedProject> projs = common.SourceProjContext.LoadQuery(common.SourceProjContext.Projects.Where(p => p.Name == name));
            common.SourceProjContext.ExecuteQuery();
            if (!projs.Any())       // no project found
            {
                return null;
            }
            return projs.FirstOrDefault();
        }

        public static PublishedProject GetDestinationProjectByName(string name)
        {
            IEnumerable<PublishedProject> projs = common.DestinationProjContext.LoadQuery(common.DestinationProjContext.Projects.Where(p => p.Name == name));
            common.DestinationProjContext.ExecuteQuery();
            if (!projs.Any())       // no project found
            {
                return null;
            }
            return projs.FirstOrDefault();
        }

        private static appSettings settings = new appSettings();
        private static common cmn = new common();

        private IEnumerable<PublishedProject> getallpublishedProjects(ProjectContext context)
        {
            IEnumerable<PublishedProject> pubEnumerableProjects = null;
            try
            {
                pubEnumerableProjects = context.LoadQuery(context.Projects.Include(p => p.Name,
                p => p.Description,
                p => p.FinishDate,
                p => p.EnterpriseProjectType,
                p => p.EnterpriseProjectType.Name,
                p => p.StartDate,
                p => p.Phase,
                p => p.Stage,
                p => p.Owner,
                p => p.IsCheckedOut,
                p => p.Draft,
                p => p.CustomFields,
                p => p.IncludeCustomFields.CustomFields.Include(cf => cf.Name,
                cf => cf.InternalName,
                cf => cf.LookupTable,
                cf => cf.LookupEntries,
                cf => cf.LookupAllowMultiSelect),
                p => p.Tasks,
                p => p.Tasks.Include(
                        t => t.Name,
                        t => t.PercentComplete,
                        t => t.CustomFields,
                        t => t.CustomFields.Include(tcf => tcf.Name,
                        tcf => tcf.InternalName,
                        tcf => tcf.LookupTable,
                        tcf => tcf.LookupEntries,
                        tcf => tcf.LookupAllowMultiSelect)
                      ),
                p => p.Assignments,
                p => p.ProjectResources));

                //context.RequestTimeout = 100000;

                context.ExecuteQuery();
            }
            catch (Exception)
            {
            }
            return pubEnumerableProjects;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="projectName"></param>
        public void UpdateCustomFieldValue(String projectName)
        {
            int DEFAULTTIMEOUTSECONDS = 50;
            var desCustomfieldsBlk = common.DestinationProjContext.LoadQuery(common.DestinationProjContext.CustomFields
                      .Include(p => p.Id,
                          p => p.Name,
                          p => p.InternalName,
                          p => p.LookupTable,
                          p => p.LookupEntries
                      )
                  );
            common.DestinationProjContext.ExecuteQuery();
            //foreach (PublishedProject Project in common.SourceProjContext.Projects)
            //{
            PublishedProject Project = GetSourceProjectByName(projectName);
            common.SourceProjContext.Load(Project.IncludeCustomFields);
            common.SourceProjContext.Load(Project.IncludeCustomFields.CustomFields, cs => cs.IncludeWithDefaultProperties(pr => pr.EntityType, pr => pr.FieldType, pr => pr.LookupTable, pr => pr.LookupTable.Name, pr => pr.LookupEntries));
            common.SourceProjContext.ExecuteQuery();
            string ProjectName = Project.Name;
            var projectColl = common.DestinationProjContext.LoadQuery(common.DestinationProjContext.Projects.Where(p => p.Name == ProjectName));
            common.DestinationProjContext.ExecuteQuery();
            if (projectColl.ToList().Count() > 0)
            {
                PublishedProject pubProj = projectColl.First();
                common.DestinationProjContext.Load(pubProj.IncludeCustomFields.CustomFields);
                common.DestinationProjContext.ExecuteQuery();
                common.DestinationProjContext.Load(pubProj.CustomFields, cs => cs.IncludeWithDefaultProperties(pr => pr.EntityType, pr => pr.FieldType, pr => pr.LookupTable, pr => pr.LookupTable.Name, pr => pr.LookupEntries, pr => pr.Name, pr => pr.InternalName));
                common.DestinationProjContext.ExecuteQuery();
                DraftProject draft = pubProj.Draft;
                JobState job1 = common.DestinationProjContext.WaitForQueue(draft.CheckIn(true), 50);
                DraftProject projCheckedOut = pubProj.CheckOut();
                foreach (var cust in Project.IncludeCustomFields.FieldValues)
                {
                    string CustomFieldName = Project.IncludeCustomFields.CustomFields.ToList().Where(pr => pr.InternalName == cust.Key).ToList().Count == 0 ? string.Empty : Project.IncludeCustomFields.CustomFields.ToList().Where(pr => pr.InternalName == cust.Key).ToList()[0].Name;
                    //string DestCFIntername = pubProj.CustomFields.ToList().Where(pr => pr.Name == CustomFieldName).ToList().Count == 0 ? string.Empty : pubProj.CustomFields.ToList().Where(pr => pr.Name == CustomFieldName).ToList()[0].InternalName;
                    string DestCFIntername = desCustomfieldsBlk.ToList().Where(cf => cf.Name == CustomFieldName).ToList().Count == 0 ? string.Empty : desCustomfieldsBlk.ToList().Where(cf => cf.Name == CustomFieldName).ToList()[0].InternalName;
                    // tskfrm.lbl_loginfo.Invoke((MethodInvoker)delegate { tskfrm.lbl_loginfo.Text = "Updating CustomField '" + CustomFieldName; });
                    string CustomFieldID = cust.Key.ToString();
                    string CustomFieldValue = Convert.ToString(cust.Value);
                    if (DestCFIntername != string.Empty)
                    {
                        if (CustomFieldValue == "System.String[]")
                        {
                            string LookupTableName = Project.IncludeCustomFields.CustomFields.ToList().Where(pr => pr.InternalName == cust.Key).ToList().Count == 0 ? string.Empty : Project.IncludeCustomFields.CustomFields.ToList().Where(pr => pr.InternalName == cust.Key).ToList()[0].LookupTable.Name;
                            var lookup = common.SourceProjContext.LoadQuery(common.SourceProjContext.LookupTables.Where(x => x.Name == LookupTableName));
                            common.SourceProjContext.ExecuteQuery();
                            string LookupTypeCustomFieldValue = string.Empty;
                            foreach (LookupTable tbl in lookup)
                            {
                                common.SourceProjContext.Load(tbl.Entries);
                                common.SourceProjContext.ExecuteQuery();
                                foreach (LookupEntry en in tbl.Entries)
                                {
                                    for (int i = 0; i < ((string[])(cust.Value)).Count(); i++)
                                    {
                                        string cmp = ((string[])(cust.Value))[i].ToString();
                                        string ent = en.Id.ToString().Replace("-""");
                                        if (cmp == "Entry_" + ent)
                                        {
                                            //common.DestinationProjContext.WaitForQueue(draft.CheckIn(true), 50);
                                            //projCheckedOut = pubProj.CheckOut();
                                            LookupTypeCustomFieldValue += en.FullValue;
                                            var lookup_Dest = common.DestinationProjContext.LoadQuery(common.DestinationProjContext.LookupTables.Where(x => x.Name == LookupTableName));
                                            common.DestinationProjContext.ExecuteQuery();
                                            common.DestinationProjContext.Load(lookup_Dest.ToList()[0].Entries);
                                            common.DestinationProjContext.ExecuteQuery();
                                            Guid SourceEntryGUID = lookup_Dest.ToList()[0].Entries.Where(pr => pr.FullValue == LookupTypeCustomFieldValue).ToList()[0].Id;
                                            projCheckedOut.SetCustomFieldValue(DestCFIntername, new object[] { SourceEntryGUID.ToString() });
                                            projCheckedOut.Update();                                            
                                            LookupTypeCustomFieldValue = string.Empty;                                            
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            //common.DestinationProjContext.WaitForQueue(draft.CheckIn(true), 50);
                            //projCheckedOut = pubProj.CheckOut();
                            projCheckedOut.SetCustomFieldValue(DestCFIntername, CustomFieldValue);
                            projCheckedOut.Update();                            
                        }
                    }
                }
                projCheckedOut.Publish(true);
                JobState jobState = common.DestinationProjContext.WaitForQueue(projCheckedOut.CheckIn(true), DEFAULTTIMEOUTSECONDS);
                if (jobState.Equals(JobState.Success))
                {
                    JobStateLog(jobState, "Project Name: " +ProjectName + " Enterprise Custom Fields update");
                    JobStateLog(JobState.ProcessingDeferred, "Project Name: (" +ProjectName + ") update task Custom Fields");
                    UpdateTaskCustomFieldValues(ProjectName);
                }
            }
            //}
        }

        private static bool checkbothPWASiteConnection()
        {
            bool IsbothSiteConnected = false;
            if (common.SourceProjContext != null && common.DestinationProjContext != null)
            {
                IsbothSiteConnected = true;
            }
            if (common.SourceProjContext == null)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Warning: PWA Source Site disconnected!!");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Please wait doing connection again...");
                var authwithSource = cmn.Auth(settings.UserId, settings.Password, settings.SourceSite);
                common.SourceProjContext = authwithSource.Value;
                common.OutPutKey = authwithSource.Key;
                if (common.OutPutKey == "True")
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("PWA Source Site Connected Sucessfully!!");
                    IsbothSiteConnected = true;
                }
                else if (common.OutPutKey == "False")
                {
                    Console.WriteLine("PWA Source Site Invalid Credentials!!");
                    IsbothSiteConnected = false;
                }
            }
            if (common.DestinationProjContext == null)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Warning: PWA Destination Site disconnected!!");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Please wait doing connection again...");
                var authwithSource = cmn.Auth(settings.UserId, settings.Password, settings.DestinationSite);
                common.DestinationProjContext = authwithSource.Value;
                common.OutPutKey = authwithSource.Key;
                if (common.OutPutKey == "True")
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("PWA Destination Site Connected Sucessfully!!");
                    IsbothSiteConnected = true;
                }
                else if (common.OutPutKey == "False")
                {
                    Console.WriteLine("PWA Destination Site Invalid Credentials!!");
                    IsbothSiteConnected = false;
                }
            }


            return IsbothSiteConnected;
        }

        /// <summary>
        /// Read and Update Project/Task/Resource custom field values,
        /// this method need a project named "New Project" with a task "New task" and assign to a local resource named "New local resource" already created.
        /// Basically please run CreateProjectWithTaskAndAssignment() before running this to avoid exceptions
        /// </summary>
        public static void UpdateTaskCustomFieldValues(string ProjectName)
        {
            try
            {

                bool Isconnected = checkbothPWASiteConnection();
                if (!Isconnected)
                {
                    Console.Write("\nPress any key to exit: ");
                    Console.ReadKey(false);
                    return;
                }
                ProjectContext sourcecontext = common.SourceProjContext;
                ProjectContext destcontext = common.DestinationProjContext;
                // Retrive publish project named "New Project"
                // if you know the Guid of project, you can just call context.Projects.GetByGuid()
                csom.PublishedProject sourceproject = GetProjectByName(ProjectName, sourcecontext);
                csom.PublishedProject project = GetProjectByName(ProjectName, destcontext);
                if (project == null)
                {
                    Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                    return;
                }
                destcontext.Load(destcontext.CustomFields, csf => csf.Include(dcf => dcf.Name,
                    dcf => dcf.InternalName,
                    dcf => dcf.FieldType,
                    dcf => dcf.LookupTable,
                    dcf => dcf.LookupEntries,
                    dcf => dcf.LookupAllowMultiSelect
                     ));
                destcontext.ExecuteQuery();
                DraftProject draftProject = project.Draft;
                JobState job1 = common.DestinationProjContext.WaitForQueue(draftProject.CheckIn(true), 50);
                DraftProject draft = project.CheckOut();

                var draftCF = project.CustomFields.Cast<CustomField>().Select(cf => new { Name = cf.Name, InterNalName = cf.InternalName }).ToList();
                foreach (PublishedTask pubtask in sourceproject.Tasks)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    string taskName = pubtask.Name;
                    if (!string.IsNullOrEmpty(taskName))
                    {
                        string position = pubtask.OutlinePosition;
                        // Retrieve project along with tasks & assignments & resources
                        destcontext.Load(draft.Tasks, dt => dt.Where(t => t.Name == taskName && t.OutlinePosition == position));
                        //destcontext.Load(draft.Assignments, da => da.Where(a => a.Task.Name == taskName &&
                        //                                                    a.Resource.Name == localResourceName));
                        //destcontext.Load(draft.ProjectResources, dp => dp.Where(r => r.Name == localResourceName));
                        destcontext.ExecuteQuery();
                        // Make sure the data on server is right
                        //if (draft.Tasks.Count != 1 || draft.Assignments.Count != 1 || draft.ProjectResources.Count != 1)
                        //{
                        //    Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                        //    Console.ReadLine();
                        //    return;
                        //}
                        if (draft.Tasks.Count != 1)
                        {
                            Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                            Console.ReadLine();
                            return;
                        }
                        Console.WriteLine("Task Name: (" + taskName + ") update Custom Fields");
                        // Since we already filetered and validated that the TaskCollection, ProjectResourceCollection and AssignmentCollection
                        // contains just one filtered item each, we just get the first one.
                        csom.DraftTask task = draft.Tasks.First();
                        //csom.DraftProjectResource resource = draft.ProjectResources.First();
                        //csom.DraftAssignment assignment = draft.Assignments.First();
                        // Retrieve custom field by name
                        Dictionary<stringobject> ECFAskeyValuePairs = pubtask.FieldValues;
                        CustomFieldCollection LCFColl = pubtask.CustomFields;
                        foreach (CustomField cf in LCFColl)
                        {
                            string taskCFName = cf.Name;
                            // do something with the custom fields
                            //csom.CustomField projCF = destcontext.CustomFields.FirstOrDefault(cf => cf.Name == projectCFName);
                            csom.CustomField taskCF = destcontext.CustomFields.FirstOrDefault(ecf => ecf.Name == taskCFName);
                            //csom.CustomField resCF = destcontext.CustomFields.FirstOrDefault(cf => cf.Name == resourceCFName);
                            if (!taskCF.LookupTable.ServerObjectIsNull.HasValue ||
                                                            (taskCF.LookupTable.ServerObjectIsNull.HasValue && taskCF.LookupTable.ServerObjectIsNull.Value))
                            {
                                if (ECFAskeyValuePairs[cf.InternalName] == null)
                                {
                                    String textValue = "is not set";
                                    Console.WriteLine("\t{0, -8}   {1, -20}        ***{2}",
                                        cf.FieldType, cf.Name, textValue);
                                }
                                else
                                {
                                    // CustomFieldType is a CSOM enumeration of ECF types.
                                    switch (taskCF.FieldType)
                                    {
                                        case CustomFieldType.COST:
                                            decimal costValue = (decimal)ECFAskeyValuePairs[cf.InternalName];
                                            task[taskCF.InternalName] = costValue;
                                            break;

                                        case CustomFieldType.DATE:
                                            string dtstring = Convert.ToString(ECFAskeyValuePairs[cf.InternalName]);
                                            DateTime parsedt = Convert.ToDateTime(dtstring);
                                            DateTime dateTime = DateTime.SpecifyKind(parsedt, DateTimeKind.Utc);
                                            task[taskCF.InternalName] = dateTime;
                                            break;

                                        case CustomFieldType.FINISHDATE:
                                            string FINISHdtvalue = Convert.ToString(ECFAskeyValuePairs[cf.InternalName]);
                                            DateTime parsefinishdt = Convert.ToDateTime(FINISHdtvalue);
                                            DateTime finishdateTime = DateTime.SpecifyKind(parsefinishdt, DateTimeKind.Utc);
                                            //task[taskCF.InternalName] = finishdateTime;
                                            break;

                                        case CustomFieldType.DURATION:
                                            string DURATIONvalue = ECFAskeyValuePairs[cf.InternalName].ToString();
                                            task[taskCF.InternalName] = DURATIONvalue;
                                            break;

                                        case CustomFieldType.FLAG:
                                            bool FLAGvalue = Convert.ToBoolean(ECFAskeyValuePairs[cf.InternalName].ToString());
                                            task[taskCF.InternalName] = FLAGvalue;
                                            break;

                                        case CustomFieldType.NUMBER:
                                            long NUMBERvalue = Convert.ToInt32(ECFAskeyValuePairs[cf.InternalName].ToString());
                                            task[taskCF.InternalName] = NUMBERvalue;
                                            break;

                                        case CustomFieldType.TEXT:
                                            string value = Convert.ToString(ECFAskeyValuePairs[cf.InternalName]);
                                            task[taskCF.InternalName] = value;
                                            break;
                                    }
                                    draft.Update();
                                }
                            }
                            else
                            {
                                // Get random lookup table entry
                                String[] entries = (String[])ECFAskeyValuePairs[cf.InternalName];

                                foreach (String entry in entries)
                                {
                                    var sourceluEntry = sourcecontext.LoadQuery(cf.LookupTable.Entries
                                            .Where(e => e.InternalName == entry));
                                    sourcecontext.ExecuteQuery();
                                    if (sourceluEntry != null)
                                    {
                                        string entryfullValue = sourceluEntry.Cast<LookupEntry>().First().FullValue;
                                        var luEntry = destcontext.LoadQuery(taskCF.LookupTable.Entries
                                                .Where(e => e.FullValue == entryfullValue));

                                        destcontext.ExecuteQuery();
                                        csom.LookupEntry taskLookUpEntry = luEntry.Cast<LookupEntry>().First();

                                        task[taskCF.InternalName] = new[] { taskLookUpEntry.InternalName };
                                        draft.Update();
                                        //Console.WriteLine(" Yes    {0, -22}  {1}", luEntry.First().FullValue, luEntry.First().Description);
                                    }
                                }
                            }
                        }
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Task Name: (" + taskName + ") update Custom Fields is done.");
                    }
                    //Console.ReadKey();
                }
                var publishJob = draft.Publish(true);                
                csom.JobState jobState = destcontext.WaitForQueue(publishJob, DEFAULTTIMEOUTSECONDS);
                JobStateLog(jobState, "Project Name: (" + ProjectName + ") of task customfield update");

            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now + " : Exception - " + ex.Message);
                Console.Write("\nPress any key to exit: ");
                Console.ReadKey(false);
            }
        }

        /// <summary>
        /// Create Project/Task/Resource custom fields. Field type will be TEXT.
        /// </summary>
        private static void CreateCustomFields(ProjectContext context, string projectCFName, string taskCFName, string resourceCFName)
        {
            context.Load(context.EntityTypes.ProjectEntity);
            context.Load(context.EntityTypes.TaskEntity);
            context.Load(context.EntityTypes.ResourceEntity);
            context.ExecuteQuery();
            string lookupName = "SampleTable";
            string[] lookupValues = new string[] { "one""two""three" };
            // Create a simple lookup table with 3 values one/two/three
            csom.LookupTable lookupTB = CreateLookupTable(context, lookupName, lookupValues);
            // Create a project custom field with field type TEXT
            csom.CustomField projCF = context.CustomFields.Add(new csom.CustomFieldCreationInformation()
            {
                EntityType = context.EntityTypes.ProjectEntity,
                FieldType = csom.CustomFieldType.TEXT,
                Name = projectCFName,
                LookupTable = null
            });
            // Create a task custom field with field type TEXT, and with the lookup table just created
            csom.CustomField taskCF = context.CustomFields.Add(new csom.CustomFieldCreationInformation()
            {
                EntityType = context.EntityTypes.TaskEntity,
                FieldType = csom.CustomFieldType.TEXT,
                Name = taskCFName,
                LookupTable = lookupTB
            });
            // Create a resource custom field with field type TEXT
            csom.CustomField resourceCF = context.CustomFields.Add(new csom.CustomFieldCreationInformation()
            {
                EntityType = context.EntityTypes.ResourceEntity,
                FieldType = csom.CustomFieldType.TEXT,
                Name = resourceCFName,
            });
            context.CustomFields.Update();
            context.ExecuteQuery();
        }

        /// <summary>
        /// Create a simple lookup table with 3 text value choose (one/two/three)
        /// </summary>
        /// <returns></returns>
        private static csom.LookupTable CreateLookupTable(ProjectContext context, string lookupName, string[] lookupValues)
        {
            //string[] lookupValues = new string[] { "one", "two", "three" };
            csom.LookupTable lookupTable = context.LookupTables.Add(new csom.LookupTableCreationInformation()
            {
                Name = lookupName,
                SortOrder = csom.LookupTableSortOrder.Ascending,
                Entries = lookupValues.Select((val, i) =>
                          new csom.LookupEntryCreationInformation()
                          {
                              Value = new csom.LookupEntryValue() { TextValue = val },
                              SortIndex = i
                          }).ToArray(),
                Masks = new csom.LookupMask[] { new csom.LookupMask()
                                                {
                                                    MaskType = csom.LookupTableMaskSequence.CHARACTERS,
                                                    Length = 0,
                                                    Separator = "."
                                                 }
                                               }
            });
            context.LookupTables.Update();
            context.ExecuteQuery();
            return lookupTable;
        }

        private static csom.LookupEntry GetRandomLookupEntries(ProjectContext context, csom.CustomField cf)
        {
            context.Load(cf, c => c, c => c.LookupEntries);
            context.ExecuteQuery();
            try
            {
                Random r = new Random();
                int index = r.Next(0, cf.LookupEntries.Count);
                csom.LookupEntry lookUpEntry = cf.LookupEntries[index];
                context.Load(lookUpEntry);
                context.ExecuteQuery();
                return lookUpEntry;
            }
            catch (CollectionNotInitializedException ex)
            {
                return null;
            }
        }

        /// <summary>
        /// Log to Console the job state for queued jobs
        /// </summary>
        /// <param name="jobState">csom jobstate</param>
        /// <param name="jobDescription">job description</param>
        private static void JobStateLog(csom.JobState jobState, string jobDescription)
        {
            switch (jobState)
            {
                case csom.JobState.Success:
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(jobDescription + " is successfully done.");
                    break;

                case csom.JobState.ReadyForProcessing:
                case csom.JobState.Processing:
                case csom.JobState.ProcessingDeferred:
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(jobDescription + " is taking longer than usual.");
                    break;

                case csom.JobState.Failed:
                case csom.JobState.FailedNotBlocking:
                case csom.JobState.CorrelationBlocked:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(jobDescription + " failed. The job is in state: " + jobState);
                    break;

                default:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Unkown error, job is in state " + jobState);
                    break;
            }
        }
    }
}



////////////// ConsoleTable.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace ProjectImportConsoleApp
{
    public class ConsoleTable
    {
        public IList<object> Columns { getset; }
        public IList<object[]> Rows { getprotected set; }

        public ConsoleTableOptions Options { getprotected set; }
        public Type[] ColumnTypes { getprivate set; }

        public static HashSet<Type> NumericTypes = new HashSet<Type>
        {
            typeof(int),  typeof(double),  typeof(decimal),
            typeof(long), typeof(short),   typeof(sbyte),
            typeof(byte), typeof(ulong),   typeof(ushort),
            typeof(uint), typeof(float)
        };

        public ConsoleTable(params string[] columns)
            : this(new ConsoleTableOptions { Columns = new List<string>(columns) })
        {
        }

        public ConsoleTable(ConsoleTableOptions options)
        {
            Options = options ?? throw new ArgumentNullException("options");
            Rows = new List<object[]>();
            Columns = new List<object>(options.Columns);
        }

        public ConsoleTable AddColumn(IEnumerable<string> names)
        {
            foreach (var name in names)
                Columns.Add(name);
            return this;
        }

        public ConsoleTable AddRow(params object[] values)
        {
            if (values == null)
                throw new ArgumentNullException(nameof(values));

            if (!Columns.Any())
                throw new Exception("Please set the columns first");

            if (Columns.Count != values.Length)
                throw new Exception(
                    $"The number columns in the row ({Columns.Count}) does not match the values ({values.Length}");

            Rows.Add(values);
            return this;
        }

        public ConsoleTable Configure(Action<ConsoleTableOptions> action)
        {
            action(Options);
            return this;
        }

        public static ConsoleTable From<T>(IEnumerable<T> values)
        {
            var table = new ConsoleTable
            {
                ColumnTypes = GetColumnsType<T>().ToArray()
            };

            var columns = GetColumns<T>();

            table.AddColumn(columns);

            foreach (
                var propertyValues
                in values.Select(value => columns.Select(column => GetColumnValue<T>(value, column)))
            ) table.AddRow(propertyValues.ToArray());

            return table;
        }

        public override string ToString()
        {
            var builder = new StringBuilder();

            // find the longest column by searching each row
            var columnLengths = ColumnLengths();

            // set right alinment if is a number
            var columnAlignment = Enumerable.Range(0, Columns.Count)
                .Select(GetNumberAlignment)
                .ToList();

            // create the string format with padding
            var format = Enumerable.Range(0, Columns.Count)
                .Select(i => " | {" + i + "," + columnAlignment[i] + columnLengths[i] + "}")
                .Aggregate((s, a) => s + a) + " |";

            // find the longest formatted line
            var maxRowLength = Math.Max(0, Rows.Any() ? Rows.Max(row => string.Format(format, row).Length) : 0);
            var columnHeaders = string.Format(format, Columns.ToArray());

            // longest line is greater of formatted columnHeader and longest row
            var longestLine = Math.Max(maxRowLength, columnHeaders.Length);

            // add each row
            var results = Rows.Select(row => string.Format(format, row)).ToList();

            // create the divider
            var divider = " " + string.Join("", Enumerable.Repeat("-", longestLine - 1)) + " ";

            builder.AppendLine(divider);
            builder.AppendLine(columnHeaders);

            foreach (var row in results)
            {
                builder.AppendLine(divider);
                builder.AppendLine(row);
            }

            builder.AppendLine(divider);

            if (Options.EnableCount)
            {
                builder.AppendLine("");
                builder.AppendFormat(" Count: {0}", Rows.Count);
            }

            return builder.ToString();
        }

        public string ToMarkDownString()
        {
            return ToMarkDownString('|');
        }

        private string ToMarkDownString(char delimiter)
        {
            var builder = new StringBuilder();

            // find the longest column by searching each row
            var columnLengths = ColumnLengths();

            // create the string format with padding
            var format = Format(columnLengths, delimiter);

            // find the longest formatted line
            var columnHeaders = string.Format(format, Columns.ToArray());

            // add each row
            var results = Rows.Select(row => string.Format(format, row)).ToList();

            // create the divider
            var divider = Regex.Replace(columnHeaders, @"[^|]""-");

            builder.AppendLine(columnHeaders);
            builder.AppendLine(divider);
            results.ForEach(row => builder.AppendLine(row));

            return builder.ToString();
        }

        public string ToMinimalString()
        {
            return ToMarkDownString(char.MinValue);
        }

        public string ToStringAlternative()
        {
            var builder = new StringBuilder();

            // find the longest column by searching each row
            var columnLengths = ColumnLengths();

            // create the string format with padding
            var format = Format(columnLengths);

            // find the longest formatted line
            var columnHeaders = string.Format(format, Columns.ToArray());

            // add each row
            var results = Rows.Select(row => string.Format(format, row)).ToList();

            // create the divider
            var divider = Regex.Replace(columnHeaders, @"[^|]""-");
            var dividerPlus = divider.Replace("|""+");

            builder.AppendLine(dividerPlus);
            builder.AppendLine(columnHeaders);

            foreach (var row in results)
            {
                builder.AppendLine(dividerPlus);
                builder.AppendLine(row);
            }
            builder.AppendLine(dividerPlus);

            return builder.ToString();
        }

        private string Format(List<int> columnLengths, char delimiter = '|')
        {
            // set right alinment if is a number
            var columnAlignment = Enumerable.Range(0, Columns.Count)
                .Select(GetNumberAlignment)
                .ToList();

            var delimiterStr = delimiter == char.MinValue ? string.Empty : delimiter.ToString();
            var format = (Enumerable.Range(0, Columns.Count)
                .Select(i => " " + delimiterStr + " {" + i + "," + columnAlignment[i] + columnLengths[i] + "}")
                .Aggregate((s, a) => s + a) + " " + delimiterStr).Trim();
            return format;
        }

        private string GetNumberAlignment(int i)
        {
            return Options.NumberAlignment == Alignment.Right
                    && ColumnTypes != null
                    && NumericTypes.Contains(ColumnTypes[i])
                ? ""
                : "-";
        }

        private List<int> ColumnLengths()
        {
            var columnLengths = Columns
                .Select((t, i) => Rows.Select(x => x[i])
                    .Union(new[] { Columns[i] })
                    .Where(x => x != null)
                    .Select(x => x.ToString().Length).Max())
                .ToList();
            return columnLengths;
        }

        public void Write(Format format = ProjectImportConsoleApp.Format.Default)
        {
            switch (format)
            {
                case ProjectImportConsoleApp.Format.Default:
                    Options.OutputTo.WriteLine(ToString());
                    break;
                case ProjectImportConsoleApp.Format.MarkDown:
                    Options.OutputTo.WriteLine(ToMarkDownString());
                    break;
                case ProjectImportConsoleApp.Format.Alternative:
                    Options.OutputTo.WriteLine(ToStringAlternative());
                    break;
                case ProjectImportConsoleApp.Format.Minimal:
                    Options.OutputTo.WriteLine(ToMinimalString());
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(format), format, null);
            }
        }

        private static IEnumerable<string> GetColumns<T>()
        {
            return typeof(T).GetProperties().Select(x => x.Name).ToArray();
        }

        private static object GetColumnValue<T>(object target, string column)
        {
            return typeof(T).GetProperty(column).GetValue(target, null);
        }

        private static IEnumerable<Type> GetColumnsType<T>()
        {
            return typeof(T).GetProperties().Select(x => x.PropertyType).ToArray();
        }
    }

    public class ConsoleTableOptions
    {
        public IEnumerable<string> Columns { getset; } = new List<string>();
        public bool EnableCount { getset; } = true;

        /// <summary>
        /// Enable only from a list of objects
        /// </summary>
        public Alignment NumberAlignment { getset; } = Alignment.Left;

        /// <summary>
        /// The <see cref="TextWriter"/> to write to. Defaults to <see cref="Console.Out"/>.
        /// </summary>
        public TextWriter OutputTo { getset; } = Console.Out;
    }

    public enum Format
    {
        Default = 0,
        MarkDown = 1,
        Alternative = 2,
        Minimal = 3
    }

    public enum Alignment
    {
        Left,
        Right
    }

    #region example help to call table class
    //public class help
    //{
    //    var table = new ConsoleTable("one", "two", "three");
    //    table.AddRow(1, 2, 3)
    //             .AddRow("this line should be longer", "yes it is", "oh");

    //    Console.WriteLine("\nFORMAT: Default:\n");
    //        table.Write();

    //        Console.WriteLine("\nFORMAT: MarkDown:\n");
    //        table.Write(Format.MarkDown);

    //        Console.WriteLine("\nFORMAT: Alternative:\n");
    //        table.Write(Format.Alternative);
    //        Console.WriteLine();

    //        Console.WriteLine("\nFORMAT: Minimal:\n");
    //        table.Write(Format.Minimal);
    //        Console.WriteLine();

    //        table = new ConsoleTable("I've", "got", "nothing");
    //    table.Write();
    //        Console.WriteLine();

    //        var rows = Enumerable.Repeat(new Something(), 10);



    //    ConsoleTable.From<Something>(rows).Write();

    //    rows = Enumerable.Repeat(new Something(), 0);
    //        ConsoleTable.From<Something>(rows).Write();

    //    Console.WriteLine("\nNumberAlignment = Alignment.Right\n");
    //        rows = Enumerable.Repeat(new Something(), 2);
    //        ConsoleTable
    //            .From(rows)
    //            .Configure(o => o.NumberAlignment = Alignment.Right)
    //            .Write();

    //    var noCount =
    //    new ConsoleTable(new ConsoleTableOptions
    //    {
    //        Columns = new[] { "one", "two", "three" },
    //        EnableCount = false
    //    });

    //    noCount.AddRow(1, 2, 3).Write();

    //    Console.ReadKey();
    //}
    #endregion
}



///////////// Main.cs

using Microsoft.Office.Interop.MSProject;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;

namespace ProjectImportConsoleApp
{
    //C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.MSProject\15.0.0.0__71e9bce111e9429c
    internal class Program
    {
        private static appSettings settings = new appSettings();
        private static common cmn = new common();
        private static void startSource()
        {
            var objProcess = Process.Start(@"winproj.exe", string.Format("/s {0}", settings.SourceSite));
        }

        private async static void startSource1()
        {
            await Task.Run(() =>
            {
                var objProcess = Process.Start(@"winproj.exe", string.Format("/s {0}", settings.SourceSite));
            });
        }

        private async static void startDestination()
        {
            //var objProcess = Process.Start(@"winproj.exe", "/s https://projectonlinepremium.sharepoint.com/sites/pwa-developer/");
            await Task.Run(() =>
            {
                var objProcess = Process.Start(@"winproj.exe", string.Format("/s {0}", settings.DestinationSite));
            });
        }

        public static void PublishProject(AbstractBar bar,int wait)
        {
            string newfileName;
            int i = 0;
            string status = "Successfully..";
            string MPPExportFolder = PathAddBackslash(settings.ExportDirlocation) + DateTime.Now.ToString("dddd_ddMMMMyyyy");
            //string MPPExportFolder = @"D:\ExportedMPP\Wednesday15January2020";
            string path = PathAddBackslash(MPPExportFolder);
            Console.ForegroundColor = ConsoleColor.Yellow;
            bar.PrintMessage("start connecting...");
            Console.WriteLine("MSProject Application process start connecting with site '" + settings.DestinationSite + "'");
            var backgroundThread = new Thread(new ThreadStart(startDestination));
            backgroundThread.Start();
            Thread.Sleep(wait);
            Console.WriteLine("MSProject Application process connected with site '" + settings.DestinationSite + "'");
            Console.ForegroundColor = ConsoleColor.Green; bar.PrintMessage("connected with site...");
            object checkIn = true;
            object noAuto = System.Reflection.Missing.Value;

            DirectoryInfo dinfo = new DirectoryInfo(path);
            FileInfo[] Files = dinfo.GetFiles("*.mpp");
            ApplicationClass appProj = new ApplicationClass
            {
                Visible = false
            };
            //ApplicationClass appProj = new ApplicationClass();
            Thread.Sleep(wait);
            //appProj.Visible = false;
            appProj.DisplayAlerts = false;

            Console.ForegroundColor = ConsoleColor.Green;
            var table = new ConsoleTable(new ConsoleTableOptions
            {
                Columns = new[] { "#SRNo","Project mpp name""Import status" },
                EnableCount = false,
                NumberAlignment = Alignment.Right
            });
            try
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                foreach (FileInfo file in Files)
                {
                    bar.Step();
                    newfileName = file.Name;
                    Console.WriteLine("Start Import this '" + newfileName + "'.");
                    appProj.FileOpenEx(Name: path + newfileName);
                    Project aactiveProject = appProj.ActiveProject;
                    appProj.FileSaveAs(Name: @"<>\" + aactiveProject.Name, FormatID: "");
                    appProj.Publish();
                    string ProjectName = aactiveProject.Name;                    
                    appProj.FileCloseEx(PjSaveType.pjDoNotSave, checkIn);                    
                    Console.WriteLine("Import this '" + newfileName + "' successfully..");                    
                    Thread.Sleep(5000);
                    table.AddRow(++i, newfileName,status);
                }                
                appProj.Quit(PjSaveType.pjDoNotSave);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("All MPP published sucessfully...");
                table.Write();
                Console.ForegroundColor = ConsoleColor.Yellow;
                var authwithSource = cmn.Auth(settings.UserId, settings.Password, settings.SourceSite);
                common.SourceProjContext = authwithSource.Value;
                common.OutPutKey = authwithSource.Key;
                if (common.OutPutKey == "True")
                {
                    Console.WriteLine("PWA Source Site Connected Sucessfully!!");
                }
                else if (common.OutPutKey == "False")
                {
                    Console.WriteLine("PWA Source Site Invalid Credentials!!");
                    Console.Write("\nPress any key to exit: ");
                    Console.ReadKey(false);
                    return;
                }
                var authWithTarget = cmn.Auth(settings.UserId, settings.Password, settings.DestinationSite);
                common.DestinationProjContext = authWithTarget.Value;
                common.OutPutKey = authWithTarget.Key;
                if (common.OutPutKey == "True")
                {
                    Console.WriteLine("PWA Target Site Connected Sucessfully!!");
                }
                else if (common.OutPutKey == "False")
                {
                    Console.WriteLine("PWA Target Site Invalid Credentials!!");
                    Console.Write("\nPress any key to exit: ");
                    Console.ReadKey(false);
                    return;
                }
                var CheckedOutProjects = common.DestinationProjContext.Projects.Where(pr => pr.IsCheckedOut == true).Cast<Microsoft.ProjectServer.Client.Project>().ToList();
                foreach (var proj in CheckedOutProjects)
                {
                    string Name = ((Microsoft.ProjectServer.Client.PublishedProject)proj).Draft.Name;
                    Microsoft.ProjectServer.Client.DraftProject draftproj = ((Microsoft.ProjectServer.Client.PublishedProject)proj).Draft;
                    Microsoft.ProjectServer.Client.JobState job1 = common.DestinationProjContext.WaitForQueue(draftproj.CheckIn(true), 50);
                    if (job1.Equals(Microsoft.ProjectServer.Client.JobState.Success))
                    {
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine(Name + " Draft Checkin Sucessfully!!");
                    }
                }
                foreach (FileInfo file in Files)
                {                   
                    string ProjectName = Path.GetFileNameWithoutExtension(file.FullName);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Project Name: " + ProjectName + " Start Enterprise Custom Fields update!!");
                    cmn.UpdateCustomFieldValue(ProjectName);
                }
                System.Windows.Forms.MessageBox.Show("Runing automation import task completed.");
                Console.Write("\nPress any key to exit: ");
                Console.ReadKey(false);
            }
            catch (System.Exception ex)
            {
                appProj.FileCloseEx(PjSaveType.pjDoNotSave, checkIn);
                appProj.Quit(PjSaveType.pjDoNotSave);
                Console.ForegroundColor = ConsoleColor.Red;
                bar.PrintMessage(ex.Message);
                //Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ResetColor();
            }
        }

        private static List<Properties> FetchAllProjects()
        {
            pwaInstance pwa = new pwaInstance();
            List<Properties> properties = null;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Source Site Url: " + settings.SourceSite);
            Console.WriteLine("RunQuery to read projects from source...");
            try
            {
                string jsonResult = pwa.RunQuery(settings.queryReadProjects);
                //string jsonResult = RunQuery("/_api/projectServer/EnterpriseResources?$select=ResourceId,EmployeeNumber,ResourceStandardRate,OracleHomeCostCenter,Recoverable,ResourceMaxUnits,ResourceDepartments");
                if (jsonResult == null)
                    return null;
                RootObjectResponse Response = JsonConvert.DeserializeObject<RootObjectResponse>(jsonResult);
                List<ProjectsEntry> entries = Response.Feed.ProjectsEntry;
                properties = entries.Where(ent => !string.IsNullOrEmpty(ent.Content.Properties.EnterpriseProjectTypeName)).Select(en => en.Content.Properties).ToList();

                int index = 0;
                var rows = properties.Select(pr => new PropetiesView() { SRNo = ++index, ProjectName = pr.ProjectName, EnterpriseProjectTypeName = pr.EnterpriseProjectTypeName }).AsEnumerable();
                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                var noCount = new ConsoleTable(new ConsoleTableOptions
                {
                    Columns = new[] { "#SRNo""Project Name""Enterprise Project Type" },
                    EnableCount = false,
                    NumberAlignment = Alignment.Right
                });
                rows.ToList().ForEach(rw =>
                {
                    noCount.AddRow(rw.SRNo, rw.ProjectName, rw.EnterpriseProjectTypeName);
                });
                noCount.Write();
                //Console.ReadKey();
            }
            catch (System.Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now + " : Exception - " + ex.Message);
            }
            return properties;
        }

        private static string PathAddBackslash(string path)
        {
            // They're always one character but EndsWith is shorter than
            // array style access to last path character. Change this
            // if performance are a (measured) issue.
            string separator1 = Path.DirectorySeparatorChar.ToString();
            string separator2 = Path.AltDirectorySeparatorChar.ToString();

            // Trailing white spaces are always ignored but folders may have
            // leading spaces. It's unusual but it may happen. If it's an issue
            // then just replace TrimEnd() with Trim(). Tnx Paul Groke to point this out.
            path = path.TrimEnd();

            // Argument is always a directory name then if there is one
            // of allowed separators then I have nothing to do.
            if (path.EndsWith(separator1) || path.EndsWith(separator2))
                return path;

            // If there is the "alt" separator then I add a trailing one.
            // Note that URI format (file://drive:\path\filename.ext) is
            // not supported in most .NET I/O functions then we don't support it
            // here too. If you have to then simply revert this check:
            // if (path.Contains(separator1))
            //     return path + separator1;
            //
            // return path + separator2;
            if (path.Contains(separator2))
                return path + separator2;

            // If there is not an "alt" separator I add a "normal" one.
            // It means path may be with normal one or it has not any separator
            // (for example if it's just a directory name). In this case I
            // default to normal as users expect.
            return path + separator1;
        }

        public async static void ExportMPP(AbstractBar bar, int wait)
        {
            ApplicationClass objApplication = null;
            bool IsException = false;
            try
            {
                //var backgroundThread = new Thread(new ThreadStart(startSource));
                //backgroundThread.Start();
                //Thread.Sleep(10000);
                //Console.WriteLine("MSProject Application process start connecting with site '" + settings.SourceSite + "'");
                //Task task = new Task(startSource);
                //task.Start();
                //task.Wait();
                //Console.WriteLine("MSProject Application process connected with site '" + settings.SourceSite + "'");
                ApplicationClass objProject = new ApplicationClass();
                Console.WriteLine("MSProject Application class ready...");
                Thread.Sleep(wait);
                objProject.Visible = false;
                objApplication = objProject;
                List<Properties> properties = FetchAllProjects();
                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.Yellow;
                string root = settings.ExportDirlocation;
                string MPPExportFolder = PathAddBackslash(settings.ExportDirlocation) + DateTime.Now.ToString("dddd_ddMMMMyyyy");
                if (Directory.Exists(root))
                {
                    if (!Directory.Exists(MPPExportFolder))
                    {
                        Directory.CreateDirectory(MPPExportFolder);
                        Console.WriteLine("MPP export folder location '" + MPPExportFolder + "' Created...");
                    }
                }
                Console.WriteLine("Start to export all MPP inside this folder '" + MPPExportFolder + "'");

                var table = new ConsoleTable(new ConsoleTableOptions
                {
                    Columns = new[] { "#SRNo""Project name""Project mpp name""Export status" },
                    EnableCount = false,
                    NumberAlignment = Alignment.Right
                });
                int i = 0;
                string status = "Successfully..";
                foreach (Properties property in properties)
                {
                    string Mppfilename = property.ProjectName + ".mpp";
                    //string File = string.Format("{0}{1}", PathAddBackslash(MPPExportFolder), Mppfilename);
                    //object mppFile = string.Format("{0}{1}", PathAddBackslash(MPPExportFolder), property.ProjectName);
                    //object oMissing = System.Reflection.Missing.Value;
                    //object oFile = string.Format(@"<>\{0}", property.ProjectName);
                    //object oFormat = "MSProject.mpp";
                    //object oReadOnly = true;
                    //objProject.DisplayAlerts = false;
                    //object MPPFileExport = File;
                    //objProject.FileOpen(oFile, oReadOnly, PjMergeType.pjDoNotMerge, oMissing,oMissing, oMissing, oMissing, oUserId, oMissing, oFormat, oMissing, PjPoolOpen.pjPoolReadOnly, oPassword, oMissing, oMissing, oMissing);
                    //objProject.FileOpenEx(oFile, oReadOnly, PjMergeType.pjDoNotMerge, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oFormat, oMissing, PjPoolOpen.pjPoolReadOnly, oMissing, oMissing, oReadOnly, oMissing);
                    //objProject.FileSaveAs(mppFile, PjFileFormat.pjMPP);
                    //objProject.FileCloseEx(PjSaveType.pjDoNotSave, true);
                    Task<bool> ExportedMppTask = fileOpenEx(MPPExportFolder, objProject, property);
                    bool IsExportedMpp = await ExportedMppTask;
                    if (IsExportedMpp == true)
                    {
                        table.AddRow(++i, property.ProjectName, Mppfilename, status);
                    }
                }
                Console.ForegroundColor = ConsoleColor.Green;
                table.Write();
                objProject.Quit(PjSaveType.pjDoNotSave);
                objApplication = null;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("All MPP exported to folder '" + MPPExportFolder + "' successfully...");
                System.Windows.Forms.MessageBox.Show("Runing automation task completed.");
                Console.Write("\nPress any key to exit: ");
                Console.ReadKey(false);
            }
            catch (System.Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now + " : Exception - " + ex.Message);
                if (objApplication != null)
                    objApplication.Quit(PjSaveType.pjDoNotSave);
                IsException = true;
                Console.Write("\nPress any key to exit: ");
                Console.ReadKey(false);
            }
            finally
            {
                if (IsException == false && objApplication != null)
                    objApplication.Quit(PjSaveType.pjDoNotSave);
                Console.ResetColor();
            }
        }

        public static async Task<bool> fileOpenEx(string MPPExportFolder, ApplicationClass objProject, Properties property)
        {
            bool IsfileOpen = false;
            await Task.Run(() =>
            {
                string Mppfilename = property.ProjectName + ".mpp";
                string File = string.Format("{0}{1}", PathAddBackslash(MPPExportFolder), Mppfilename);
                object mppFile = string.Format("{0}{1}", PathAddBackslash(MPPExportFolder), property.ProjectName);
                object oMissing = System.Reflection.Missing.Value;
                object oFile = string.Format(@"<>\{0}", property.ProjectName);
                object oFormat = "MSProject.mpp";
                object oReadOnly = true;
                objProject.DisplayAlerts = false;
                object MPPFileExport = File;
                IsfileOpen = objProject.FileOpenEx(oFile, oReadOnly, PjMergeType.pjDoNotMerge, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oFormat, oMissing, PjPoolOpen.pjPoolReadOnly, oMissing, oMissing, oReadOnly, oMissing);
                objProject.FileSaveAs(mppFile, PjFileFormat.pjMPP);
                objProject.FileCloseEx(PjSaveType.pjDoNotSave, true);
            });
            return IsfileOpen;
        }

        private static void StartExport()
        {
            Console.WriteLine("MSProject Application process start connecting with site '" + settings.SourceSite + "'");
            var backgroundThread = new Thread(new ThreadStart(startSource));
            backgroundThread.Start();
            Thread.Sleep(10000);
            Console.WriteLine("MSProject Application process connected with site '" + settings.SourceSite + "'");
            // Make COM calls.
            // If directory does not exist, create it.
            string root = settings.ExportDirlocation;
            if (!Directory.Exists(root))
            {
                Directory.CreateDirectory(root);
                Console.WriteLine("MPP export Directory '" + root + "' Created...");
            }
            AbstractBar bar;
            bar = new AnimatedBar();
            int wait = 10000;           

            //Animated bar
            //Test(bar, wait, end);
            //Sway bar
            bar = new SwayBar();
            Action actionEXPORT = () => ExportMPP(bar, wait);
            Task task = new Task(actionEXPORT);
            task.Start();
            task.Wait();
            //PublishProject();
            //FetchAllProjects();
            Console.ReadKey(false);
        }
        [STAThread]
        private static void Main(string[] args)
        {
            OleMessageFilter.Register();            
            
            string myChoice;
            Console.WriteLine("\nPress E(Export) and I(Import).");
            string userChoice = Console.ReadLine();
            string execAction = "";
            if (userChoice == ConsoleKey.E.ToString())
            {
                execAction = "Export";
                Console.WriteLine("Would you Like to Start MPP Export Process?");
                Console.WriteLine("\nPress Y(Yes) to start the MPP Export Process.");
            }
            else if (userChoice == ConsoleKey.I.ToString())
            {
                execAction = "Import";
                Console.WriteLine("Would you Like to Start MPP Import Process?");
                Console.WriteLine("\nPress Y(Yes) to start the MPP Import Process.");
            }
            myChoice = Console.ReadLine();
            //Starting a Switch-Case Condition Cheking
            try
            {
                switch (myChoice)
                {
                    //If user Enter Y then Call the
                    // StartMPPExportProcess() Method
                    case "Y":
                        if (execAction.Equals("Export"))
                        {
                            StartExport();
                        }
                        else if (execAction.Equals("Import"))
                        {
                            AbstractBar bar;
                            int wait = 10000;

                            //Animated bar
                            //Test(bar, wait, end);
                            //Sway bar
                            bar = new SwayBar();
                            //Action actionEXPORT = () => PublishProject(bar, wait);
                            PublishProject(bar, wait);
                        }
                        Console.ReadKey();
                        break;
                    default:
                        //Otherwise just Print The Error Message
                        Console.WriteLine("You have Enetered the Wrong Key.");
                        Console.ReadKey();
                        break;
                }
            }
            catch (System.Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(DateTime.Now + " : Exception - " + ex.Message);
                Console.Write("\nPress any key to exit: ");
                Console.ReadKey(false);
            }            
            OleMessageFilter.Revoke();
            //Console.Write("\nPress any key to exit: ");
        } //end of Main
        
        private static void Main1(string[] args)
        {
            string str=DateTime.Now.ToString("dddd_ddMMMMyyyy");
            common.UpdateTaskCustomFieldValues("Training_Project1");
            Console.Write("\nPress any key to exit: ");
            Console.ReadKey(false);
            //var authwithSource = cmn.Auth(settings.UserId, settings.Password, settings.SourceSite);
            //common.SourceProjContext = authwithSource.Value;
            //common.OutPutKey = authwithSource.Key;
            //if (common.OutPutKey == "True")
            //{
            //    Console.WriteLine("PWA Source Site Connected Sucessfully!!");
            //}
            //else if (common.OutPutKey == "False")
            //{
            //    Console.WriteLine("PWA Source Site Invalid Credentials!!");
            //    Console.Write("\nPress any key to exit: ");
            //    Console.ReadKey(false);
            //    return;
            //}
            //var authWithTarget = cmn.Auth(settings.UserId, settings.Password, settings.DestinationSite);
            //common.DestinationProjContext = authWithTarget.Value;
            //common.OutPutKey = authWithTarget.Key;
            //if (common.OutPutKey == "True")
            //{
            //    Console.WriteLine("PWA Target Site Connected Sucessfully!!");
            //}
            //else if (common.OutPutKey == "False")
            //{
            //    Console.WriteLine("PWA Target Site Invalid Credentials!!");
            //    Console.Write("\nPress any key to exit: ");
            //    Console.ReadKey(false);
            //    return;
            //}
            //var CheckedOutProjects = common.DestinationProjContext.Projects.Where(pr => pr.IsCheckedOut==true).Cast<Microsoft.ProjectServer.Client.Project>().ToList();
            //foreach(var proj in CheckedOutProjects)
            //{
            //    string Name = ((Microsoft.ProjectServer.Client.PublishedProject)proj).Draft.Name;
            //    Microsoft.ProjectServer.Client.DraftProject draftproj = ((Microsoft.ProjectServer.Client.PublishedProject)proj).Draft;
            //    Microsoft.ProjectServer.Client.JobState job1 = common.DestinationProjContext.WaitForQueue(draftproj.CheckIn(true), 50);
            //    if (job1.Equals(Microsoft.ProjectServer.Client.JobState.Success))
            //    {
            //        Console.WriteLine(Name+" Draft Checkin Sucessfully!!");
            //    }
            //}
        }
        ///////
        public static void Test(AbstractBar bar, int wait, int end)
        {
            bar.PrintMessage("Loading...");
            for (int cont = 0; cont < end; cont++)
            {
                bar.Step();
                Thread.Sleep(wait);
            }
            bar.PrintMessage("Finished");
        }
    }
}



//////////////// Message

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace ProjectImportConsoleApp
{
    public class RSACryptoService
    {
        static public byte[] Encryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
        {
            try
            {
                byte[] encryptedData;
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    RSA.ImportParameters(RSAKey);
                    encryptedData = RSA.Encrypt(Data, DoOAEPPadding);
                }
                return encryptedData;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        static public byte[] Decryption(byte[] Data, RSAParameters RSAKey, bool DoOAEPPadding)
        {
            try
            {
                byte[] decryptedData;
                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    RSA.ImportParameters(RSAKey);
                    decryptedData = RSA.Decrypt(Data, DoOAEPPadding);
                }
                return decryptedData;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(e.ToString());
                return null;
            }
        }
    }
}









///////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ProjectImportConsoleApp
{
    public class OleMessageFilter : IOleMessageFilter
    {
        public static void Register()
        {
            IOleMessageFilter newFilter = new OleMessageFilter();
            IOleMessageFilter oldFilter = null;

            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
            {
                CoRegisterMessageFilter(newFilter, out oldFilter);
            }
            else
            {
                throw new COMException("Unable to register message filter because the current thread apartment state is not STA.");
            }
        }

        public static void Revoke()
        {
            IOleMessageFilter oldFilter = null;
            CoRegisterMessageFilter(nullout oldFilter);
        }

        int IOleMessageFilter.HandleInComingCall(
            int dwCallType,
            System.IntPtr hTaskCaller,
            int dwTickCount,
            System.IntPtr lpInterfaceInfo)
        {
            return (int)SERVERCALL.SERVERCALL_ISHANDLED;
        }

        int IOleMessageFilter.RetryRejectedCall(
            System.IntPtr hTaskCallee,
            int dwTickCount,
            int dwRejectType)
        {
            if (dwRejectType == (int)SERVERCALL.SERVERCALL_RETRYLATER)
            {
                return 99;
            }

            return -1;
        }

        int IOleMessageFilter.MessagePending(
            System.IntPtr hTaskCallee,
            int dwTickCount,
            int dwPendingType)
        {
            return (int)PENDINGMSG.PENDINGMSG_WAITDEFPROCESS;
        }

        [DllImport("Ole32.dll")]
        private static extern int CoRegisterMessageFilter(
            IOleMessageFilter newFilter,
            out IOleMessageFilter oldFilter);
    }

    enum SERVERCALL
    {
        SERVERCALL_ISHANDLED = 0,
        SERVERCALL_REJECTED = 1,
        SERVERCALL_RETRYLATER = 2
    }

    enum PENDINGMSG
    {
        PENDINGMSG_CANCELCALL = 0,
        PENDINGMSG_WAITNOPROCESS = 1,
        PENDINGMSG_WAITDEFPROCESS = 2
    }

    [ComImport(), Guid("00000016-0000-0000-C000-000000000046"),
    InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    interface IOleMessageFilter
    {
        [PreserveSig]
        int HandleInComingCall(
            int dwCallType,
            IntPtr hTaskCaller,
            int dwTickCount,
            IntPtr lpInterfaceInfo);

        [PreserveSig]
        int RetryRejectedCall(
            IntPtr hTaskCallee,
            int dwTickCount,
            int dwRejectType);

        [PreserveSig]
        int MessagePending(
            IntPtr hTaskCallee,
            int dwTickCount,
            int dwPendingType);
    }

}