Resource::DeactivateResources method
Office 2013 and later
Deactivates one or more enterprise resources.
Namespace: WebSvcResource
Assembly: ProjectServerServices (in ProjectServerServices.dll)
Parameters
- resourceGuids
- Type: []
Array of Guids of the enterprise resources to deactivate.
- validateOnly
- Type: System.Boolean
If true, only validates the data and does not deactivate the resources.
This example retrieves enterprise resources, creating several if they are not present. It then deativates these resources and reactivates them.
Please see Prerequisites for ASMX-based code samples in Project 2013 for critical information on running this code sample.
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Data; using System.Web.Services.Protocols; using System.Threading; using PSLibrary = Microsoft.Office.Project.Server.Library; namespace Microsoft.SDK.Project.Samples.DeactivateResources { class Program { [STAThread] static void Main() { try { const string PROJECT_SERVER_URI = "http://ServerName/ProjectServerName/"; const string RESOURCE_SERVICE_PATH = "_vti_bin/psi/resource.asmx"; SvcResource.ResourceDataSet resourceDs; PSLibrary.Filter resourceFilter; // Set up the Web service objects SvcResource.Resource resourceSvc = new SvcResource.Resource(); resourceSvc.Url = PROJECT_SERVER_URI + RESOURCE_SERVICE_PATH; resourceSvc.Credentials = CredentialCache.DefaultCredentials; // Get the resources Console.WriteLine("Getting/Creating resources"); Guid[] resources = EnsureEnterpriseResources(resourceSvc); // Deactivate resources. Console.WriteLine("Deactivating resources"); resourceSvc.DeactivateResources(resources, false); resourceFilter = GetResourceFilter(resources); resourceDs = resourceSvc.ReadResources(resourceFilter.GetXml(), false); WriteResourceState(resourceDs); // Activate Resources so we can use them later if we want Console.WriteLine("Activating resources"); resourceSvc.ActivateResources(resources, false); resourceDs = resourceSvc.ReadResources(resourceFilter.GetXml(), false); WriteResourceState(resourceDs); } catch (SoapException ex) { PSLibrary.PSClientError error = new PSLibrary.PSClientError(ex); PSLibrary.PSErrorInfo[] errors = error.GetAllErrors(); string errMess = "==============================\r\nError: \r\n"; for (int i = 0; i < errors.Length; i++) { errMess += "\n" + ex.Message.ToString() + "\r\n"; errMess += "".PadRight(30, '=') + "\r\nPSCLientError Output:\r\n \r\n"; errMess += errors[i].ErrId.ToString() + "\n"; for (int j = 0; j < errors[i].ErrorAttributes.Length; j++) { errMess += "\r\n\t" + errors[i].ErrorAttributeNames()[j] + ": " + errors[i].ErrorAttributes[j]; } errMess += "\r\n".PadRight(30, '='); } Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(errMess); } catch (WebException ex) { string errMess = ex.Message.ToString() + "\n\nLog on, or check the Project Server Queuing Service"; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error: " + errMess); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error: " + ex.Message); } finally { Console.ResetColor(); Console.WriteLine("\r\n\r\nPress any key..."); Console.ReadKey(); } } private static void WriteResourceState(SvcResource.ResourceDataSet resourceDs) { foreach (SvcResource.ResourceDataSet.ResourcesRow row in resourceDs.Resources.Rows) { // If the resource type is greater than the inactive offset, it is inactive. Console.ForegroundColor = ConsoleColor.Blue; Console.Write(row.RES_NAME); Console.ForegroundColor = ConsoleColor.Gray; Console.Write(" is "); if (row.RES_TYPE > (int)PSLibrary.Resource.Type.INACTIVATED_OFFSET) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("Inactive\r\n"); } else { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.Write("Active\r\n"); } Console.ResetColor(); } } private static Guid[] EnsureEnterpriseResources(SvcResource.Resource resourceSvc) { string[] resourceNames = new string[] { "Lertchai Treetawatchaiwong", "Bricks", "Conference Room A", "Rental"}; PSLibrary.Resource.Type[] resourceTypes = new PSLibrary.Resource.Type[] { PSLibrary.Resource.Type.WorkResource, PSLibrary.Resource.Type.MaterialResource, PSLibrary.Resource.Type.WorkResource, PSLibrary.Resource.Type.CostResources }; Guid[] resources = new Guid[resourceNames.Length]; for (int i = 0; i < resourceNames.Length; i++) { resources[i] = EnsureEnterpriseResource(resourceSvc, resourceNames[i], resourceTypes[i]); } return resources; } private static Guid EnsureEnterpriseResource(SvcResource.Resource resourceSvc, string resourceName, PSLibrary.Resource.Type resourceType) { SvcResource.ResourceDataSet resourceDs = new SvcResource.ResourceDataSet(); PSLibrary.Filter resourceFilter = new Microsoft.Office.Project.Server.Library.Filter(); resourceFilter.FilterTableName = resourceDs.Resources.TableName; resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName, resourceDs.Resources.RES_UIDColumn.ColumnName, PSLibrary.Filter.SortOrderTypeEnum.None)); resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName, resourceDs.Resources.RES_NAMEColumn.ColumnName, PSLibrary.Filter.SortOrderTypeEnum.None)); resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName, resourceDs.Resources.RES_INITIALSColumn.ColumnName, PSLibrary.Filter.SortOrderTypeEnum.None)); resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName, resourceDs.Resources.RES_TYPEColumn.ColumnName, PSLibrary.Filter.SortOrderTypeEnum.None)); PSLibrary.Filter.FieldOperator existingResource = new PSLibrary.Filter.FieldOperator(PSLibrary.Filter.FieldOperationType.Equal, resourceDs.Resources.RES_NAMEColumn.ColumnName, resourceName); resourceFilter.Criteria = existingResource; string filterXml = resourceFilter.GetXml(); resourceDs = resourceSvc.ReadResources(filterXml, false); if (resourceDs.Resources.Count >= 1) { return resourceDs.Resources[0].RES_UID; } else { resourceDs = new SvcResource.ResourceDataSet(); SvcResource.ResourceDataSet.ResourcesRow resourceRow = resourceDs.Resources.NewResourcesRow(); resourceRow.RES_UID = Guid.NewGuid(); resourceRow.RES_NAME = resourceName; resourceRow.RES_INITIALS = resourceName.Substring(0, 1) + (resourceName.IndexOf(" ") > 0 ? resourceName.Substring(resourceName.IndexOf(" ") + 1, 1) : ""); resourceRow.RES_TYPE = (int)resourceType; resourceDs.Resources.AddResourcesRow(resourceRow); resourceSvc.CreateResources(resourceDs, false, true); return resourceRow.RES_UID; } } private static PSLibrary.Filter GetResourceFilter(Guid[] resources) { SvcResource.ResourceDataSet resourceDs = new SvcResource.ResourceDataSet(); PSLibrary.Filter resourceFilter = new PSLibrary.Filter(); resourceFilter.FilterTableName = resourceDs.Resources.TableName; resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName, resourceDs.Resources.RES_UIDColumn.ColumnName, PSLibrary.Filter.SortOrderTypeEnum.None)); resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName, resourceDs.Resources.RES_NAMEColumn.ColumnName, PSLibrary.Filter.SortOrderTypeEnum.None)); resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName, resourceDs.Resources.RES_INITIALSColumn.ColumnName, PSLibrary.Filter.SortOrderTypeEnum.None)); resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName, resourceDs.Resources.RES_TYPEColumn.ColumnName, PSLibrary.Filter.SortOrderTypeEnum.None)); //List<PSLibrary.Filter.FieldOperator> resourceFieldOps = new List<PSLibrary.Filter.FieldOperator>(); PSLibrary.Filter.IOperator[] fos = new PSLibrary.Filter.IOperator[resources.Length]; for (int i = 0; i < resources.Length; i++) { fos[i] = new PSLibrary.Filter.FieldOperator(PSLibrary.Filter.FieldOperationType.Equal, resourceDs.Resources.RES_UIDColumn.ColumnName, resources[i]); } PSLibrary.Filter.LogicalOperator lo = new Microsoft.Office.Project.Server.Library.Filter.LogicalOperator(PSLibrary.Filter.LogicalOperationType.Or, fos); resourceFilter.Criteria = lo; return resourceFilter; } } }
Show: