Blog

Programmatically create a task item and assign to a user in SharePoint 2010

Posted by Vijai Anand Blogs | SharePoint 2010 Nov 17, 2012
In this blog you will see how to create a task item and assign to a user in SharePoint 2010
In this blog you will see how to create a task item and assign to a user in SharePoint 2010 using SharePoint object model.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

 

namespace Console

{

    class Program

    {

        static void Main(string[] args)

        {

            using (SPSite site = new SPSite("https://serverName/sites/TEST"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists.TryGetList("Tasks");

                    if (list != null)

                    {

                        // get the SharePoint group

                        SPUser user = web.EnsureUser(@"domainName\userName");

 

                        // Create a new task item

                        SPListItem task = list.Items.Add();

                        SPFieldUserValue assignToGroup = new SPFieldUserValue(web, user.ID, user.Name);

                        task["AssignedTo"] = assignToGroup;

                        task["Title"] = "Testing";

                        task["Description"] = "Testing";                      

                        task.Update();

                    }

                }

            }

        }

    }

}

 

 

post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter