The following code snippet can be used to programmatically create a new task.
using (SPSite site = new SPSite("SiteUrl"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList("Tasks");
if (list != null)
{
// get the SharePoint group
SPUser user = web.EnsureUser(@"domain\administrator");
// Create a new task item
SPListItem task = list.Items.Add();
SPFieldUserValue assignToGroup = new SPFieldUserValue(web, user.ID, user.Name);
task["AssignedTo"] = assignToGroup;
task["Title"] = "Testing Tasks";
task["Description"] = "Testing Tasks";
task.ModerationInformation.Status = SPModerationStatusType.Approved;
task.Update();
}
}
}
using (SPSite site = new SPSite("SiteUrl"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList("Tasks");
if (list != null)
{
// get the SharePoint group
SPUser user = web.EnsureUser(@"domain\administrator");
// Create a new task item
SPListItem task = list.Items.Add();
SPFieldUserValue assignToGroup = new SPFieldUserValue(web, user.ID, user.Name);
task["AssignedTo"] = assignToGroup;
task["Title"] = "Testing Tasks";
task["Description"] = "Testing Tasks";
task.ModerationInformation.Status = SPModerationStatusType.Approved;
task.Update();
}
}
}