Read Lookup and Non-Lookup custom field value using odata service project server

Read Lookup and Non-Lookup custom field value using odata service project server

Read Lookup and Non-Lookup custom field value using odata service project server. Reporting data in Project Server can be read in multiple ways. Business Intelligence includes a data connection in Project Web App for OData reporting. In this article, I am going to create a simple console application to create OData connection and use of OData service using Linq Query.

  1. Open visual studio and create console application.
  2. Add ProjectData service reference to providing below address “http://ServerName/PWAName/_api/ProjectData/”

 

 

Add service reference in OData

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PRCLNT = Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using Microsoft.ProjectServer.Client;
using System.Security;
using OdataService.Pdata;
using System.Net;

namespace OdataService
{

    class Program
    {

        static string pwaPath = "http://ServerName/PWA/";
        static string PSDATA = "http://ServerName/PWA/_api/ProjectData";
        static string UserName = "UserName";
        static string Password = "Password";
        static string Domain = "Domain";
        private static ProjectContext projContext;
        
        static void Main(string[] args)
        {
            ReadProjectInformationOData();
        }
        private static void ReadProjectInformationOData()
        {

            string MainProjectCode = string.Empty;
            try
            {
                Dictionary<string, object> projDict = new Dictionary<string, object>();
                string NewProjectCode = string.Empty;
                projContext = new ProjectContext(pwaPath);

                ReportingData context =
                    new ReportingData(new Uri(PSDATA, UriKind.Absolute));

                context.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);
                var PQProjectDetails = from p in context.Projects
                                       orderby p.ProjectName
                                       select p;

                foreach (Pdata.Project proj in PQProjectDetails)
                {
                    string ProjName = proj.ProjectName;
                    string ProjectOwner = proj.ProjectOwnerName;

                }
            }

            catch (Exception ex)
            {

            }
            finally
            {

            }

        }
    }
}

You can read lookup type and non-lookup type custom field value using this code

 foreach (Pdata.Project proj in PQProjectDetails)
                {
                    string CustmField = proj.CustomFieldName;
                   
                }

You can read task and resource assigned on task using below code. Only you can see your own task because you are running this code using your credential.

 foreach (Pdata.Project proj in PQProjectDetails)
                {
                    string CustmField = proj.CustomFieldName;
                    string ProjName = proj.ProjectName;
                    string ProjectPercentComplt = proj.ProjectPercentCompleted.Value.ToString();
                    string ProjectOwner = proj.ProjectOwnerName;
                    foreach (Pdata.Task tsk in proj.Tasks)
                    {
                        string TaskName = tsk.TaskName;
                        string TaskPercentCmpl = tsk.TaskPercentCompleted.Value.ToString();
                        foreach (Pdata.Assignment assgn in tsk.Assignments)
                        {
                            string ResourceName = assgn.ResourceName;
                        }
                    }

                }

 


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