Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add Task to Projects in SharePoint 2013 using CSOM?

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.32k
    Comment on it

    In SharePoint, each project has its own tasks, assignments, team members etc.While creating projects in SharePoint we have to also create its tasks, assignments, team members and custom fields.

    Here below is the sample code for creating tasks in projects. 

    PublishedProject publishedProject = null;
    DraftProject draftProject = null;
    TaskCreationInformation taskInfo = null;
    DraftTask draftTask;
    QueueJob queueJobTask = null;
    
    try
    {	
        publishedProject = availableProjects[new Guid(projectId)]; //find publishedProject from all available Projects
    	draftProject = publishedProject.CheckOut();
    
    	// Load/read all draft tasks
    	projectContext.Load(draftProject.Tasks);
    	projectContext.ExecuteQuery();	
    	
    		try
    		{	
    			taskInfo = new TaskCreationInformation();
    			taskInfo.Id = new Guid();
    			taskInfo.Name = "Scope";
    			taskInfo.Duration = "200.000000";
    			taskInfo.ParentId = "Task Parent Id";
    			taskInfo.IsManual = true;
    			taskInfo.Start = DateTime.ParseExact("2015-01-01 08:00 AM", "yyyy-MM-dd HH:mm tt", null);
    			taskInfo.Finish = DateTime.ParseExact("2016-01-01 08:00 AM", "yyyy-MM-dd HH:mm tt", null);
    
    			// Add the task to the checked out project.
    			draftTask = draftProject.Tasks.Add(taskInfo);
    
    			//Add the rest of the fields
    			draftTask.IsMarked = false;
    			draftTask.ActualCost = Convert.ToDouble("0");
    
    			//Now update the task with the rest of the custom fields
    			draftTask[internalname] = "Custom field Internal Name";		
    			
    		}
    		catch (Exception ex)
    		{
    			// log exception task not added
    		}
    	
    	// Update the draft project collection.
    	queueJobTask = draftProject.Update();
    	queueJobTask = draftProject.Publish(true);
    }
    catch (Exception ex)
    {
    	// log exception task not added
    }

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: