How to create custom task field in project server 2019 using csom

Raj Shekar 21 Reputation points
2022-09-19T07:20:59.657+00:00

Hi team,

I created a task column in project sever, but through code I'm unable to get it. Please provide the code if some one already worked on it.

Microsoft 365 and Office SharePoint Server Development
{count} votes

3 answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-09-20T06:17:45.917+00:00

    Hi @Raj Shekar ,

    Do you want to create a new item in a task List using CSOM? If my understanding is incorrect, please contact me.

    If yes, according to my research and testing, you can refer to the following code to achieve it:

                    List targetList = context.Web.Lists.GetByTitle("task");  
      
                    ListItemCreationInformation oListItemCreationInformation = new ListItemCreationInformation();  
                    ListItem oItem = targetList.AddItem(oListItemCreationInformation);  
                    oItem["Title"] = "test2";  
                    oItem["DueDate"] = DateTime.ParseExact("2022-09-22 08:00 AM", "yyyy-MM-dd HH:mm tt", null);  
                    oItem["StartDate"] = DateTime.ParseExact("2022-09-21 08:00 AM", "yyyy-MM-dd HH:mm tt", null);  
                    oItem.Update();  
                    context.ExecuteQuery();  
    

    My test result:
    242821-01.png

    More information for reference: Create List Item in SharePoint using CSOM

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

  2. Raj Shekar 21 Reputation points
    2022-09-20T06:39:50.263+00:00

    Hi TongZhangMSFT-7548,

    Sorry , I was not explained properly. Sorry for that. Actually i'm working on project server 2019 using csom, I want to update custom task field name "ProjectID" of the project need to be updated, sample code as below:

    string projname = "testproject";

                             var projcoll = projectContext.LoadQuery(projectContext.Projects.Where(p => p.Name == projname).Include(  
                     p => p.Name,  
                     p => p.Tasks,  
                     p => p.Tasks.Include(  
                         t => t.Name,  
                         t => t.PercentComplete,  
                         t => t.CustomFields  
                       )  
                     ));  
    
    
    
    
                
                projectContext.ExecuteQuery();  
                PublishedProject pubproj = projcoll.FirstOrDefault();  
                DraftProject draft = pubproj.Draft;  
    
                projectContext.Load(projectContext.EnterpriseResources);  
                projectContext.ExecuteQuery();  
    
                JobState job1 = projectContext.WaitForQueue(draft.CheckIn(true), 50);  
    
    
                DraftProject projCheckedOut = pubproj.CheckOut().IncludeCustomFields;  
    
                projectContext.Load(projCheckedOut.Tasks);  
                projectContext.ExecuteQuery();  
    
                foreach (var task in projCheckedOut.Tasks)  
                {  
    
                            projectContext.Load(task.CustomFields);  
                        projectContext.ExecuteQuery();  
    
    
    
                        foreach (CustomField cus in task.CustomFields)  
                        {  
                            Console.WriteLine(cus.Name);  
                            if (cus.Name == "ProjectID")  
                            {  
                                Console.WriteLine("Found ProjectID Field at Task Level" + " TaskNAme as  " + task.Name);  
                                  
                                string intname = cus.InternalName.ToString();  
                                string cusvalue = task[intname].ToString();  
                                  
                                    task[intname] = projectNum;  
                                  
                            }  
    
    
                        }  
    
    
                    }  
    
    
                    projCheckedOut.Update();  
    
                      
    
                             JobState jobState = projectContext.WaitForQueue(projCheckedOut.Publish(true), 20);  
    

    But im not able to fetch the customfield

    0 comments No comments

  3. Tong Zhang_MSFT 9,251 Reputation points
    2022-09-20T07:28:13.2+00:00

    Hi @Raj Shekar ,

    Our forum mainly discusses about SharePoint, since your question is about project server 2019, we suggest you create a new thread on techcommunity for help. Thanks for your understanding.

    Techcommunity:
    https://techcommunity.microsoft.com/t5/project/bd-p/Project


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.