我有一个工具可以从 Project Web App(PWA/Project Server)打开 MS Project 文件。并做出一些改变。然后,我需要保存文件并发布更改,然后再关闭它并检查它。在发布过程完成之前签入文件时,Project Server 会出现问题。我试图在 API 中找到一些指示发布何时完成的内容,但我找不到任何内容。
有什么方法可以以编程方式告知 Project Server 发布何时完成?
我尝试在
Application.Publish
方法之前调用 Application.FileCloseEx(PjSaveType.pjDoNotSave, CheckIn: true)
方法,但 FileCloseEx
方法将在我的测试中发布完成之前执行
也许 CSOM 库中的某些内容可以访问 Project Server 的排队作业?
找到了一种使用 CSOM NuGet 包来做到这一点的方法:
// add this using statement at the top of the file.
using PWA = Microsoft.ProjectServer.Client;
public static PWA.JobState WaitForProjectToFinishPublishing(string projectName, string serverUrl)
{
PWA.JobState jState = PWA.JobState.Failed;
PWA.ProjectContext serverContext = null;
using (serverContext = new PWA.ProjectContext(serverUrl))
{
try
{
serverContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
// Query PWA for the Projects
serverContext.Load(serverContext.Projects);
serverContext.ExecuteQuery();
// get the project we are wanting to wait to finish publishing
var pubPrj = serverContext.Projects.Where(p => p.Name == projectName).FirstOrDefault();
if (pubPrj != null)
{
// Query PWA for the Queued jobs of the Project. (Publish will be one of the queued jobs if it is still publishing)
serverContext.Load(pubPrj.QueueJobs);
serverContext.ExecuteQuery();
// get the publish job
var publishJob = pubPrj.QueueJobs.Where(qJ => qJ.MessageType == PWA.QueueMsgType.ProjectPublish).FirstOrDefault();
if (publishJob != null)
{
// use the WaitForQueue method to wait for the publish job to finish. Timeout after 5 minutes (large projects can take some time to publish)
jState = serverContext.WaitForQueue(publishJob, 300);
}
}
}
catch (Exception ex)
{
// log something if you want
}
}
return jState;
}