DraftProject.Update 方法
在新的项目或已检出草稿项目返回到项目服务器中保存的更改。
命名空间: Microsoft.ProjectServer.Client
程序集: Microsoft.ProjectServer.Client(位于 Microsoft.ProjectServer.Client.dll 中)
语法
声明
<RemoteAttribute> _
Public Function Update As QueueJob
用法
Dim instance As DraftProject
Dim returnValue As QueueJob
returnValue = instance.Update()
[RemoteAttribute]
public QueueJob Update()
返回值
类型:Microsoft.ProjectServer.Client.QueueJob
QueueJob对象,该对象包含有关排队的作业的信息。如果队列作业成功,项目服务器队列服务将保存该项目的新的或草稿版本。
备注
Update方法无法处理的 CSOM 请求大于 2 MB 的大小。有关 CSOM 限制的详细信息,请参阅CSOM 执行的操作和不执行的操作。
示例
下面的示例演示创建大于 2 MB 的 CSOM 请求的问题。该示例在第一次调用TestCreateTasks方法两次,将 252 任务添加到项目中,然后再尝试 253 任务。因为在第二个请求的数据量超过 2 MB,则请求就会失败。
TestCreateTasksInGroups方法实现分成较小的组大型请求的一种方法。
using System;
using System.Net;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
namespace LimitTest
{
internal static class Program
{
const int SUCCESS = 0;
const int ERROR = 1;
private static void Main()
{
using (var context = CreateContext())
{
// Create a request of less than 2 MB.
int numTasks = 252;
TestCreateTasks(context, numTasks);
// Create a request that is greater than 2 MB.
numTasks = 253;
TestCreateTasks(context, numTasks);
// Break a large request into groups of less than 2 MB.
numTasks = 301;
int groupCount = 200;
TestCreateTasksInGroups(context, numTasks, groupCount);
}
exitApp(SUCCESS);
}
private static ProjectContext CreateContext()
{
const string url = "https://ServerName/pwa"; // Change the PWA URL.
var context = new ProjectContext(url);
return context;
}
private static void TestCreateTasks(ProjectContext context, int taskCount)
{
PublishedProject project;
DraftProject projectDraft = null;
Console.WriteLine("\n*** Creating project for {0} tasks", taskCount);
var projectInfo = new ProjectCreationInformation();
projectInfo.Id = Guid.NewGuid();
projectInfo.Name = "LimitTest_" + taskCount + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
projectInfo.Start = DateTime.Today;
project = context.Projects.Add(projectInfo);
var job = context.Projects.Update();
if (WaitForJob(context, job))
{
projectDraft = project.CheckOut();
Console.WriteLine("Creating the {0} tasks", taskCount);
for (var i = 0; i < taskCount; i++)
{
CreateTask(projectDraft.Tasks, i);
}
Console.WriteLine("Calling DraftProject.Update for project: {0}", projectInfo.Name);
job = projectDraft.Update();
}
else
{
Console.WriteLine("\nThe Update job failed for Projects.Add.");
exitApp(ERROR);
}
Console.WriteLine("\tWaitForJob: after DraftProject:Update");
if (WaitForJob(context, job))
{
Console.WriteLine("\nChecking in and publishing project");
job = projectDraft.Publish(true);
Console.WriteLine("\tWaitForJob: after DraftProject.Publish");
if (!WaitForJob(context, job))
{
Console.WriteLine("\nThe DraftProject.Publish job failed for the project.");
exitApp(ERROR);
}
}
else
{
Console.WriteLine("\nThe DraftProject.Update job failed for adding {0} tasks.", taskCount);
}
Console.WriteLine();
}
private static void TestCreateTasksInGroups(ProjectContext context, int taskCount, int groupCount)
{
PublishedProject project;
DraftProject projectDraft = null;
int numGroups = taskCount / groupCount;
int remainingTasks = taskCount % groupCount;
if (remainingTasks > 0) numGroups++;
var projectInfo = new ProjectCreationInformation();
projectInfo.Id = Guid.NewGuid();
projectInfo.Name = "LimitTest_" + taskCount + "_groupCount_" + groupCount
+ "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss");
projectInfo.Start = DateTime.Today;
Console.WriteLine("\n*** Creating project for {0} tasks in groups of {1}:\n\t{2}",
taskCount, groupCount, projectInfo.Name);
project = context.Projects.Add(projectInfo);
var job = context.Projects.Update();
if (WaitForJob(context, job))
{
int tasks2Create = groupCount;
projectDraft = project.CheckOut();
for (int i = 0; i < numGroups; i++)
{
if (i == numGroups - 1) tasks2Create = remainingTasks;
Console.WriteLine("\nCreating group {0}: {1} tasks", i, tasks2Create);
for (int t = 0; t < tasks2Create; t++)
{
CreateTask(projectDraft.Tasks, i);
}
Console.WriteLine("Calling DraftProject.Update for task group: {0}", i);
job = projectDraft.Update();
if (WaitForJob(context, job))
{
Console.WriteLine("\n\tSuccess: DraftProject.Update job complete");
}
else
{
Console.WriteLine("\nThe DraftProject.Update job failed.");
exitApp(ERROR);
}
}
Console.WriteLine("\nPublishing the project");
job = projectDraft.Publish(true);
if (WaitForJob(context, job))
{
Console.WriteLine("\nThe project '{0}'\n\tis published, with {1} tasks.",
projectInfo.Name, taskCount);
}
else
{
Console.WriteLine("\nThe DraftProject.Publish job failed.");
exitApp(ERROR);
}
}
}
private static void exitApp(int exitCode)
{
Console.Write("Press any key to exit: ");
Console.ReadKey(true);
Environment.Exit(exitCode);
}
private static void CreateTask(DraftTaskCollection draftTasks, int i)
{
var taskInfo = new TaskCreationInformation();
taskInfo.Id = Guid.NewGuid();
taskInfo.Name = i.ToString();
taskInfo.IsManual = false;
taskInfo.Duration = "1d";
draftTasks.Add(taskInfo);
}
private static bool WaitForJob(ProjectContext context, QueueJob job)
{
bool result = true;
try
{
context.WaitForQueue(job, int.MaxValue);
}
catch (ServerException ex)
{
Console.WriteLine("\n" + ex);
result = false;
}
return result;
}
}
}
下面是运行LimitTest示例的输出:
*** Creating project for 252 tasks
Creating the 252 tasks
Calling DraftProject.Update for project: LimitTest_252_20120530_084023
WaitForJob: after DraftProject:Update
Checking in and publishing project
WaitForJob: after DraftProject.Publish
*** Creating project for 253 tasks
Creating the 253 tasks
Calling DraftProject.Update for project: LimitTest_253_20120530_084224
WaitForJob: after DraftProject:Update
Microsoft.SharePoint.Client.ServerException: The request uses too many resources.
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at Microsoft.ProjectServer.Client.ProjectContext.WaitForQueue(QueueJob job, Int32 timeoutSeconds)
at LimitTest.Program.WaitForJob(ProjectContext context, QueueJob job)
in d:\Project\P15\CSOM\CSOMLimitTest\Program.cs:line 193
The DraftProject.Update job failed for adding 253 tasks.
*** Creating project for 301 tasks in groups of 200:
LimitTest_301_groupCount_200_20120530_085506
Creating group 0: 200 tasks
Calling DraftProject.Update for task group: 0
Success: DraftProject.Update job complete
Creating group 1: 101 tasks
Calling DraftProject.Update for task group: 1
Success: DraftProject.Update job complete
Publishing the project
The project 'LimitTest_301_groupCount_200_20120530_085506'
is published, with 301 tasks.
Press any key to exit: