Sunday, April 7, 2013

Project Server - Read custom field with value from lookup

In one of my previous posts, I've described how to update value of a Enterprise custom field that contains value from a Lookup table. 

Reading of that field is similar, only shorter and easier. Here, I'm using PSI functions from FluentPS library which is free and easy to use but the same PSI functions can be used when you set your own call of Project Server web services.

Let's say we have custom field called "Product" and it contains values from Lookup table called "Products".

NOTE: If Custom field on your project contains no value, then you won't be able to see that field on that project. If field is empty, Project Server acts as if that field doesn't exist.  


This is the code to do it:

        public bool ReadProduct(string projectUid)
        {        
            Guid projectGuid = new Guid(projectUid);

            if (projectGuid.Equals(Guid.Empty)) return false;
            var logService = new LogService();
            var sessionService = new PSSessionService()
            {
                HostName = "server_name",
                SiteName ="PWA"
            };

             FluentPS.Services.Impl.PsiContextService psiContextService = new PsiContextService();
             FluentPS.Services.Impl.PSISvcsFactory psiSvcsFactory = new PSISvcsFactory(sessionService, psiContextService);

             FluentPS.WebSvcLookupTable.LookupTable svcLookupTable = psiSvcsFactory.CreateSvcClient<FluentPS.WebSvcLookupTable.LookupTable>();
             FluentPS.WebSvcCustomFields.CustomFields svcCustomFields = psiSvcsFactory.CreateSvcClient<FluentPS.WebSvcCustomFields.CustomFields>();

             FluentPS.WebSvcProject.Project svcProject = psiSvcsFactory.CreateSvcClient<FluentPS.WebSvcProject.Project>();


             FluentPS.WebSvcProject.ProjectDataSet _project = svcProject.ReadProject(projectGuid, FluentPS.WebSvcProject.DataStoreEnum.WorkingStore);

             try
             {
                 //Guid of lookup table (Product) in which we look for value
                 Guid _lookupTableUid = Guid.Empty;
                 //MD Guid of lookup custom field
                 Guid _lookupCustomFieldGuid = Guid.Empty;


                 FluentPS.WebSvcCustomFields.CustomFieldDataSet customFieldsDs = svcCustomFields.ReadCustomFields("", false);

                 //First, we need to find Lookup table guid for that Custom field on our project
                 FluentPS.WebSvcCustomFields.CustomFieldDataSet.CustomFieldsDataTable cfDataTable = customFieldsDs.CustomFields;
                 for (int i = 0; i < cfDataTable.Count; i++)
                 {
                     if (cfDataTable[i].MD_PROP_NAME == "Product")
                     {
                         _lookupCustomFieldGuid = cfDataTable[i].MD_PROP_UID;
                         _lookupTableUid = cfDataTable[i].MD_LOOKUP_TABLE_UID;

                         break;
                     }

                 }

                 //Then, we need to find the value of guid of Lookup table value stored in that Custom field
                 Guid _lookupTableValueGuid = Guid.Empty;
                 foreach (FluentPS.WebSvcProject.ProjectDataSet.ProjectCustomFieldsRow cfRow in _project.ProjectCustomFields)
                 {
                     if (cfRow.MD_PROP_UID == _lookupCustomFieldGuid)
                     {
                         _lookupTableValueGuid = cfRow.CODE_VALUE;
                     }

                 }

                 string _value = "";

                 using (FluentPS.WebSvcLookupTable.LookupTableDataSet lookupTableDs = svcLookupTable.ReadLookupTables(string.Empty, false, 1033))
                 {
                  //now, we search through Lookup table for the text value which corresponds to guid found in previous loop
                  for (int i = 0; i < lookupTableDs.LookupTableTrees.Count; i++)
                  {
                       try
                       {
                             if (lookupTableDs.LookupTableTrees[i].LT_STRUCT_UID == _lookupTableValueGuid)
                             {
                                   //and here we read the value we were looking for
                                   _value = lookupTableDs.LookupTableTrees[i].LT_VALUE_TEXT;
                                   break;
                             }
                        }
                        catch (Exception)
                        {
                                //this is only for possible null values
                         }

                  }
              }

              return _value;

          }
          catch (Exception)
          {
                return null;
          }
           
 }

No comments:

Post a Comment