In SharePoint, while creating projects we would also have to add team members for each projects. Team members can contribute source control and other team item activity.
Team member are of two types:
1.Enterprise team member
2. Non enterprise team member.
Here below is the sample code for adding both enterprise and non enterprise team members in projects.
PublishedProject publishedProject;
DraftProject draftProject = null;
QueueJob queueJobProjectResource = null;
Dictionary<Guid, Guid> uniqueTeamMembers = new Dictionary<Guid, Guid>();
List<Microsoft.ProjectServer.Client.EnterpriseResource> EnterpriseTeamMembers = new List<Microsoft.ProjectServer.Client.EnterpriseResource>();
List<Microsoft.ProjectServer.Client.ProjectResourceCreationInformation> NonEnterpriseTeamMembers = new List<Microsoft.ProjectServer.Client.ProjectResourceCreationInformation>();
Dictionary<Guid, Microsoft.ProjectServer.Client.EnterpriseResource> availableResources = AvailableResources();
try
{
publishedProject = availableProjects[new Guid(projectId)];
draftProject = publishedProject.CheckOut();
// Load/read all draft project resources
projectContext.Load(draftProject.ProjectResources);
projectContext.ExecuteQuery();
// Cached project resources
Dictionary<Guid, Microsoft.ProjectServer.Client.DraftProjectResource> availableDraftProjectResources = new Dictionary<Guid, Microsoft.ProjectServer.Client.DraftProjectResource>();
foreach (Microsoft.ProjectServer.Client.DraftProjectResource dProjectResource in draftProject.ProjectResources)
{
availableDraftProjectResources.Add(dProjectResource.Id, dProjectResource);
}
// Add Team Member
#region Adding Team Members to Project
//If not already on the team then add
Guid currentResourceGuid = new Guid("ProjectResourceId");
if (!uniqueTeamMembers.ContainsKey(currentResourceGuid))
{
uniqueTeamMembers.Add(currentResourceGuid, currentResourceGuid);
if (availableResources.ContainsKey(currentResourceGuid))
{
EnterpriseTeamMembers.Add(availableResources[currentResourceGuid]);
}
else
{
NonEnterpriseTeamMembers.Add(new Microsoft.ProjectServer.Client.ProjectResourceCreationInformation());
NonEnterpriseTeamMembers.Last().Id = new Guid();
NonEnterpriseTeamMembers.Last().Name =NonEnterpriseTeamMemberName;
}
}
foreach (Microsoft.ProjectServer.Client.EnterpriseResource teamMember in EnterpriseTeamMembers)
{
try
{
if (availableDraftProjectResources.ContainsKey(teamMember.Id))
{
// team member already exists ;
continue;
}
draftProject.ProjectResources.AddEnterpriseResource(teamMember);
}
catch (Exception ex)
{
//log exception
}
}
foreach (Microsoft.ProjectServer.Client.ProjectResourceCreationInformation teamMember in NonEnterpriseTeamMembers)
{
try
{
if (availableDraftProjectResources.ContainsKey(teamMember.Id))
{
// team member already exists ;
continue;
}
draftProject.ProjectResources.Add(teamMember);
}
catch (Exception ex)
{
//log exception
}
}
queueJobProjectResource = draftProject.Update();
Microsoft.ProjectServer.Client.QueueJob queueJobProjectResourcePublish = draftProject.Publish(true);
#endregion
}
catch (Exception ex)
{
//log exception
}
Dictionary<Guid, Microsoft.ProjectServer.Client.EnterpriseResource> AvailableResources()
{
// First load/read all projects
projectContext.Load(projectContext.EnterpriseResources);
projectContext.ExecuteQuery();
// Cached projects
Dictionary<Guid, Microsoft.ProjectServer.Client.EnterpriseResource> availableResources = new Dictionary<Guid, Microsoft.ProjectServer.Client.EnterpriseResource>();
foreach (Microsoft.ProjectServer.Client.EnterpriseResource pub in projectContext.EnterpriseResources)
{
availableResources.Add(pub.Id, pub);
}
return availableResources;
}
Hope this code will help you. Thanks
0 Comment(s)