none
Checkout and Update a Project Using Managed Code RRS feed

  • Question

  • Is it possible using C# and the object model to actually open a project from project server 2007, update the values, and check back in?  I cannot use PSI because I am updating timephased fields (Actual Cost), and I can't find how to do that with PSI.  So I have written C# code that will open a MPP file, update Actual Cost, and save the file.  Now I need to do that with instead of a MPP file, a project from project server.  Any help would be appreciated!


    • Edited by heavywoody Wednesday, January 4, 2012 11:00 PM
    Wednesday, January 4, 2012 11:00 PM

Answers

  • I wanted to put this down in case someone else has run into this and needs help.  All the answsers were on this forum, but I just had to put them together so they would make sense to me.  So where is how I accomplished automated the entire checkout/update/checkin using C# and Office Interop

    I wanted to make sure credit goes to this fine article, which a lot of the code and ideas were borrowed from:

    http://epmdatapop.codeplex.com/SourceControl/list/changesets

     

    public Boolean SetActualCost(string serverurl, string projectname,int taskid, string month, string year, string newvalue, Boolean publish = false)
            {
                try
                {
                    dictErrors = new Dictionary<string, string>();
                    object readOnly = false;
                    Project wp = new Project(serverurl);
                    wp.Open(projectname);
                    Boolean bFound = false;
                   
     //Loop through all the tasks and find the taskID to match the one looking for
                    foreach (Task task in wp.app.ActiveProject.Tasks)
                    {
                        if (task.ID == taskid)
                        {
                                //Pass in the values to the assigment in order to set the approprimate month.  We always hit the middle of the month.
                                var tsv = task.TimeScaleData(month + "/15/" + year, month + " / 15 / " + year, PjTaskTimescaledData.pjTaskTimescaledActualCost, PjTimescaleUnit.pjTimescaleMonths, 1);
                                foreach (Microsoft.Office.Interop.MSProject.TimeScaleValue ts in tsv)
                                {
                                    try
                                    {
      //Update the field.  This is actual cost.
                                        ts.Value = newvalue;
                                        bFound = true;
                                    }
                                    catch (System.Exception ex)
                                    {
                                        AddError("1001", ex.Message);
                                    }
                                }
                                if (bFound == false)
                                {
                                    AddError("9005", "Task Not Found/Updated");
                                }
                        }
                    }

                    wp.Save();
                    wp.Close();
                    wp.Dispose();
                    return true;
                }
                catch (System.Exception ex)
                {
                    AddError("1002", ex.Message);
                    return false;
                }

            }
        }

    public void Dispose()
            {
                if (app != null)
                {
                    app.Quit(PjSaveType.pjDoNotSave);
                    app = null;
                }
            }

            public bool Open(string projName)
            {
                try
                {
                    if (app != null)
                    {
                        string currentProject = "<>\\" + projName;
                        app.FileOpen(currentProject, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, PjPoolOpen.pjDoNotOpenPool, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                        return true;
                    }
                    return false;
                }
                catch (System.Exception ex)
                {
                    throw (new System.Exception("WinProj.Open " + ex.Message));
                }
            }

            public void Close()
            {
                try
                {
                    if (app != null)
                    {
                        app.FileCloseEx(PjSaveType.pjDoNotSave, false, !app.ActiveProject.ReadOnly);
                    }
                }
                catch (System.Exception ex)
                {
                    throw (new System.Exception("WinProj.Close.  " + ex.Message));
                }
            }


    public Project(string serverURL)
            {
                if (serverURL.EndsWith("/"))
                {
                    serverURL = serverURL.Substring(0, serverURL.Length - 1);
                }
                string paramList = " /s " + serverURL;
                var startInfo = new ProcessStartInfo(GetWinProjectLocation(), paramList);
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;

                Process.Start(startInfo);
               
                // Wait for Project Server connection to startup
                if (IsProjectRunning(10000.0))
                {
                    app = new ApplicationClass();
                    app.Visible = false;
                    app.Alerts((object)false);
                    app.DisplayAlerts = false;

                    bool connectedToProjectServer = (app.ActiveProject.ServerURL == serverURL);
                    app.FileCloseAll(PjSaveType.pjDoNotSave);
                    if (!connectedToProjectServer)
                    {
                        throw (new System.Exception("Could not connect to Project Server"));
                    }
                }
                else
                {
                    throw (new System.Exception("Project failed to startup."));
                }
            }

    • Marked as answer by heavywoody Thursday, January 19, 2012 7:08 PM
    Thursday, January 19, 2012 7:08 PM

All replies

  • Easiest way is always to get the code working in VBA then translate to C#.Most of this code can be discovered by recording a macro.

    With all office automation, C# 2010 is strongly recommended as it allows named and optional parameters which mae for much easier top read and support code.


    Rod Gill

    The one and only Project VBA Book Rod Gill Project Management
    Thursday, January 5, 2012 2:09 AM
    Moderator
  • Rod, thanks, I had actually tried that since I take a lot of the code ideas from VBA and translate.  The problem I run into is that the login to an Account happens before I can record a macro, so  I don't know how to get that process recorded as well.  Is there a way, after you log into a local account, to start recording a macro, login to an account on the server, and then open a project?
    Thursday, January 5, 2012 3:08 AM
  • I wanted to put this down in case someone else has run into this and needs help.  All the answsers were on this forum, but I just had to put them together so they would make sense to me.  So where is how I accomplished automated the entire checkout/update/checkin using C# and Office Interop

    I wanted to make sure credit goes to this fine article, which a lot of the code and ideas were borrowed from:

    http://epmdatapop.codeplex.com/SourceControl/list/changesets

     

    public Boolean SetActualCost(string serverurl, string projectname,int taskid, string month, string year, string newvalue, Boolean publish = false)
            {
                try
                {
                    dictErrors = new Dictionary<string, string>();
                    object readOnly = false;
                    Project wp = new Project(serverurl);
                    wp.Open(projectname);
                    Boolean bFound = false;
                   
     //Loop through all the tasks and find the taskID to match the one looking for
                    foreach (Task task in wp.app.ActiveProject.Tasks)
                    {
                        if (task.ID == taskid)
                        {
                                //Pass in the values to the assigment in order to set the approprimate month.  We always hit the middle of the month.
                                var tsv = task.TimeScaleData(month + "/15/" + year, month + " / 15 / " + year, PjTaskTimescaledData.pjTaskTimescaledActualCost, PjTimescaleUnit.pjTimescaleMonths, 1);
                                foreach (Microsoft.Office.Interop.MSProject.TimeScaleValue ts in tsv)
                                {
                                    try
                                    {
      //Update the field.  This is actual cost.
                                        ts.Value = newvalue;
                                        bFound = true;
                                    }
                                    catch (System.Exception ex)
                                    {
                                        AddError("1001", ex.Message);
                                    }
                                }
                                if (bFound == false)
                                {
                                    AddError("9005", "Task Not Found/Updated");
                                }
                        }
                    }

                    wp.Save();
                    wp.Close();
                    wp.Dispose();
                    return true;
                }
                catch (System.Exception ex)
                {
                    AddError("1002", ex.Message);
                    return false;
                }

            }
        }

    public void Dispose()
            {
                if (app != null)
                {
                    app.Quit(PjSaveType.pjDoNotSave);
                    app = null;
                }
            }

            public bool Open(string projName)
            {
                try
                {
                    if (app != null)
                    {
                        string currentProject = "<>\\" + projName;
                        app.FileOpen(currentProject, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, PjPoolOpen.pjDoNotOpenPool, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                        return true;
                    }
                    return false;
                }
                catch (System.Exception ex)
                {
                    throw (new System.Exception("WinProj.Open " + ex.Message));
                }
            }

            public void Close()
            {
                try
                {
                    if (app != null)
                    {
                        app.FileCloseEx(PjSaveType.pjDoNotSave, false, !app.ActiveProject.ReadOnly);
                    }
                }
                catch (System.Exception ex)
                {
                    throw (new System.Exception("WinProj.Close.  " + ex.Message));
                }
            }


    public Project(string serverURL)
            {
                if (serverURL.EndsWith("/"))
                {
                    serverURL = serverURL.Substring(0, serverURL.Length - 1);
                }
                string paramList = " /s " + serverURL;
                var startInfo = new ProcessStartInfo(GetWinProjectLocation(), paramList);
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;

                Process.Start(startInfo);
               
                // Wait for Project Server connection to startup
                if (IsProjectRunning(10000.0))
                {
                    app = new ApplicationClass();
                    app.Visible = false;
                    app.Alerts((object)false);
                    app.DisplayAlerts = false;

                    bool connectedToProjectServer = (app.ActiveProject.ServerURL == serverURL);
                    app.FileCloseAll(PjSaveType.pjDoNotSave);
                    if (!connectedToProjectServer)
                    {
                        throw (new System.Exception("Could not connect to Project Server"));
                    }
                }
                else
                {
                    throw (new System.Exception("Project failed to startup."));
                }
            }

    • Marked as answer by heavywoody Thursday, January 19, 2012 7:08 PM
    Thursday, January 19, 2012 7:08 PM