views:

2422

answers:

2

I need to create tasks in a SharePoint Task List with c#.

Can I use SharePoint API like SPList.Items.Add() ? I will not be monitoring the task changes after creation. Or Should I consider using Outlook API?

+1  A: 

You got it right the first time.

Use the SharePoint object model. It's pretty easy to work with and reasonably intuitive. The only things you need to watch our for are populating the fields (check they exist first as SharePoint is customisable and someone could delete or rename a field), and when you've created the SPWeb and SPSite objects, be sure to dispose() of them afterwards.

OK. But I cannot figure out how to populate fields. Because some task spcific fields such as "Assigned To" is read-only. Can you post a very simple code snippet to create task
Onur Bıyık
Finally I got it. The not-so-informative sharepoint error messages caused me for another 4 hours :). Thank you!
Onur Bıyık
A: 

private void CreateTaskSOW_MethodInvoking_1(object sender, EventArgs e) { using (SPSite site = new SPSite(workflowProperties.Web.Site.ID)) { using (SPWeb web = site.OpenWeb()) { try { CreateTaskSOW.TaskId = Guid.NewGuid(); CreateTaskSOW.TaskProperties.AssignedTo = workflowProperties.Originator.ToString(); CreateTaskSOW.TaskProperties.Title = workflowProperties.Item["ID"].ToString() + " SOW "+ workflowProperties.Item["Proj Name"].ToString(); CreateTaskSOW.TaskProperties.Description = _ApproverNotes; //CreateTaskSOW.TaskProperties.DueDate = DateTime.Now.AddDays(5.0); CreateTaskSOW.TaskProperties.DueDate = DateTime.Parse(workflowProperties.Item["Estim Value SOW"].ToString()); CreateTaskSOW.TaskProperties.PercentComplete = (float)0.0; //CreateTaskSOW.TaskProperties.StartDate = DateTime.Now; CreateTaskSOW.TaskProperties.StartDate = DateTime.Parse(workflowProperties.Item["Act Work Begin"].ToString()); CreateTaskSOW.TaskProperties.EmailBody = _ApproverNotes; CreateTaskSOW.TaskProperties.SendEmailNotification = true;

                }
                catch (Exception ex)
                {
                    logger.Error("Could not Create Project Task SOW", ex);
                }
            }
        }
    }

:D