Create Add lookup type custom field using CSOM in Project Server 2013

Create Add lookup type custom field using CSOM:-

Create Add lookup type custom field using CSOM in Project Server 2013. In this article, I will explain how to Create add the lookup type custom field using CSOM Project Server Project Online. The lookup type custom field is different from a text type custom field, In text type custom field user enter the value for a custom field. And the lookup type custom field is bind through the lookup table. The user can select multiple values for a custom field and can select the single value. In lookup type custom field user cannot enter the value manually.

Below is code for Create Add lookup type custom field using CSOM:-

[AdSense-A]

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

namespace Add_Lookup_Type_Custom_Field_Using_CSOM
{
    class Program
    {
       static string UserName = "Project online username";
        static string Passwords = "project online password";
        public static string OnlinePWA = "project online pwapath";
        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;
                PRCLNT.LookupTableCollection LookuptablColl = ProjectCont.LookupTables;
                ProjectCont.Load(CustomField);
                ProjectCont.Load(Entitytype);
                ProjectCont.Load(LookuptablColl);

                var customfieldcoll = ProjectCont.LoadQuery(ProjectCont.CustomFields
                   .Where(pr => pr.Name == "LooktypeCustomField")); //Enter Your Custom Field Name
                ProjectCont.ExecuteQuery();
                if (customfieldcoll.ToList().Count == 0)
                {
                    var projLutCollection = ProjectCont.LoadQuery(ProjectCont.LookupTables
                         .Where(lut => lut.Name == "Add LookupTable"));

                    ProjectCont.ExecuteQuery();
                    if (projLutCollection.ToList().Count > 0)
                    {
                        PRCLNT.LookupTable lookupktbl = projLutCollection.Last();
                        PRCLNT.CustomFieldCreationInformation NewfieldInfo = new PRCLNT.CustomFieldCreationInformation();

                        NewfieldInfo.Id = new Guid();
                        NewfieldInfo.Name = "LooktypeCustomField";
                        NewfieldInfo.LookupTable = lookupktbl;  //Enter Existing Lookup Table Name through wich you want to bind
                        NewfieldInfo.Description = "Custom Field Description";
                        NewfieldInfo.IsWorkflowControlled = false;
                        NewfieldInfo.IsRequired = false;
                        NewfieldInfo.IsEditableInVisibility = false;
                        NewfieldInfo.IsMultilineText = false;
                        NewfieldInfo.FieldType = PRCLNT.CustomFieldType.TEXT;
                        NewfieldInfo.EntityType = Entitytype.ProjectEntity;
                        NewfieldInfo.EntityType = Entitytype.ProjectEntity;
                        ProjectCont.CustomFields.Add(NewfieldInfo);
                        ProjectCont.CustomFields.Update();
                        ProjectCont.ExecuteQuery();
                    }
                }
            }
        }

    }
}

Add lookup type custom field using CSOM


Comments

  1. HI ,
    Thanks for the great help.

    I wanted to Update project level LookupType custom field values using csom. Im able to update all types(Text,Duration,Date…etc) using setCustomFeield() method but im unable to update lookup type fields.

    Can you please tell me how to that?

    Thanks in advance for the great Help….

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 !!