Read Custom Field and Lookup Custom field Value using PSI Project Server 2013

Read Custom Field and Lookup Custom field Value using PSI Project Server 2013. In this article, you learn to Read Custom Field and Lookup Custom field Value using PSI Project Server 2013. You can read custom field value as DATE_VALUE,TEXT_VALUE, NUM_VALUE, and DUR_VALUE using below the article.PSI references you can follow my post,  Create Project using PSI Programmatically Project Server 2013. And for required namespace and methods, follow my post, Update Lookup Type and non- lookup Custom Field using PSI.

If you don’t know how to add service reference then follow my post. Add PSI Web Reference in Solution file

Read Projects, Custom fields, and Lookup table dataset using PSI 

 SvcProject.Project ProjectWS = new SvcProject.Project
                   {
                       Credentials = System.Net.CredentialCache.DefaultNetworkCredentials,
                       Url = string.Format("{0}{1}", PWAPath, "_vti_bin/PSI/Project.asmx")
                   };
                QueueSystem queueWS = new QueueSystem
                {
                    Credentials = System.Net.CredentialCache.DefaultCredentials,
                    Url = string.Format("{0}{1}", PWAPath, "_vti_bin/PSI/queuesystem.asmx")
                };
                ProjectDataSet = ProjectWS.ReadProjectList();

                int count = ProjectDataSet.Tables[0].Rows.Count;
                customFieldsWS = new CustomFields { Credentials = System.Net.CredentialCache.DefaultCredentials, Url = string.Format("{0}{1}", PWAPath, "_vti_bin/PSI/CustomFields.asmx") };
                lookupTableWS = new LookupSvc.LookupTable { Credentials = System.Net.CredentialCache.DefaultCredentials, Url = string.Format("{0}{1}", PWAPath, "_vti_bin/PSI/LookupTable.asmx") };
                customFieldDataSet = customFieldsWS.ReadCustomFields("", false);
                lookUpDataSet = lookupTableWS.ReadLookupTables("", false, 1033);

Read Custom Field Value using PSI

public static string ReadCustomFieldValue(SvcProject.Project ProjectWS, ProjectDataSet projDataSet, string CustomeFieldName, string ProjectName)
        {
            ProjectDataSet ProjDS = new SvcProject.ProjectDataSet();
            string ProjectCustomFieldValue = string.Empty;
            try
            {
                Guid sessionGuid = Guid.NewGuid();
                Guid jobGuid = Guid.NewGuid();


                if (!string.IsNullOrEmpty(ProjectName))
                {

                    DataRow[] row = ProjectDataSet.Project.Select("PROJ_NAME='" + ProjectName.Trim() + "'");
                    ProjectGuid = new Guid(row[0]["PROJ_UID"].ToString());
                    SingleprojectDS = ProjectWS.ReadProject(ProjectGuid, DataStoreEnum.WorkingStore);

                }

                DataRow[] customrow = customFieldDataSet.CustomFields.Select("MD_PROP_NAME='" + CustomeFieldName + "'").Length > 0 ? customFieldDataSet.CustomFields.Select("MD_PROP_NAME='" + CustomeFieldName + "'") : null;
                Guid CFGuid = Guid.NewGuid();
                if (customrow != null)
                {

                    CFGuid = new Guid(customrow[0]["MD_PROP_UID"].ToString());
                    Guid LookupValueGuid = GetGuidForLTValue(customFieldDataSet, lookUpDataSet, CFGuid, CustomeFieldName);
                    if (SingleprojectDS != null)
                    {
                        DataRow[] ProjectDatacustomrow = SingleprojectDS.ProjectCustomFields.Select("MD_PROP_UID='" + CFGuid + "'").Length > 0 ? SingleprojectDS.ProjectCustomFields.Select("MD_PROP_UID='" + CFGuid + "'") : null;
                        if (LookupValueGuid == Guid.Empty)
                        {
                            if (ProjectDatacustomrow != null)
                            {
                                int FieldType =Convert.ToInt32( ProjectDatacustomrow[0]["FIELD_TYPE_ENUM"].ToString());
                                switch (FieldType)
                                {
                                    case 4:
                                         ProjectCustomFieldValue = ProjectDatacustomrow[0]["DATE_VALUE"].ToString();
                                        break;
                                    case 21:
                                        ProjectCustomFieldValue = ProjectDatacustomrow[0]["TEXT_VALUE"].ToString();
                                        break;
                                    case 15:
                                        ProjectCustomFieldValue = ProjectDatacustomrow[0]["NUM_VALUE"].ToString();
                                        break;
                                    case 9:
                                        ProjectCustomFieldValue =Convert.ToString( Convert.ToInt32( ProjectDatacustomrow[0]["NUM_VALUE"])/100);
                                        break;
                                    case 6:
                                        ProjectCustomFieldValue =Convert.ToString( Convert.ToInt32( ProjectDatacustomrow[0]["DUR_VALUE"])/4800);
                                        break;

                                }
                                //ProjectCustomFieldValue = ProjectDatacustomrow[0]["TEXT_VALUE"].ToString();
                            }
                        }
                        else
                        {

                            if (ProjectDatacustomrow != null)
                            {
                                Guid ProjectLookupEntityGUID = new Guid(ProjectDatacustomrow[0]["CODE_VALUE"].ToString());
                                LookupTableDataSet.LookupTableTreesRow LookupValueDataRow = lookUpDataSet.LookupTableTrees.FindByLT_STRUCT_UID(ProjectLookupEntityGUID);
                                ProjectCustomFieldValue = LookupValueDataRow.LT_VALUE_TEXT;

                            }
                        }
                    }

                }
                // }

            }
            catch (Exception ex)
            {

                Byte[] title = new UTF8Encoding(true).GetBytes(ex.InnerException.Message);
                fs.Write(title, 0, title.Length);
            }
            return ProjectCustomFieldValue;
        }

Check Custom Field lookup Type or Non-Lookup Type using PSI

 public static Guid GetGuidForLTValue(CustomFieldDataSet CFDataSet, LookupTableDataSet LUDataSet, Guid CFGuid, string CustomFieldName)
        {
            DataRow[] customrow = CFDataSet.CustomFields.Select("MD_PROP_NAME='" + CustomFieldName + "'");
            CFGuid = new Guid(customrow[0]["MD_PROP_UID"].ToString());

            Guid LookupValueGuid = Guid.Empty;
            try
            {
                if (CFDataSet.CustomFields.Select("MD_PROP_UID = '" + CFGuid + "'").Length > 0)
                {
                    string CFLT = (customrow.Length > 0 ? customrow[0]["MD_LOOKUP_TABLE_UID"].ToString() : (string.Empty));

                    if (!string.IsNullOrEmpty(CFLT))
                    {
                        Guid CFLTGuid = new Guid(CFLT);
                        if (LUDataSet.LookupTableTrees.Select("LT_UID = '" + CFLTGuid + "'").Length > 0)
                        {
                            LookupValueGuid = new Guid(LUDataSet.LookupTableTrees.Select("LT_UID = '" + CFLTGuid + "'")[0][LUDataSet.LookupTableTrees.LT_STRUCT_UIDColumn].ToString());
                        }
                    }
                    else
                    {

                    }
                }

            }
            catch (Exception ex)
            {

                Byte[] title = new UTF8Encoding(true).GetBytes(ex.InnerException.Message);
                fs.Write(title, 0, title.Length);
            }
            return LookupValueGuid;
        }

 


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