1

I can change project's name , start date etc in my code but my change to project's owner doesn't apply to project server. here is my code:

        ProjectContext projectContext = new ProjectContext("http://servername:12247/PWA/");

        var projectContextVar = projectContext.LoadQuery(
                 projectContext.Projects.Include(
                 p => p.Id,
                 p => p.Name,
                 p => p.StartDate,
                 p => p.FinishDate,
                 p => p.IncludeCustomFields,
                 p => p.IncludeCustomFields.CustomFields,
                 p => p.Owner.LoginName
       ));
        projectContext.ExecuteQuery();
        PublishedProject pubPro = null;
        foreach (var p in projectContextVar)
        {
            if (new Guid("86C21C48-71BE-E811-80C4-00155D011303") == p.Id)
            {
                pubPro = p;
            }
        }


        DraftProject draft;
        draft = pubPro.Draft;
        JobState job1 = projectContext.WaitForQueue(draft.CheckIn(true), 20);

        draft = pubPro.CheckOut();
        projectContext.Load(draft);
        projectContext.ExecuteQuery();
        var resources = projectContext.EnterpriseResources;
        projectContext.Load(resources);
        projectContext.ExecuteQuery();
        foreach (EnterpriseResource er in resources)
        {
            if (er.Name.Equals("some name"))
            {
                var o = er.User;
                projectContext.Load(o);
                projectContext.ExecuteQuery();
                projectContext.Load(draft.Owner);
                projectContext.ExecuteQuery();
                draft.Owner = o;
                Console.WriteLine("changed...");
            }
        }
        draft.Update();
        JobState jobState = projectContext.WaitForQueue(draft.Publish(true), 10);

the draft project's owner at last is successfully changed to the user that is expected, but after publishing the draft project, the change doesn't apply to the project. can any body say what is the problem with my code, or god forbid by project-server?!

1 Answers1

0

Your code is very close, but I don't believe it is possible to set a project owner based on an EnterpriseResource user object.

Instead, try using an SharePoint users object.

var newOwner = projectContext.Web.SiteUsers.GetByLoginName("some name");
draft.Owner = newOwner;

newOwner here will be an object of type Microsoft.SharePoint.Client.User, which is what the "Owner" field in the ProjectDraft class is expecting.

  • I have already tried to set the owner in this way, it will be set but after publishing the draft project , the change will not apply... Do you know what would be the problem?? – Amir Hossein Eyvazkhani Feb 02 '19 at 08:26
  • Hmmm odd. I'm seeing references online that suggest you need to call projectContext.ExecuteQuery() after you call draft.Update(). After both of those calls, then try to publish your project and check it in. I've never had to do that for other project changes but that might help in this case with the owner. – Eric MPC Feb 04 '19 at 13:17
  • Thanks for your response. – Amir Hossein Eyvazkhani Feb 07 '19 at 12:01
  • Did calling ExecuteQuery() after updating the draft fix your issue? – Eric MPC Feb 07 '19 at 17:34