Welcome to MSDN Blogs Sign in | Join | Help

Pranab Paul's Blog - SharePoint 2007 (MOSS/WSS 3.0) Development Tips

------------------------------------------Web Parts, Workflow, InfoPath Form Services, Features, Site Definition, Event Receivers, Excel Services, Business Data Catalog (BDC), Search
How to Add a Task in a SharePoint 2007 (MOSS/WSS) Site Programmatically

Here is a piece of code (a function) to add a task. You can use it as a Web Method in a custom Web Service. This method can be used from Applications outside of SharePoint, provided the user using this application has sufficient privilege to update Tasks Lists.

public string CreateTask(string SitePath, string TaskName, string AssgnTo, DateTime DueDate)

    {

        string ReturnVal = "";

        try

        {

            SPSite WebApp = new SPSite(SitePath);

            SPWeb Site = WebApp.OpenWeb();

            SPList TaskList = Site.Lists["Tasks"];

            // add a new item…

            SPListItem NewTask = TaskList.Lists["Tasks"].Items.Add();

            NewTask["Priority"] = "(2) Normal";

            NewTask["Title"] = TaskName;

            NewTask["DueDate"] = DueDate;

            NewTask["Assigned To"] = Site.Users.GetByEmail(AssgnTo);

            NewTask.Update();

        }

        catch (Exception ex)

        {

            ReturnVal += "Task not added, reason: " + ex.Message;

        }

        return ReturnVal;

    }

Posted: Wednesday, July 25, 2007 7:40 PM by pranab

Comments

kemaltolga said:

Good post...exactly what I needed.

Thanks

# October 10, 2007 12:50 AM

Murat said:

Firstly Thanx for the code. It work very well. But when I write a name from the AD, to 'AssignTo' parameter, program throws an exception which is 'user not found'.

What will be the syntax of the AssagnTo field? I have tried some values ("domainame\\username", or only "username" or "email") but it didnt work.

# February 8, 2008 10:28 AM

Pradipta Nayak said:

sir,

Thanks for a such a butful prog. it nice and helpful to me but can we update a task list on particular codition as where file name is this or any other things ...if possible tell me plz .....

                   Pradipta Nayak

                    convergent technologies,GGN,Haryana

                    pradiptanayak2007@gmail.com

# April 19, 2008 2:22 AM

Joseph said:

Thanks for this blog.

Do I need any Namespace/librarie? If I do, please can u tell me how can I set it up? I will apreciate any help.

e-mail: josephcotto@gmail.com

# May 20, 2008 10:36 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker