从Project Online获取ProjectSiteUrl失败,请求技术支持
Let's break down why your code might be failing to fetch the ProjectSiteUrl property and fix it step by step:
1. You're not explicitly loading the ProjectSiteUrl property
CSOM (Client Side Object Model) doesn't load all project properties by default—you have to explicitly specify which properties you want to retrieve. If you skip this step, accessing ProjectSiteUrl will either return null or throw an error.
2. Authentication might not be properly configured
Your code sets up a SecureString for the password, but make sure you're assigning the credentials to the ProjectContext so it can authenticate against Project Online. Without this, the context won't have permission to access project data.
3. Projects might not have an associated Project Site
Some published projects might not have a project site created yet, so ProjectSiteUrl will be null for those cases—you'll need to handle that gracefully to avoid null reference exceptions.
Corrected Code Example
Here's how to adjust your code to address these issues:
using (ProjectContext projContext = new ProjectContext(pwaUrl)) { // Configure authentication using (SecureString securePassword = new SecureString()) { Helper.GetSecurePassword(projContext, securePassword, emailID, password); // Assign credentials to the context (critical for accessing Project Online) projContext.Credentials = new SharePointOnlineCredentials(emailID, securePassword); foreach (var listItem in listGuid) { string spoGuid = listItem.ProjectGuid; if (!string.IsNullOrEmpty(spoGuid) && Guid.TryParse(spoGuid, out Guid projectId)) { try { // Retrieve the project by ID var project = projContext.Projects.GetById(projectId); // Explicitly load only the ProjectSiteUrl property (optimizes performance) projContext.Load(project, p => p.ProjectSiteUrl); // Execute the query to fetch the data from Project Online projContext.ExecuteQuery(); // Access the property and handle null cases if (project.ProjectSiteUrl != null) { Console.WriteLine($"Project Site URL: {project.ProjectSiteUrl.AbsoluteUri}"); } else { Console.WriteLine($"Project {projectId} has no associated Project Site."); } } catch (Exception ex) { Console.WriteLine($"Error retrieving project {projectId}: {ex.Message}"); // For more details, check ex.InnerException to get root cause } } } } }
Additional Debugging Steps
- Check for permission errors: If you get a 403 Forbidden error, ensure the user account has access to the published projects and their associated sites in Project Online.
- Verify project GUIDs: Double-check that the
ProjectGuidvalues inlistGuidare valid and correspond to published projects (draft projects won't return a valid ProjectSiteUrl). - Inspect exceptions: Wrap
ExecuteQuery()in a try-catch block to capture detailed error messages—this will help pinpoint issues like invalid project IDs or authentication failures.
内容的提问来源于stack exchange,提问作者Abdul Hameed

