Updating Resources Custom Field values using CSOM, Project Online, #Project Server 2013

Project Online applications need to be written using CSOM or OData rather the PSI which will be deprecated soon.

The CSOM – Client-side object model implements common server functionality.

If you are not familiar with development in Project Server 2013 refer to the links below to learn about CSOM, OData, JSOM.

ProjectData – Project 2013 OData service reference
https://msdn.microsoft.com/en-us/library/office/jj163015(v=office.15).aspx

Client-side object model (CSOM) for Project 2013
https://msdn.microsoft.com/en-us/library/office/jj163123(v=office.15).aspx

What the CSOM does and does not do
https://msdn.microsoft.com/en-us/library/office/jj163082.aspx

The solution has been developed as a console app using Visual Studio 2015 allowing to specify resource name, custom resource field name , field value

The code from the console app

1. The projectContext has to be initialised using the url. If the user running the app does not have permissions to the PWA site, then user name and password need to be specified by adding the code

ProjContext.Credentials = new SharePointOnlineCredentials credentails = new SharePointOnlineCredentials(username, password);

2. Find the guid of the resource you want to update
and set it against variable resUID

3. The code find the resource based on resUID provided, the resource entity id and internal name of the resource custom field

4. The value of the custom field is updated by the following line

entRes2Edit[customfield.First().InternalName] = “3456”;
If an out of the box field is updated , e.g. CanLevel. The property can be accessed simply as follows
// Toggle the CanLevel property.
entRes2Edit.CanLevel = !entRes2Edit.CanLevel;

To commit the change , use the following lines
projContext.EnterpriseResources.Update();

// Save the change.
projContext.ExecuteQuery();


class Program
{
private const string pwaPath = "https://radev/PWA/"; // Change the path for Project Web App.

// Set the Project Server client context.
private static ProjectContext projContext;

// For applications that access both the Project Server CSOM and the SharePoint CSOM, you could
// use the ProjectServer object. Those statements are commented out in this application.
// However, it is not necessary to instantiate a ProjectServer object, because the the
// ProjectContext object inherits from ClientContext in SharePoint.

static void Main(string[] args)
{
projContext = new ProjectContext(pwaPath);
//GUID for reshmee auckloo
Guid resUID = new Guid("02C5EE34-5CE8-E411-80C1-00155D640C06");
string customFieldName = "Staff Number";
string customFieldValue = "000000";
// Get the list of published resources and custom fields in Project Web App.
projContext.Load(projContext.EnterpriseResources);
projContext.Load(projContext.CustomFields);

projContext.ExecuteQuery();

int numResInCollection = projContext.EnterpriseResources.Count();
var usrs = projContext.Web.SiteUsers;

if (numResInCollection > 0)
{
projContext.Load(projContext.EnterpriseResources.GetByGuid(resUID));
projContext.Load(projContext.EntityTypes.ResourceEntity);
projContext.ExecuteQuery();

var entRes2Edit = projContext.EnterpriseResources.GetByGuid(resUID);

var userCustomFields = entRes2Edit.CustomFields;

Guid ResourceEntityUID = projContext.EntityTypes.ResourceEntity.ID;

var customfield = projContext.CustomFields.Where(x => x.Name == customFieldName);

entRes2Edit[customfield.First().InternalName] = "3456";

Console.WriteLine("\nEditing resource : GUID : Can Level");
Console.WriteLine("\n{0} : {1} : {2}", entRes2Edit.Name, entRes2Edit.Id.ToString(),
entRes2Edit.CanLevel.ToString());

// Toggle the CanLevel property.
entRes2Edit.CanLevel = !entRes2Edit.CanLevel;

// The entRes2Edit object is in the EnterpriseResources collection.
projContext.EnterpriseResources.Update();

// Save the change.
projContext.ExecuteQuery();

// Check that the change was made.
projContext.Load(projContext.EnterpriseResources.GetByGuid(resUID));
projContext.ExecuteQuery();

entRes2Edit = projContext.EnterpriseResources.GetByGuid(resUID);

Console.WriteLine("\n\nChanged resource : GUID : Can Level");
Console.WriteLine("\n{0} : {1} : {2}", entRes2Edit.Name, entRes2Edit.Id.ToString(),
entRes2Edit.CanLevel.ToString());
}

Console.Write("\nPress any key to exit: ");
Console.ReadKey(false);

}

}


The solution can be downloaded from Github link
https://github.com/reshmee011/ProjectServer-2013/tree/master/CSOM_ProjectServer2013

Advertisements

One thought on “Updating Resources Custom Field values using CSOM, Project Online, #Project Server 2013

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s