0

I am using CSOM in order to import Enterprise Resources in Project Server 2013 from an external file. Resources, in my case, have some custom fields. The code is the following:

    private static void InitializeContext()
    {
        Context = new ProjectContext(PwaPath);
        Context.Credentials = Credentials;
    }

    static void Main(string[] args)
    {
        InitializeContext();

        // Load resources and custom fields
        Context.Load(Context.EnterpriseResources);
        Context.Load(Context.CustomFields);
        Context.Load(Context.LookupTables);
        Context.ExecuteQuery();

        var newResourceId = Guid.NewGuid();
        var newResourceInfo = new EnterpriseResourceCreationInformation
        {
            Id = newResourceId,
            IsBudget = false,
            IsGeneric = false,
            IsInactive = false,
            Name = "AName",
            ResourceType = EnterpriseResourceType.Work
        };
        Context.EnterpriseResources.Add(newResourceInfo);
        Context.EnterpriseResources.Update();
        Context.Load(Context.EnterpriseResources);
        Context.ExecuteQuery();
        EnterpriseResource newResource = Context.EnterpriseResources.GetByGuid(newResourceId);
        LookupTable lTable = Context.LookupTables.First(lut => lut.Name == "SomeText");
        Context.Load(lTable.Entries);
        Context.ExecuteQuery();
        LookupEntry entry = lTable.Entries.First(c => c.FullValue.EndsWith("SomeText"));
        CustomField cField = Context.CustomFields.First(c => c.Name == "aValue");
        newResource[cField.InternalName] = entry.FullValue;
        Context.EnterpriseResources.Update();
    }

The last 2 lines of code should update the value (read from a lookup table) of the field, but they don't. Anybody has any idea why?

Thanks in advance.

P.s: This question is also asked on SO. In case it should be deleted just let me know and I will.

1 Answer 1

1

I think you miss Context.ExecuteQuery(); after Context.EnterpriseResources.Update();

The below simple example show how to update a resource custom field that uses a value that is picked up from a Lookup table by using CSOM that I think can help you.

var cfInternalName = "Custom_aaf4156c7804e511943500155d569905";
            foreach (EnterpriseResource res in resources)
            {
                string[] Newval = new string[] { "Entry_333982d27604e511943500155d569905" };     //Lookup table row value UID without "-"   and small letters                                             
                res[cfInternalName] = Newval;                               
            }
            projContext.EnterpriseResources.Update();
            projContext.ExecuteQuery();

For more details check how to update a resource custom field that uses a value that is picked up from a Lookup table by using CSOM?

2
  • 1
    thanks for the reply. It helped a lot. What I was doing wrong was assigning the wrong value to the field and, as you mentioned, omitting the Context.ExecuteQuery(); command. It should've been newResource[cdcField.InternalName] = new string[] { cdcEntry.InternalName }; and not newResource[cdcField.InternalName] = cdcEntry.FullValue; Commented Aug 23, 2016 at 9:11
  • Good Job my bro , Commented Aug 23, 2016 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.