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 (without look up data) in PWA - CSOM

In this article, I will show you how to set/write custom fields (without 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.

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(), "Field Name"); // GetFieldInternalName is the custom method which returns the column internal name
//Replace "Field Name" with your own column name

 

               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;

        }

Happy project server programming...

No comments:

Post a Comment