问题
I have the MS Project Online account at sharepoint.com and I need to authenticate from client C# code to PSI services to get list of projects.
Server has Forms based authentication. I am trying to login via next code:
SvcLoginForms.LoginForms loginform = new SvcLoginForms.LoginForms();
loginform.Credentials = new NetworkCredential("admin@myserver.onmicrosoft.com", "password");
loginform.Url = "https://myserver.sharepoint.com/sites/pwa/_vti_bin/PSI/Project.asmx";
loginform.Login("admin@myserver.onmicrosoft.com", "password");
When I am executing loginform.Login I am getting SoapException with message: "Value cannot be null. Parameter name: account". Inner exception xml is:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Value cannot be null.
Parameter name: account
What I am doing wrong?
回答1:
Authentication to Project Online PSI services is described in this excellent article: http://www.umtsoftware.com/blog/how-to-project-online-psi/
回答2:
You can use:
new SharePointOnlineCredentials(username, secpassword);
instead of
new NetworkCredential("admin@myserver.onmicrosoft.com", "password");
First: Install required Client SDK
- SharePoint Client SDK : http://www.microsoft.com/en-au/download/details.aspx?id=35585
- Project 2013 SDK: http://www.microsoft.com/en-au/download/details.aspx?id=30435
Second: add the reference to your project
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll
- 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);
}
}
来源:https://stackoverflow.com/questions/32372670/how-to-authenticate-to-project-online-psi-services