If you need to access Microsoft Project Server, you can use the ProjectContext class from the Microsoft.ProjectServer.Client.dll assembly. This class inherits from the SharePoint ClientContext:
[code language=”csharp”]
using System;
using Microsoft.SharePoint.Client;
namespace Microsoft.ProjectServer.Client
{
public class ProjectContext : ClientContext
{…
[/code]
and adds a few new methods and properties.
If your SharePoint is running with ADFS, you can use the GetADFSUserNameMixedAuthenticatedContext-methode to authenticate with the ClientContext. The message needs some additional information like sts (hostname of ADFS), idpId (identifier of the ADFS relying party) … and returns an authenticated ClientContext.
[code language=”csharp”]
OfficeDevPnP.Core.AuthenticationManager am = new OfficeDevPnP.Core.AuthenticationManager();
ClientContext ctx = am.GetADFSUserNameMixedAuthenticatedContext(
siteUrl: “https://myprojects.mycompany.com”,
user: USERNAME,
password: PASSWORD,
domain: “DOMAIN”,
sts: “HOSTNAME OF ADFS”,
idpId: “sharepoint:myprojects:mycompany”);
[/code]
But the ProjectContext can only be created via the ProjectContext-constructor, the GetADFSUserNameMixedAuthenticatedContext cannot return a ProjectContext and the ClientContext cannot be casted to the ProjectContext. So how to authenticate against ADFS?
The solution is to use GetFedAuthCookie-method from the UsernameMixed()-class. It must be executed in the ExecutingWebRequest-event and applied the federation cookie to the cookie container of the web request:
[code language=”csharp”]
string projectUrl = “https://myprojects.mycompany.com”;
ProjectContext pc = new ProjectContext(projectUrl);
pc.ExecutingWebRequest += delegate (object oSender, WebRequestEventArgs webRequestEventArgs)
{
CookieContainer fedAuth = new OfficeDevPnP.Core.IdentityModel.TokenProviders.ADFS.UsernameMixed().GetFedAuthCookie(
projectUrl,
string.Format(“{0}\\{1}”, “DOMAIN”, USERNAME),
PASSWORD,
new Uri(string.Format(“https://{0}/adfs/services/trust/13/usernamemixed”, “HOSTNAME OF ADFS”)),
“sharepoint:myprojects:mycompany”,
10);
webRequestEventArgs.WebRequestExecutor.WebRequest.CookieContainer = fedAuth;
};
var projects = pc.Projects;
pc.Load(projects);
pc.ExecuteQuery();
[/code]
Leave a Reply
You must be logged in to post a comment.