Work Management service in SharePoint 2013 (6): Creating a task

To create a task, we will use the session object created above. When we create a task through Work Management Application, it is initially always a personal task, stored only in the task cache list in user’s My Site. Even if we are able to set a task location when creating a task, that task is still only a personal task.

There are two overloads of the CreateTask() method, one with, and one without location. Setting the location to null, we can create the task without location even with the location-aware overload:

TaskWriteResult taskWriteResult = uos.CreateTask("My task name", "My task description", "03/24/2014", "03/27/2014", isCompleted, isPinned, "", locationId);
Task myNewTask = taskWriteResult.Result;

With those two lines, we create a task with a name, description, start date (localized string), due date (localized string), booleans that define if the task is completed and pinned, task edit link (this would be interesting if we would have our own task providers), and the location id (nullable integer).

As mentioned above, this task is still a personal task, even if there is a location info associated with it.

To “promote” this task to its location (basically, to push it to the associated SharePoint list, Project etc), we will use one of the two overloads of the Promote method. If location information has already been associated with the task, this will suffice:

taskWriteResult = uos.PromotePersonalTaskToProviderTask(myNewTask.Id);

If there was no location information, we can set it during the task promotion:

taskWriteResult = uos.PromotePersonalTaskToProviderTaskInLocation(myNewTask.Id, locationId);

Now, this task can be found in its new location as well, and the location information cannot be changed anymore.