2

コンソール アプリで CSOM を使用して Project Online に接続しようとすると、エラー 401 (または 403) が発生します。(これはオンプレミスではありません。Microsoft Project Online 2013 です。) コードは次のとおりです。

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");
}

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: コードに問題はありますか?

Q2: Project Online に不足している設定はありますか?

4

1 に答える 1

3

以下を使用できます。

new SharePointOnlineCredentials(username, secpassword);

それ以外の

new NetworkCredential("admin@myserver.onmicrosoft.com", "password");

最初: 必要なクライアント SDK をインストールする

2番目:プロジェクトへの参照を追加します

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

dll は次の場所に%programfiles%\Common Files\microsoft shared\Web Server Extensions\15\ISAPI あります。%programfiles(x86)%\Microsoft SDKs\Project 2013\REDIST

サンプルコードは次のとおりです。

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);

    }

}

この質問には、別の質問で既に回答してい ます。Project Online PSI サービスの認証方法を教えてください。

于 2015-09-07T05:12:24.400 に答える