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.

Monday 25 July 2016

Project Server 2013 - Update/Write/Set Project Server 2013 Custom Field values (with look up data) in PWA - CSOM

In this article, I will show you how to set/write custom fields (with look up data) for the project (project server 2013). In this series of articles, I am taking windows application as a working example to get data from PWA.

In a nutshell, we can’t directly set value to custom field which has look up values. In order to set the value programmatically, first we should resolve/find whether the value is part of the look up list or not. Only, you can set/write available values in the look up table. In order to achieve this, we should pass the value to the look up entries and return the right value and then set accordingly.

I have explained in detail through code.

Note: By default, you can’t access custom fields and their properties. Hence, the trick is, we should create custom 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

Step 1: Create windows application from visual studio

Step 2: Add Microsoft.ProjectServer.Client.dll

Step 3: 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; }
    }

Step 4: Add button control to form.

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

Step 6: Add following either in form load or button click event

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

Step 7: Define project context on Form load

private static ProjectContext projContext;

Step6: Add following code on button click event

private void setCustomFieldValue_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.Projects);

                projContext.ExecuteQuery();

                PublishedProject pubProject = projContext.Projects.Where(proj => proj.Name == “projName”).FirstOrDefault(); //pass respective project name

 projContext.ExecuteQuery();

projContext.Load(pubProject.CustomFields);

                projContext.ExecuteQuery();

                projContext.Load(pubProject.IncludeCustomFields);

                projContext.ExecuteQuery();

                DraftProject projectDraft = pubProject.CheckOut();

projectDraft.SetCustomFieldValue(GetFieldInternalName(projContext, “

Field Name”).ToString(), GetLookUpEntries(projContext, “Field Name”, “Value to be set”)); 

// GetFieldInternalName is the custom method which returns the column internal name

// GetLookUpEntries is the custom method which will returns the value from field lookup table.

// Pass respective column name and value to be set

               projectDraft.Update();

projContext.ExecuteQuery(); 

                projectDraft.Publish(true);               

                projectDraft.CheckIn(true);

                projContext.ExecuteQuery();

// Custom method return field internal name

private string GetFieldInternalName(ProjectContext projContext, string fieldName)
        {
            string fieldInternalName = string.Empty; 

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

            try
            {
                projContext.Load(projContext.CustomFields);

                projContext.ExecuteQuery(); 

                var customFieldEnum = projContext.CustomFields.GetEnumerator(); 

                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;

                    customFieldList.Add(customFieldData);

                }

                projContext.Load(projContext.Projects);

                projContext.ExecuteQuery();

                foreach (CustomField customField in customFieldList)

                {

                    if (customField.Name.ToString().Equals(fieldName, StringComparison.OrdinalIgnoreCase))

                    {

                        fieldInternalName = customField.InternalName.ToString();

                        return fieldInternalName;

                    }

                }

            }

            catch(Exception ex)
            {          

            }

            return fieldInternalName;

        }

private string[] GetLookUpEntries(ProjectContext projContext, string fieldName, string fieldValue)
        {
            string[] valueSet = new string[] { "" };

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

            try
            {

                projContext.Load(projContext.CustomFields);

                projContext.ExecuteQuery(); 

                var customFieldEnum = projContext.CustomFields.GetEnumerator(); 

                while (customFieldEnum.MoveNext())
                {
                    var customField = customFieldEnum.Current;

                    if (customField.Name.ToString().Equals(fieldName, StringComparison.OrdinalIgnoreCase))
                    {

                        var lookupTable = customField.LookupTable;

                        projContext.Load(lookupTable);

                        projContext.ExecuteQuery(); 

                        var ltEntries = lookupTable.Entries;

                        projContext.Load(ltEntries);

                        projContext.ExecuteQuery(); 

                        var ltEnum = ltEntries.GetEnumerator(); 

                        while (ltEnum.MoveNext())
                        {
                            var ltEntry = ltEnum.Current;

                            CustomField ltValues = new CustomField();

                            ltValues.InternalName = ltEntry.InternalName;

                            ltValues.FullValue = ltEntry.FullValue;

                            ltValues.Description = ltEntry.Description;

                            customFieldList.Add(ltValues);

                        }

                    }

                }// end of while 

                foreach (var value in customFieldList)
                {
                    if (value.FullValue.ToString().Equals(fieldValue, StringComparison.OrdinalIgnoreCase))
                    {
                        valueSet = new string[] { value.InternalName.ToString() };

                    }

                }

            }
            catch(Exception ex)            {

            }
            return valueSet;
        }

Happy project server programming...

 

Project Server 2013 - Update/Write/Set Project Server 2013 Custom Field values (without look up data) in PWA - CSOM

This summary is not available. Please click here to view the post.