Freigeben über


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;

    }

Comments

  • Anonymous
    July 26, 2007
    PingBack from http://mhinze.com/8-links-today-2007-07-26/

  • Anonymous
    October 09, 2007
    Good post...exactly what I needed. Thanks

  • Anonymous
    February 08, 2008
    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.

  • Anonymous
    April 18, 2008
    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

  • Anonymous
    May 20, 2008
    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

  • Anonymous
    October 20, 2008
    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.

  • Anonymous
    July 13, 2009
    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

  • Anonymous
    June 09, 2010
    The comment has been removed

  • Anonymous
    January 10, 2011
    You don't need TaskList.Lists["Tasks"] when TaskList is already the Tasks list.

  • Anonymous
    September 20, 2011
    Thanks alot its working .......