Welcome to MSDN Blogs Sign in | Join | Help

Pranab Paul's Blog - Development Tips on SharePoint, Office and Web

------------------------------------------Web Parts, Workflow, InfoPath Form Services, Features, Site Definition, Event Receivers, Excel Services, Business Data Catalog (BDC), Search

News

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

Ariel Ponce said:

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.

# October 20, 2008 12:59 PM

Tracy said:

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

# July 13, 2009 4:18 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: 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