Error 401 authenticating with Project Online and CSOM

2.2k views Asked by At

I get error 401 (or 403) when trying to connect to Project Online with CSOM in a console app. (This is not on-premise. It is Microsoft Project Online 2013.) Here is the code.

ProjectContext projContext = new ProjectContext(pwaPath);
projContext.Credentials = new NetworkCredential("myUserID", "mypwd", "xxx.onmicrosoft.com");
projContext.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(projContext_ExecutingWebRequest);
projContext.Load(projContext.Projects);
projContext.ExecuteQuery();
**// Error 401 Unauthorized**

static void projContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
    e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}

And another try, without ExecutingWebRequest:

ProjectContext projContext = new ProjectContext(pwaPath);
projContext.Credentials = new NetworkCredential("myUserID", "mypwd", "xxx.onmicrosoft.com");
projContext.Load(projContext.Projects);
projContext.ExecuteQuery();
**// Error 403 Forbidden**

Q1: Are there any problems with the code?

Q2: Is there a setting in Project Online that I'm missing?

1

There are 1 answers

1
Ammaroff On BEST ANSWER

You can use:

new SharePointOnlineCredentials(username, secpassword);

instead of

new NetworkCredential("[email protected]", "password");

First: Install required Client SDK

Second: add the reference to your project

  1. Microsoft.SharePoint.Client.dll
  2. Microsoft.SharePoint.Client.Runtime.dll
  3. Microsoft.ProjectServer.Client.dll

You can find the dlls in %programfiles%\Common Files\microsoft shared\Web Server Extensions\15\ISAPI and %programfiles(x86)%\Microsoft SDKs\Project 2013\REDIST

Here is sample code:

using System;
using System.Security;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;

public class Program
{
    private const string pwaPath = "https://[yoursitehere].sharepoint.com/sites/pwa";
    private const string username ="[username]";
    private const string password = "[password]";
    static void Main(string[] args)
    {
        SecureString secpassword = new SecureString();
        foreach (char c in password.ToCharArray()) secpassword.AppendChar(c);


        ProjectContext pc = new ProjectContext(pwaPath);
        pc.Credentials = new SharePointOnlineCredentials(username, secpassword);

        //now you can query
        pc.Load(pc.Projects);
        pc.ExecuteQuery();
        foreach(var p in pc.Projects)
        {
            Console.WriteLine(p.Name);
        }

        //Or Create a new project
        ProjectCreationInformation newProj = new ProjectCreationInformation() {
            Id = Guid.NewGuid(),
            Name = "[your project name]",
            Start = DateTime.Today.Date
        };        
        PublishedProject newPublishedProj = pc.Projects.Add(newProj);
        QueueJob qJob = pc.Projects.Update();

        JobState jobState = pc.WaitForQueue(qJob,/*timeout for wait*/ 10);

    }

}

I already answered this question in other question How to authenticate to Project Online PSI services?