How to Add a Task in a SharePoint 2007 (MOSS/WSS) Site Programmatically

How to Add a Task in a SharePoint 2007 (MOSS/WSS) Site Programmatically

  • Comments 10

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;

    }

Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post
  • PingBack from http://mhinze.com/8-links-today-2007-07-26/

  • Good post...exactly what I needed.

    Thanks

  • 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.

  • 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

  • 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

  • Good work! Thank a lot.

    Take in account that you have to reference the Microsoft.Sharepoint.dll directly from C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI, otherwise you can get security errors.

  • 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:  http://www.signaturesterling.com/blog/post/MOSS-2007---User-cannot-be-found!.aspx

  • 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?

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

  • Thanks alot its working .......

Page 1 of 1 (10 items)