none
Setting the Project Owner when initially creating a Project with CSOM RRS feed

  • Question

  • Can someone explain how to set a project owner when when I initially create a project using the CSOM way?

    I have tried the code below and it creates the project, but it does not set the project owner that was found via the query below.

    When I checked the queue, it says that the project is not checked out as if it is trying to do an update after the fact.

    I need the project owner to be set initially so that all tasks under the project template in the EPT get the status manager set as the project owner. If I change it after the fact, the status manager does not change for the tasks. I have to believe that we can set this project owner (and other things like custom fields) upon initial creation.

    Please help.

    User owner = null; var resourcesQuery = projContext.LoadQuery(projContext.EnterpriseResources.Where(p => p.Name == "Some User")); projContext.ExecuteQuery(); var resources = resourcesQuery.SingleOrDefault(); if (resources != null && resources.User != null) { owner = resources.User; } ProjectCreationInformation newProj = new ProjectCreationInformation(); newProj.Id = Guid.NewGuid(); newProj.Name = ProjName; newProj.Description = projectDescription; newProj.Start = projectStart.Date; PublishedProject newPublishedProj = projContext.Projects.Add(newProj); newPublishedProj.Owner = owner; // trying to set owner before "Update" and "ExecuteQuery"

    // I have also tried setting newProblishedProj.Draft.Owner with NO success. QueueJob qJob = projContext.Projects.Update(); projContext.ExecuteQuery();



                  

    Wednesday, February 19, 2014 5:38 PM

Answers

  • Hi Bill,

    Thanks for responding.

    Using the REST technique does seem promising, but I have had a chance to play around with this in the last few days while waiting for a response and have more info and more questions. :)

    I abandoned the idea of using CSOM and chose instead to use PSI to create the project, since I know that in that case I can set the project owner on creation. My theory that if I set the project owner on creation it would default the task status manager of all of the tasks in the EPT (which uses a project template), did not work. It still sets the status manager to the account that created the project. Bummer. But, in looking at it closer I found a post that suggested that you can now (in PS2013) set the status manager of the tasks using PSI. I used that code and updated the status manager of all tasks to the project owner after creation and got through my requirement with this technique. Woohoo! I don't think that was possible in PS2010.

    As far as your alternate way (setting the "owner" after the fact with DraftAssignment objects), this is not the right scope. What I wanted to do is set the status manager of the task (not the assignment owner). So theoretically, using CSOM, I would be able to set the Status Manager of the task level with the DraftTask object, BUT I don't see a StatusManager property here either. Maybe it's available in the REST interface?

    So for now, I will still use the PSI and look into the REST techniques in the future. CSOM is still limiting for complex requirements (at least for this one).

     

    Thursday, February 27, 2014 3:52 PM

All replies

  • Hi Carlos,

    Here is the REST post that will update the project owner:

    POST http://projects.contoso.com/pwa/_api/ProjectServer/Projects('16284a27-a7e3-497b-85ed-96e0c3a9dde0')/Owner HTTP/1.1
    User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
    Content-Type: application/json; odata=verbose
    Accept: application/json; odata=verbose
    X-RequestDigest: 0xA7A528E8070DE39F395EC9E1F4518CF4CB6B8A33DAC066BE337987FAC1D6FA119F905A0EC583B5C6E8EE608E6C4ABA3797C91A3F43D8B6489B3E80E53E5433D7,20 Feb 2014 04:17:56 -0000
    Accept-Language: en-us
    Accept-Encoding: gzip, deflate
    Cookie: FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1A+RmFsc2UsMGguZnxtZW1iZXJzaGlwfDEwMDMzZmZmODg2ZTM0NWNAbGl2ZS5jb20sMCMuZnxtZW1iZXJzaGlwfGNocmlzQHByb2pjb25mMzAzLm9ubWljcm9zb2Z0LmNvbSwxMzAzNzc3NTQ3NTk5Mjg3NzYsRmFsc2UsQnhtZ2NWMEo0QTZFUk0ySUU4VXgxMCszT0xBZCttd0pOVDdSbFJsUDZjTUNWYzd3WkUzWTlEYnd0VmRXYm5kaU5WZDJHd3BmODRhR3BFNW5ndTVsK1hYbER4WEJaT1V6bXI2aGE5aHZXSVBpK3lPaTh1a0JCY1AvaDZFMjRmcmFqNllJMEpzZGZnQ3pSS1ExaHV3YncvR29vWXpVY3BqM2RVZWJKRXAvamtyaTlvb1VtSFRFaS81STBpTHNISmo1Z0dkdndhTFg4V1pYSlNRd3NTWlk1UVNZamNNTGRRNHBTYTljTUZuR3FxZE5KWXBwYUQzUy9hbTh2eFVnMEp2dEtia2daMWZaNGVzWWxnZ3JKc1pkQm03SjNtTWNnM2N0MVZtK1lvMElMSE8vclJ1MjRGeFhMSElNZVc4YW5vd1VQVFlqT2FOcnA4MEdpUVRJM0p1WTJ3PT0saHR0cHM6Ly9wcm9qY29uZjMwMy5zaGFyZXBvaW50LmNvbS88L1NQPg==;
    X-HTTP-Method: MERGE
    Host: projects.contoso.com
    Content-Length: 47
    
    {"__metadata":{"type":"SP.User"},"Id":2,"LoginName":"i:0#.w|contoso\\sp_farm","Title":"SharePoint Farm","PrincipalType":1,"Email":"","IsSiteAdmin":true,"UserId":{"__metadata":{"type":"SP.UserIdInfo"},"NameId":"s-1-5-21-1078081533-1343024091-1957994488-3650","NameIdIssuer":"urn:office:idp:activedirectory"}}}


    Chris Givens CEO, Architecting Connected Systems Blog Twitter

    Thursday, February 20, 2014 4:41 AM
  • Hi Chris,

    While I appreciate your quick response, this is not what I am looking for. From the REST post I can tell that this is a MERGE or an update after the creation. I know how to do this with CSOM, that's not the question.

    I would like to do this on project creation because of the very common Status Manager issue. When you change the project owner after it has been created the status manager stays with the old project manager. Also, the PSI (and probably CSOM) does not let me change the status manager. This has never been supported.

    Anyway, my question is 

    1. How to set the project owner on project creation

    2. If possible, please provide CSOM code to do it with. I have some code that I posted above and I don't know why the project owner field gets ignored on project creation.

    Carlos


    Thursday, February 20, 2014 2:13 PM
  • Can someone at Microsoft please address this question?

    Carlos

    Tuesday, February 25, 2014 3:32 PM
  • Hi Carlos,

    I haven't tried this yet, but I think that if you use the REST technique Chris suggested and POST the PublishedProject, including the Owner property, to the _api/ProjectServer/Projects collection endpoint it will merge the Owner property of the PublishedProject entity you provided into the initial ProjectDataSet used in the QueueCreateProject call behind the scenes. It behaves differently from the CSOM ExceuteQuery batch in your sample, which will treat setting the Owner property as a separate update to be performed after the project has been created and encounter the Status Manager issue you're seeing.

    Alternatively, if you'd like to workaround this using the managed CSOM proxy, have you tried setting the Owner property on all of the DraftAssignment objects? You'll need to wait for the creation QueueJob returned from ProjectCollection.Update() to complete to be able to enumerate the assignments, but it looks like DraftAssignment.Owner is a settable property which should do what you want.

    Hope that helps,

    -Bill

    Thursday, February 27, 2014 6:16 AM
  • Hi Bill,

    Thanks for responding.

    Using the REST technique does seem promising, but I have had a chance to play around with this in the last few days while waiting for a response and have more info and more questions. :)

    I abandoned the idea of using CSOM and chose instead to use PSI to create the project, since I know that in that case I can set the project owner on creation. My theory that if I set the project owner on creation it would default the task status manager of all of the tasks in the EPT (which uses a project template), did not work. It still sets the status manager to the account that created the project. Bummer. But, in looking at it closer I found a post that suggested that you can now (in PS2013) set the status manager of the tasks using PSI. I used that code and updated the status manager of all tasks to the project owner after creation and got through my requirement with this technique. Woohoo! I don't think that was possible in PS2010.

    As far as your alternate way (setting the "owner" after the fact with DraftAssignment objects), this is not the right scope. What I wanted to do is set the status manager of the task (not the assignment owner). So theoretically, using CSOM, I would be able to set the Status Manager of the task level with the DraftTask object, BUT I don't see a StatusManager property here either. Maybe it's available in the REST interface?

    So for now, I will still use the PSI and look into the REST techniques in the future. CSOM is still limiting for complex requirements (at least for this one).

     

    Thursday, February 27, 2014 3:52 PM
  • Hi Chris,

    I am trying to update a project using your example but I keep getting a "No Content" response. I have all of the headers that you do except the Cookie. How did you get that value?

    (I am using c# in a console application)

    Thanks,


    120811049008

    Monday, August 25, 2014 5:39 PM
  • Hi Chris.

    I've tried to update the project owner using the call you suggested: the response doesn't give me any error, but when I check the data back I can see that just the Title and Email fields are update...the ID and Login are still those of the old Owner and when I check in PWA I still see the old one.

    Have you got any idea?

    Fabrizio.

    Friday, October 31, 2014 9:39 PM
  • Try It... This is Working for Me

    using (ProjectContext context =new ProjectContext(url))
                {
                    string projname = "DemoProject7";
                    string puser = "farmadmin";
                    context.Load(context.Projects, o => o.Where(i => i.Name == projname));
                    context.ExecuteQuery();

                    var users = context.LoadQuery(context.Web.SiteUsers.Where(o => o.Title == puser));
                    context.ExecuteQuery();

                    foreach (PublishedProject p in context.Projects)
                    {
                        DraftProject dftproj = p.CheckOut();
                        context.Load(dftproj);
                        context.ExecuteQuery();

                        foreach (User u in users)
                        {
                            Console.WriteLine(u.Title);
                            dftproj.Owner = u;
                        }

                        QueueJob job= dftproj.Update();
                        job = dftproj.Publish(false);
                        JobState state = context.WaitForQueue(job, 10);
                        if (state==JobState.Success)
                        {
                            dftproj.CheckIn(true);
                            context.ExecuteQuery();
                        }
                    }
                }

    • Proposed as answer by Abidulla Friday, November 7, 2014 12:47 PM
    • Unproposed as answer by Abidulla Friday, November 7, 2014 5:09 PM
    • Proposed as answer by Mokhtar Bepari Friday, November 7, 2014 5:31 PM
    Friday, November 7, 2014 12:47 PM
  • Hi friends
    Please try the below code, its working for me

    private static string Username = "i:0#.w|dev\\muralidhar.kumar";

    private static string strProjectGuid = "b61434b7-2885-4d39-81dc-2ed4950e8210";

               

    Microsoft.SharePoint.Client.User newOwner = projContext.Web.SiteUsers.GetByLoginName(Username);

                    Guid ProjectGuid = Guid.Parse(strProjectGuid);

                     DraftProject draftProjectToUpdate = projContext.Projects.GetByGuid(ProjectGuid).CheckOut();
                    draftProjectToUpdate.Owner = newOwner;
                    QueueJob qJob = draftProjectToUpdate.Update();
                    projContext.Load(qJob);
                    projContext.ExecuteQuery();

    Friday, August 7, 2015 7:57 AM
  • Hi Carlos,

    I am new to the Microsoft-Sharepoint platform, so apologies if I sound stupid.

    As you seem to be an expert on CSOM, ProjectServer...Could you please let me know if there is a way to UPDATE properties within a ProjectServer object like a 'Task' property say 'PercentComplete' using Javascript or browser?

    All the options we have tried either give a Forbidden Access or do not do the expected update.

    Any pointers would really be appreciated.

    Thanks,

    Pramod

     

    Monday, October 5, 2015 11:43 AM
  • This shows you to create a project and add tasks to it:

    https://msdn.microsoft.com/EN-US/library/office/microsoft.projectserver.client.draftproject.update_di_pj14mref.aspx

    It's a good stating point.

    Please do not hijack this post anymore. If you have a specific question, please post another question. This post is about setting the project owner.

    Tuesday, October 6, 2015 4:56 PM
  • This worked for me.   You just have to check the project out as a draft, add the owner, update the project, publish it and check it in.

    Tuesday, March 22, 2016 11:50 PM
  • Hi ,

    The below code will work for sure, have tested it on the Project Server 2016.

     using (ProjectContext context = new ProjectContext(PWAUrl))
                    {
                        string projname = "ProjectName";
                        string puser = "Raj Kumar Allepu";
                        
                        context.Load(context.Projects, o => o.Where(p => p.Name == projname));
                        context.ExecuteQuery();


                        var resources = context.LoadQuery(context.EnterpriseResources.Where(r => r.Name == puser));
                        context.ExecuteQuery();
                        var resource = resources.FirstOrDefault();

                        foreach (PublishedProject p in context.Projects)
                        {
                            if (p.IsCheckedOut) // check-in the project forecfully if it is already checked out
                            {
                                
                                DraftProject dft = p.Draft;
                                JobState state1 = context.WaitForQueue(dft.CheckIn(true), 10);
                            }

                            DraftProject dftproj = p.CheckOut();
                            context.Load(dftproj);
                            context.ExecuteQuery();

                            //set the project owner
                            if (resource != null && resource.User != null)
                                dftproj.Owner = resource.User;

                                QueueJob job = dftproj.Update();
                                job = dftproj.Publish(false);
                                JobState state = context.WaitForQueue(job, 10);
                                if (state == JobState.Success)
                                {
                                    dftproj.CheckIn(true);
                                    context.ExecuteQuery();
                                    Console.WriteLine("Update completed successfully.");
                                    Console.ReadKey(false);
                                }

    }
          }




    Wednesday, January 31, 2018 4:58 AM