About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Friday, 1 July 2016

Project Server 2013 - Get/Read custom fields in PWA - CSOM


In this article, I will show you how to get/read available custom fields and their entity type in given PWA site using CSOM in project server 2013. In this series of articles, I am taking windows application as a working example to get data from PWA.

Note: By default, you can’t access custom fields and their properties. Hence, the trick is, we should create class with required properties and load all properties and from there you should access all required info.

Prerequisites

1. Visual studio 2010/2012/2013/2015

2. Copy Microsoft.ProjectServer.Client.dll from SharePoint 2013 servers from the location

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI

Step1: Create windows application from visual studio

Step2: Add Microsoft.ProjectServer.Client.dll

Step3: Add one combo box and one button control to form.

Step4: Declare PWA site URL in form load. Change URL accordingly based on your environment

Step5: Create class called ‘CustomField’ in the same project and add following properties

    public class CustomField
    {
        public Guid Id { get; set; }

        public string Name { get; set; }

        public string InternalName { get; set; } 

        public string EntityType { get; set; }

    }

Step6: Add following either in form load or button click event

private const string pwaPath = https://dev.xxx.com/PWA/;

Step5: Define project context on Form load

private static ProjectContext projContext;

Step6: Add following code on button click event

private void loadProjectsNew_Click(object sender, EventArgs e)

{// create PWA context

projContext = new ProjectContext(pwaPath); 

// Access with default logged in credentials

projContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 

projContext.Load(projContext.CustomFields);

            projContext.ExecuteQuery();  

var customFieldEnum = projContext.CustomFields.GetEnumerator();

            List<CustomField> customFieldList = new List<CustomField>(); 

            while (customFieldEnum.MoveNext())

            {

                var customField = customFieldEnum.Current;

                CustomField customFieldData = new CustomField(); //This is your custom class

                customFieldData.Id = customField.Id;

                customFieldData.Name = customField.Name;

                customFieldData.InternalName = customField.InternalName;

                customFieldData.EntityType = customField.EntityType.Name;

                customFieldList.Add(customFieldData);               

            }

            foreach (CustomField field in customFieldList)

            {

                comboCustomFields.Items.Add(field.Name.ToString() + "-" + comboCustomFields.Items.Add(field.InternalName.ToString()));

                comboCustomFields.SelectedIndex = 0;

            }

Happy project server programming...

No comments:

Post a Comment