0

We are using Microsoft Project Server 2013 within Microsoft SharePoint 2013. Is it possible to create a Task in a Project for example from a WebPart via C# in CodeBehind?

STORM
  • 4,005
  • 11
  • 49
  • 98

1 Answers1

2

I don't know if you can do a Webpart to create tasks but, via CSOM you can connect to your PS2013 Projects and create some tasks.

I will post you here a piece of code that could help you:

You must first "Check-Out" the project like this:

projContext.Load(projContext.Projects);
projContext.ExecuteQuery();

var proj = projContext.Projects.First(p => p.Name == "Project");
projContext.ExecuteQuery();

var draftProj = proj.CheckOut();

projContext.Load(draftProj.Tasks);
projContext.ExecuteQuery();

CreateNewTask(draftProj);

Then, you can call the method of create new task sending the "draft project"

private static void CreateNewTask(DraftProject draftProj)
{
    TaskCreationInformation nt = new TaskCreationInformation();

    nt.Name = "Task name";
    nt.Start = DateTime.Today;
    nt.Duration = "20d";
    nt.Id = Guid.NewGuid();

    draftProj.Tasks.Add(nt);
    projContext.Load(draftProj.Tasks);

    draftProj.Update();
    projContext.ExecuteQuery();

}

Hope that helps,

Marc Lluis
  • 61
  • 7
  • 1
    Hi Marc, thanks for the post. But the problem is the user authorization. I mean this code works fine when using SharePoint Online/PWA Online or using from a Console Application. But it does not work when you execute it from a "artefact" within SharePoint. Cause the Project Context is always executed by the IIS User/App Pool Account. Within Project Online you can use a Token for the user and execute within the users context. But that is not possible within OnPremise SharePoint. And Username/Password is not an option. No user would type in credentials (again) within enterprise SSO environment. – STORM Jul 13 '16 at 22:06
  • Hi Storm, I'm so sorry hearing that and also sorry for not helping you and missunderstood what you was asking for. Well, if I find some solution to your problem, don't doubt I will post it here. Thanks for the answer! – Marc Lluis Jul 14 '16 at 09:29
  • No problem. Thanks trying helping me. – STORM Jul 14 '16 at 15:48
  • @STORM There is a javascript CSOM -- that, I believe, uses the user's current session -- You could probably build a webpart that uses that API. -- We definitely use the SharePoint version of that API in web apps that run inside of SharePoint. – BrainSlugs83 Sep 18 '16 at 22:43
  • @MarcLluis -- is it not necessary to Wait for the `QueueJob` returned by the `.Update` call? -- and is it not necessary to call `draftProj.CheckIn` afterwards? (Like does it leave the project in a weird "checked out" state?) – BrainSlugs83 Sep 18 '16 at 22:45
  • @BrainSlug: We have tested this but the "authorization" worked only within the PWA sitecollection not within "normal" sitecollection outsite pwa. There we had to provide user credentials. Is this also your experience? – STORM Sep 19 '16 at 10:35
  • @BrainSlugs83 -- Yep, there some missing things because of I resume the answer but you are in truth, you need to call `.Update` and also `.CheckIn` to set in the "current" status that you could spect to find a project as a Project Manager – Marc Lluis Sep 21 '16 at 14:33
  • its return this error "CICOCheckedOutInOtherSession" while executing check out – Seyed Reza Dadrezaei Nov 13 '19 at 13:17