Read current Project data using JSOM

Hello Readers,
In this article, I’m going to discuss about reading current Project Data using JSOM (JavaScript Object Model) with a simple code.
First create a .js file with the below code then upload it to the document library on the PWA tenant. Add it to a content editor web part of the schedule page.
First create a .js file with the below code then upload it to the document library on the PWA tenant. Add it to a content editor web part of the schedule page.
Add the below links to make use of the existing SharePoint and Project online APIs.
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script> <script type="text/javascript" src ="/_layouts/15/sp.runtime.js"></script> <script type="text/javascript" src ="/_layouts/15/sp.js"></script> <script type="text/javascript" src="/_layouts/15/ps.js"></script></pre> <script type="text/javascript"> var projectContext; var custom_Fields; var Allprojects; var projectID ; // Read the ProjectUId of the current opened project from the Querystring. function GetParameterValues(param) { var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for (var i = 0; i < url.length; i++) { var urlparam = url[i].split('='); if (urlparam[0] == param) { return urlparam[1]; } } } jQuery(document).ready(function () { SP.SOD.executeOrDelayUntilScriptLoaded(ReadProjectData, "PS.js"); }); //Read project data from the Project context object function ReadProjectData() { var projectContext = PS.ProjectContext.get_current(); var allProjects=projectContext.get_projects(); var project= allProjects.getById(projectID); projectContext.load(project); projectContext.executeQueryAsync(ReadProject, errorHandler); function ReadProject() { alert(project.get_title()); // returns Project name alert(project.get_id()); alert(project.get_owner().get_title()); //Project owner } function errorHandler(sender, args) { if (args instanceof SP.ClientRequestFailedEventArgs) { alert(args.get_errorTraceCorrelationId()); alert( args.get_message()); alert(args.get_errorDetails()); } } </script>
Recent Comments