Project Web App - get Project ID from project related site via REST
-
31-12-2020 - |
题
This is in SP2016 / Project Server 2016 on-prem. (Also, full disclosure, my first experience with Project Server / PWAs.)
Let's say I have a PWA set up at the URL http://servername/pwa/MyProjects
. I set up the "Connected SharePoint Sites" to automatically create a project site when a project is published to the PWA. Then I publish a project called "Project1".
Now I have a SharePoint site at the URL http://servername/pwa/MyProjects/Project1
.
Is there a way to get the Project ID of the published project from the related SharePoint site, via SharePoint REST API?
Meaning, I want to use
http://servername/pwa/MyProjects/Project1/_api/web/Some-endpoint-I-don't-know
in order to get the ID of the related project so that I can then use
http://servername/pwa/MyProjects/_api/ProjectServer/projects('use-the-ID-here')
to get access to the project through the Project Server REST API.
I know it is there somewhere because on the QuickLaunch of the SharePoint site, there is a link to "Project Details" that has a URL of
http://servername/pwa/MyProjects/ProjectDrilldown.aspx?ProjUid=GUID-is-here&ret=1
Is there a way I can get the GUID without resorting to scraping the page?
解决方案
This applies for SharePoint Online/Project Online, however assuming it's the same for on-prem.
The Project GUID is stored as a site property called MSPWAPROJUID for the Project Site.
This is accessible using the /_api/Web/AllProperties end point.
ie. http://servername/pwa/MyProjects/Project1/_api/Web/AllProperties
Using JS
var ProjectUID;
ExecuteOrDelayUntilScriptLoaded(getProjectUIDProperty, "sp.js");
function getProjectUIDProperty() {
var ctx = new SP.ClientContext.get_current();
this.web = ctx.get_web();
this.props = this.web.get_allProperties();
ctx.load(this.web);
ctx.load(this.props);
ctx.executeQueryAsync(Function.createDelegate(this, gotProperty), Function.createDelegate(this, failedGettingProperty));
}
function gotProperty() {
ProjectUID = this.props.get_item('MSPWAPROJUID');
}
function failedGettingProperty() {
alert('Error: ' + args.get_message());
}
Using SPD
Set Variable:ps to [%Workflow Context:Current Site URL%]_api/Web/AllProperties/
Dictionary:
Accept: application/json; odata=nometadata
Content-Type: application/json; odata=nometadata