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?
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,
-
1Hi 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 9:29
-
-
@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 calldraftProj.CheckIn
afterwards? (Like does it leave the project in a weird "checked out" state?) – BrainSlugs83 Sep 18 '16 at 22:45