Update Custom Field Value Using CSOM in Project Online

Update Custom Field Value Using CSOM:

In this post, I will explain to you how to update custom field value using CSOM. We can update a custom field value in respective projects. All the custom fields on PWA will show on Project Details Page with an empty value. If a custom field has a Default value then will show on every project. And you can update it on every project.

You need to set custom field value as

Custom_b50f29a9881de61180d700155d1c581a=Custom_(Your Custom Field GUID without hyphen)

projCheckedOut.SetCustomFieldValue("Custom_7ad42d64-8949-e611-80df-00155d1c6619", "CustoFieldValue");

Get custom field value using CSOM

foreach (var cust in item.IncludeCustomFields.FieldValues)
                        {
                            string CustomFieldName = item.IncludeCustomFields.CustomFields.ToList().Where(pr => pr.InternalName == cust.Key).ToList().Count == 0 ? string.Empty : item.IncludeCustomFields.CustomFields.ToList().Where(pr => pr.InternalName == cust.Key).ToList()[0].Name;
 
                            string CustomFieldID = cust.Key.ToString();
                            string CustomFieldValue = cust.Value.ToString();
                        }

Read Custom Field Value Using CSOM in Project Online

Get Custom Field Value using JavaScript Object Model(JSOM)

[AdSense-A]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.ServiceModel;
using System.Data;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
using System.Security;

namespace Update_Custom_Field_Using_CSOM
{
    class Program
    {
        static string UserName = "UserName";
        static string Passwords = "Password";
        public static string OnlinePWA = "PWAPath";
        static void Main(string[] args)
        {
            UpdateCustomField();
        }
         private static void UpdateCustomField()
         {
            using (ProjectContext ProjectCont = new ProjectContext(OnlinePWA))//PWA Url
            {
                SecureString passWord2 = new SecureString();
                foreach (char c in Passwords.ToCharArray()) passWord2.AppendChar(c);
                ProjectCont.Credentials = new SharePointOnlineCredentials(UserName, passWord2);
                ProjectCont.Load(ProjectCont.Projects,
                c => c.IncludeWithDefaultProperties(pr => pr.StartDate, pr => pr.CustomFields, pr => pr.Assignments, pr => pr.FinishDate, pr => pr.Owner.LoginName, pr => pr.Tasks, pr => pr.ProjectSiteUrl, pr => pr.PercentComplete, pr => pr.ProjectResources, pr => pr.CustomFields, pr => pr.EnterpriseProjectType));
                ProjectCont.ExecuteQuery();
                var projectColl = ProjectCont.LoadQuery(ProjectCont.Projects.Where(p => p.Name == "Project Name"));
                ProjectCont.ExecuteQuery();
                PublishedProject pubProj = projectColl.First();
                
                DraftProject draft = pubProj.Draft;
                JobState job1 = ProjectCont.WaitForQueue(draft.CheckIn(true), 20);
                DraftProject projCheckedOut = pubProj.CheckOut();
                projCheckedOut.Name = "Project Name";
                projCheckedOut.Description = "This content updated by ProjectServerCode";
                projCheckedOut.SetCustomFieldValue("Custom_7ad42d64-8949-e611-80df-00155d1c6619", "CustoFieldValue");
                //Note:Custom_b50f29a9881de61180d700155d1c581a=Custom_(Your Custom Field GUID without hyphen)
                projCheckedOut.Update();
                ProjectCont.Load(projCheckedOut);
                projCheckedOut.Publish(true);
                ProjectCont.ExecuteQuery();
                ProjectCont.Projects.Update();
                ProjectCont.ExecuteQuery();
                projCheckedOut.Publish(true);
                JobState jobState = ProjectCont.WaitForQueue(ProjectCont.Projects.Update(), 10);
               
            }
        }

    }
}

 

 

Comments

  1. Hi,

    Thanks for this post but I am looking for a sample code for updating a task. For example, I want to update the field “Actual Cost” from $10 to $100, is there any way to achieve that?

    Thanks in advance!

    Regards,
    AML

  2. nice post helped me a lot. thanks
    Would you tell me please how to update lookup type custom field using csom

      1. Hi,

        Below code condition is checking whether that project is checked out or not.

        Doubut : if the Project is already checkedOut aslo i shout update the custom field.

        can you suggest me for the above.

        if (!pubProj.IsCheckedOut)
        {

        }
        else
        {
        //else condittion will work if the project is checkedout.
        // can we update the same custom field here ?

        }

        1. Hello Raghava,
          else condition will not work because the project is check out. so before to update the custom field value you have to checkIn and checkout the project and then update it. below is some code of line you have to add for checkIn the project
          DraftProject draft = pubProj.Draft;
          JobState job1 = ProjectCont.WaitForQueue(draft.CheckIn(true), 20);

          These lines i am adding in my code also.

  3. Hi admin,

    the code is working fine but in the Manage Queue jobs ( Job state ) is ” Failed But Not Blocking Correlation”

    On pwa site every project custom fields update we are getting one mail as like below message.

    Your ProjectCheckIn job failed. Its current state is FailedNotBlocking. It was 0 complete. It entered the queue at 11/16/2016 04:23:30.
    The errors returned from the queue are as follows:
    Error ID: 10102
    Error ID: 26000

    Detailed error below – send it to the administrator for more detailed troubleshooting.

    You can do the following:
    1. Try troubleshooting using the error IDs, error XML.
    2. Contact administrator with your jobID (b119c178-f7ab-e611-80ce-00155d50d12b) and error XML.

    kindly suggest for this.

    Thanks.

  4. projCheckedOut.Publish(false);

    Error is not coming in Manage Queue jobs but after 1 or 2 days ( job state in the manage queue jobs is cancelled ) and error details is below.

    General
    Microsoft.Office.Project.Server.BusinessLayer.Queue.Message:
    ProjectPublishFailure (23000). Details: id=’23000′
    name=’ProjectPublishFailure’
    uid=’ce663ad7-4eaa-e611-80d1-00155da0360e’
    projectuid=’96de4ae0-92db-e511-9424-00155d1edcb4′
    messagetype=’Microsoft.Office.Project.Server.BusinessLayer.QueueMsg.PublishProjectMessage’
    messageID=’12’
    stage=”
    blocking=’Block’
    .
    Queue:
    GeneralQueueJobFailed (26000) – ProjectPublish.PublishProjectMessage. Details: id=’26000′
    name=’GeneralQueueJobFailed’
    uid=’d2663ad7-4eaa-e611-80d1-00155da0360e’
    JobUID=’8e8047cb-4eaa-e611-80d1-00155da0360e’
    ComputerName=’a01fbc47-3956-4d93-beaf-71fcadbef092′
    GroupType=’ProjectPublish’
    MessageType=’PublishProjectMessage’
    MessageId=’12’
    Stage=”
    CorrelationUID=’5246b79d-c0e5-3000-a4b6-b1b80943d707′
    For more details, check the ULS logs on machine a01fbc47-3956-4d93-beaf-71fcadbef092 for entries with JobUID 8e8047cb-4eaa-e611-80d1-00155da0360e.

    please suggest me to fix this issue.

    Thanks.

  5. Hello,I read your blogs named “Update Custom Field Value Using CSOM – SharePoint & Project server tutorials 2013” like every week.Your story-telling style is witty, keep it up! And you can look our website about تحميل اغانى.

  6. Hi,

    Thanks for your post! it helped me a lot! Question, can project Level data be updated, so the PDP should the update, without having to Publish the schedule? Currently your code seems to force the schedule to be published, some PMs want to keep the control of when the schedule is published, but we still need to update some Project Level information for accurate reporting.

    Thanks!

  7. Hi,

    Can you help me to get list of checkout objects like (Lookup tables and custom fields) using CSOM ?

    and how can we force checkin Lookup table and custom fields using CSOM ?

    Please suggest me to get solution for above.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.