You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否通过Project Server CSOM从已发布项目创建项目模板?

如何用Project Server CSOM从已发布项目创建项目模板(MS Project 2016)

在MS Project Server 2016中,没有直接的CSOM API方法可以一键从已发布项目生成模板,但可以通过导出项目文件→转换为模板→上传到企业模板库的间接流程实现需求。

实现步骤

  • 导出已发布项目的.mpp文件:通过CSOM获取已发布项目的二进制内容,保存为本地.mpp文件
  • 将.mpp转换为.mpt模板:利用MS Project的COM对象或第三方库将.mpp另存为.mpt格式
  • 上传.mpt到Project Server的企业模板库:通过CSOM将模板文件上传到指定的企业模板文档库

代码示例(C#)

1. 导出已发布项目为.mpp

using Microsoft.ProjectServer.Client;
using System.IO;

public void ExportPublishedProjectToMpp(string pwaUrl, string projectId, string outputPath)
{
    using (var context = new ProjectContext(pwaUrl))
    {
        // 根据环境选择凭据类型,此处用默认凭据
        context.Credentials = System.Net.CredentialCache.DefaultCredentials;
        
        var project = context.Projects.GetById(new Guid(projectId));
        context.Load(project);
        context.ExecuteQuery();
        
        // 获取项目文件二进制流
        var fileContent = context.Web.GetFileByServerRelativeUrl(project.ServerRelativeUrl).OpenBinaryStream();
        context.ExecuteQuery();
        
        using (var fileStream = new FileStream(outputPath, FileMode.Create))
        {
            fileContent.Value.CopyTo(fileStream);
        }
    }
}

2. 将.mpp转换为.mpt(依赖MS Project COM组件)

using Microsoft.Office.Interop.MSProject;

public void ConvertMppToMpt(string mppPath, string mptPath)
{
    var app = new Application();
    try
    {
        var project = app.FileOpen(mppPath, ReadOnly: true);
        app.FileSaveAs(mptPath, PjFileFormat.pjMPT);
        app.FileClose(PjSaveType.pjDoNotSave);
    }
    finally
    {
        app.Quit(PjSaveType.pjDoNotSave);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    }
}

3. 上传.mpt到企业模板库

public void UploadTemplateToEnterpriseLibrary(string pwaUrl, string mptPath, string libraryServerRelativeUrl)
{
    using (var context = new ProjectContext(pwaUrl))
    {
        context.Credentials = System.Net.CredentialCache.DefaultCredentials;
        
        var library = context.Web.GetListByServerRelativeUrl(libraryServerRelativeUrl);
        context.Load(library.RootFolder);
        context.ExecuteQuery();
        
        using (var fileStream = new FileStream(mptPath, FileMode.Open))
        {
            var fileCreationInfo = new FileCreationInformation
            {
                ContentStream = fileStream,
                Url = Path.GetFileName(mptPath),
                Overwrite = true
            };
            
            var uploadedFile = library.RootFolder.Files.Add(fileCreationInfo);
            context.ExecuteQuery();
        }
    }
}

关键注意事项

  • 权限要求:执行代码的账号需要具备Project Server的项目读取权限和企业模板库的编辑权限
  • COM组件依赖:转换.mpp到.mpt的代码需要安装MS Project客户端,且运行环境支持COM调用;若服务器环境不允许安装MS Project,可考虑使用开源库(如MPXJ)处理文件格式转换
  • 项目状态:确保目标项目是已发布状态,未发布项目无法通过CSOM导出完整内容
  • 模板库路径:Project Server的企业模板库默认路径通常为/PWA/Enterprise Project Templates,可根据实际环境调整

内容的提问来源于stack exchange,提问作者bigal440

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 08:52:16