Create Add Custom Field using CSOM Project Online/ Server

Create Add Custom Field using CSOM Project Online Project Server:-

Create Add Custom Field using CSOM Project Online/ Server. Create Add Custom Field using CSOM Project Online or Project Server. In my previous articles, I already told you about Custom Fields and programmatically update custom fields using PSI.  In this, I will let you know Create Add Custom Field using CSOM Project Online or Project Server. You can Choose whether the field has the single line of text, multiple lines of text, a calculated, or a lookup table formula. Fields with multiple lines of text will not be available in the Project client. In this article, will explain to you the single line of text. And wait for my next post I will explain to you how to add lookup table and lookup type custom field using CSOM

Subscribe my channel

Below is code to Create Add Custom Field using CSOM:-

using System;
using System.Linq;
using System.Security;
using Microsoft.SharePoint.Client;
using PRCLNT = Microsoft.ProjectServer.Client;

namespace Add_Custom_Field_Using_CSOM
{
    class Program
    {
        static string UserName = "Project online username";
        static string Passwords = "Project online password";
        public static string OnlinePWA = "Project online pwa path";
        static void Main(string[] args)
        {

            using (PRCLNT.ProjectContext ProjectCont = new PRCLNT.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();
                PRCLNT.CustomFieldCollection CustomField = ProjectCont.CustomFields;
                PRCLNT.EntityTypes Entitytype = ProjectCont.EntityTypes;
                ProjectCont.Load(CustomField);
                ProjectCont.Load(Entitytype);
                var customfieldcoll = ProjectCont.LoadQuery(ProjectCont.CustomFields
                    .Where(pr => pr.Name == "CustomFieldName"));// Enter Custom Field Name to check if already exist or not
                ProjectCont.ExecuteQuery();
                if (customfieldcoll.ToList().Count == 0)
                {
                    PRCLNT.CustomFieldCreationInformation NewfieldInfo = new PRCLNT.CustomFieldCreationInformation();

                    NewfieldInfo.Id = new Guid(); ;
                    NewfieldInfo.Name = "CustomFieldName";
                    NewfieldInfo.Description = "CustomFieldDesc";
                    NewfieldInfo.IsWorkflowControlled = false;
                    NewfieldInfo.IsRequired = false;
                    NewfieldInfo.IsEditableInVisibility = false;
                    NewfieldInfo.IsMultilineText = false;
                    NewfieldInfo.FieldType = PRCLNT.CustomFieldType.TEXT;//Field Tpye you can change like COST, Date, Number etc.
                    NewfieldInfo.EntityType = Entitytype.ProjectEntity; //You can change your entity type like ProjectEntity,TaskEntity,ResourceEntity etc.
                    ProjectCont.CustomFields.Add(NewfieldInfo);
                    ProjectCont.CustomFields.Update();
                    ProjectCont.ExecuteQuery();
                }
            }


        }

    }
}

 


Comments

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

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.

error: Content is protected !!