Cannot change project owner

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?!

Upvotes: 1

Views: 957

Answers (1)

Eric MPC
Eric MPC

Reputation: 1

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.

Upvotes: 0

Related Questions