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 = "";



            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);



        catch (Exception ex)


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


        return ReturnVal;


Comments (10)

  1. Vijender M says:

    Thanks alot its working …….

  2. Andy Maggs says:

    You don't need TaskList.Lists["Tasks"] when TaskList is already the Tasks list.

  3. Ryan says:

    thanks, exactly what i needed.

    just a thought, but dont you ned to contain the SPsite and SPweb statements in either 'using' statements, or using a 'finally' block?

  4. Tracy says:

    Thanks for your helpful posting, a bit of insight regarding some problems I encountered.  You will only be able to access an SPUser object (Site.Users.GetByEmail(email)) for users that have browsed to the relevant SharePoint site and authenticated.  Otherwise, a ‘User Cannot Be Found’ exception will be thrown.  This behavior will occur even if the user possesses a UserProfile object within SharePoint for the same user (i.e. a successful AD import).  Check out my blog posting for a resolution to this problem:—User-cannot-be-found!.aspx

  5. Ariel Ponce says:

    Good work! Thank a lot.

    Take in account that you have to reference the Microsoft.Sharepoint.dll directly from C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12ISAPI, otherwise you can get security errors.

  6. Joseph says:

    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.


  7. Pradipta Nayak says:


    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


  8. Murat says:

    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.

  9. kemaltolga says:

    Good post…exactly what I needed.


Skip to main content