Resource.CreateResources Method
Creates one or more enterprise work, material, or cost resources.
Namespace: WebSvcResource
Assembly: ProjectServerWebServices (in ProjectServerWebServices.dll)
Syntax
'Declaration
<SoapDocumentMethodAttribute("http://schemas.microsoft.com/office/project/server/webservices/Resource/CreateResources", RequestNamespace := "http://schemas.microsoft.com/office/project/server/webservices/Resource/", _
ResponseNamespace := "http://schemas.microsoft.com/office/project/server/webservices/Resource/", _
Use := SoapBindingUse.Literal, ParameterStyle := SoapParameterStyle.Wrapped)> _
Public Function CreateResources ( _
rds As ResourceDataSet, _
validateOnly As Boolean, _
autoCheckIn As Boolean _
) As ResourceDataSet
'Usage
Dim instance As Resource
Dim rds As ResourceDataSet
Dim validateOnly As Boolean
Dim autoCheckIn As Boolean
Dim returnValue As ResourceDataSet
returnValue = instance.CreateResources(rds, _
validateOnly, autoCheckIn)
[SoapDocumentMethodAttribute("http://schemas.microsoft.com/office/project/server/webservices/Resource/CreateResources", RequestNamespace = "http://schemas.microsoft.com/office/project/server/webservices/Resource/",
ResponseNamespace = "http://schemas.microsoft.com/office/project/server/webservices/Resource/",
Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
public ResourceDataSet CreateResources(
ResourceDataSet rds,
bool validateOnly,
bool autoCheckIn
)
Parameters
- rds
Type: WebSvcResource.ResourceDataSet
The ResourceDataSet containing the data for the new resources.
- validateOnly
Type: System.Boolean
If true, only validates the data and does not create the new resources.
- autoCheckIn
Type: System.Boolean
If true, automatically checks in the resource data.
Return Value
Type: WebSvcResource.ResourceDataSet
Remarks
If you pass in a null ResourceDataSet as the Rds parameter, CreateResources doesn't throw an exception, but takes no action.
Note
To add a work or cost resource availability date, specify a date only, not a time. For example, DateTime dt = DateTime(2007, 06, 26) or DateTime dt = DateTime(2007, 06, 26, 0, 0, 0) works because either value is the same date and time. DateTime dt = DateTime(2007, 06, 26, 9, 0, 0) causes a SOAP exception with the error GeneralInvalidColumnValue (error code 20000) when you try to create the resource.
Project Server Permissions
Permission |
Description |
---|---|
Manage all enterprise users, resources and groups. Global permission. |
|
Create a new resource. Global permission. |
Examples
The following example creates two work resources, a material resource, and a cost resource. It does not check for pre-existing resources and reports an error if a resource already exists. The example also adds a ResourceAvailabilitiesDataTable for the work and cost resources and specifies the RES_AVAIL_FROM and RES_AVAIL_UNITS values.
You can use the example in DeleteResources to delete the resources, or to manually delete resources, go to the Server Settings of your Project Web Access site and choose Delete Enterprise Objects (for example, http://ServerName/pwa/_layouts/pwa/admin/admin.aspx).
Please see Prerequisites for Reference Code Samples 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.CreateResources
{
class Program
{
[STAThread]
static void Main()
{
try
{
const string PROJECT_SERVER_URI = "http://DevDocPj12/PWA/";
const string RESOURCE_SERVICE_PATH = "_vti_bin/psi/resource.asmx";
ResourceWebSvc.ResourceDataSet resourceDs;
// Set up the Web service objects
ResourceWebSvc.Resource resourceSvc = new ResourceWebSvc.Resource();
resourceSvc.Url = PROJECT_SERVER_URI + RESOURCE_SERVICE_PATH;
resourceSvc.Credentials = CredentialCache.DefaultCredentials;
// Set up the resources
Console.WriteLine("Set up the resources");
resourceDs = new ResourceWebSvc.ResourceDataSet();
// NOTE: Resource names must be unique. Open Server Settings - Delete Enterprise Objects
// in Project Web Access to delete resources, if you have previously run
// the resource code sample.
string[] resourceNames = new string[] { "Charlotte Weiss",
"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++)
{
ResourceWebSvc.ResourceDataSet.ResourcesRow resourceRow =
resourceDs.Resources.NewResourcesRow();
resourceRow.RES_UID = Guid.NewGuid();
resources[i] = resourceRow.RES_UID;
resourceRow.RES_NAME = resourceNames[i];
resourceRow.RES_INITIALS = resourceNames[i].Substring(0, 1) +
(resourceNames[i].IndexOf(" ") > 0
? resourceNames[i].Substring(resourceNames[i].IndexOf(" ") + 1, 1)
: "");
resourceRow.RES_TYPE = (int)resourceTypes[i];
resourceDs.Resources.AddResourcesRow(resourceRow);
// To add work or cost resource availability, specify a date only, not a time.
// DateTime(2007, 06, 26) or DateTime(2007, 06, 26, 0, 0, 0) is OK.
// DateTime(2007, 06, 26, 9, 0, 0) causes a Soap exception.
// The default availability is the current date, and 100 RES_AVAIL_UNITS.
if (i != 1) // Can't add availability for material resources.
{
ResourceWebSvc.ResourceDataSet.ResourceAvailabilitiesRow availRow =
resourceDs.ResourceAvailabilities.NewResourceAvailabilitiesRow();
availRow.RES_UID = resourceRow.RES_UID;
DateTime dt = new DateTime(2007, 06, 26);
dt = DateTime.SpecifyKind(dt, DateTimeKind.Local); // Optional.
availRow.RES_AVAIL_FROM = dt;
availRow.RES_AVAIL_TO = new DateTime(2007, 7, 1);
availRow.RES_AVAIL_UNITS = 100.0;
ResourceWebSvc.ResourceDataSet.ResourcesRow resRow =
(ResourceWebSvc.ResourceDataSet.ResourcesRow)resourceDs.Resources.Rows[i];
availRow.ResourcesRow = resRow;
resourceDs.ResourceAvailabilities.AddResourceAvailabilitiesRow(availRow);
}
}
Console.WriteLine("Save the data to the server");
resourceSvc.CreateResources(resourceDs, false, true);
// retrieve the data from the server for display
Console.WriteLine("Retrieve the data just created and display");
PSLibrary.Filter resourceFilter = GetResourceFilter(resources);
string filterXml = resourceFilter.GetXml();
resourceDs = resourceSvc.ReadResources(filterXml, false);
WriteTablesToConsole(resourceDs.Tables);
}
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(ResourceWebSvc.ResourceDataSet resourceDs)
{
foreach (ResourceWebSvc.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 PSLibrary.Filter GetResourceFilter(Guid[] resources)
{
ResourceWebSvc.ResourceDataSet resourceDs = new ResourceWebSvc.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));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_CODEColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_ACCRUE_ATColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_BOOKING_TYPEColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_CAN_LEVELColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_CHECKOUTBYColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_CHECKOUTDATEColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_COST_CENTERColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_DEF_ASSN_OWNERColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_EXTERNAL_IDColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_GROUPColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_HAS_NOTESColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_HIRE_DATEColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_HYPERLINK_ADDRESSColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_HYPERLINK_FRIENDLY_NAMEColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_HYPERLINK_SUB_ADDRESSColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_IDColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_IS_TEAMColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_IS_WINDOWS_USERColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_MATERIAL_LABELColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_PHONETICSColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_RTF_NOTESColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_TERMINATION_DATEColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
resourceFilter.Fields.Add(new PSLibrary.Filter.Field(resourceDs.Resources.TableName,
resourceDs.Resources.RES_TIMESHEET_MGR_UIDColumn.ColumnName,
PSLibrary.Filter.SortOrderTypeEnum.None));
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;
}
// Write all contents of a table collection to the console
private static void WriteTablesToConsole(System.Data.DataTableCollection theTables)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
foreach (System.Data.DataTable table in theTables)
{
int[] columnWidths = new int[table.Columns.Count];
int tableWidth = 0;
string dataString;
Console.WriteLine("Table: " + table.TableName);
// Write out the column names and get their spacing
StringBuilder tableRow = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
columnWidths[i] = GetColumnWidth(table.Columns[i]);
tableRow.Append(table.Columns[i].ColumnName.PadRight(columnWidths[i]));
tableWidth += columnWidths[i];
}
// add a space so it won't wrap
tableWidth += 1;
// make the console as wide as the widest table
Console.BufferWidth = (Console.BufferWidth > tableWidth
? Console.BufferWidth
: tableWidth);
tableRow.Append("\r\n");
Console.Write(tableRow.ToString());
// Write out the data
foreach (DataRow row in table.Rows)
{
tableRow = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
dataString = row[i].ToString();
// truncate output if it is wider than
// the desired column width
if (dataString.Length >= columnWidths[i])
{
dataString = dataString.Substring(0, columnWidths[i] - 1);
}
// add the output to the stringbuilder and pad right to fill
// up to the column width.
tableRow.Append(dataString.PadRight(columnWidths[i]));
}
tableRow.Append("\r\n");
Console.Write(tableRow.ToString());
}
Console.Write("\r\n".PadLeft(tableWidth, '-'));
}
Console.ResetColor();
}
// Helper function for WriteTablesToConsole
private static int GetColumnWidth(DataColumn column)
{
// Note: may not handle byte[]data types well
const int MAX_COL_WIDTH = 40;
int dataWidth = 0;
//return 12 for numbers, 30 for dates, and string width for strings.
switch (column.DataType.UnderlyingSystemType.ToString())
{
case "System.Boolean":
case "System.Byte":
case "System.Byte[]":
case "System.Char":
case "System.Decimal":
case "System.Double":
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.SByte":
case "System.Single":
case "System.UInt16":
case "System.UInt32":
case "System.UInt64":
dataWidth = 12;
break;
case "System.DateTime":
case "System.TimeSpan":
dataWidth = 30;
break;
case "System.Guid":
dataWidth = 37;
break;
case "System.String":
// If it has a maxlength, use it
if (column.MaxLength > 0)
{
dataWidth = column.MaxLength;
}
else
{
// Otherwise use the max col width
dataWidth = MAX_COL_WIDTH;
}
break;
default:
dataWidth = column.ColumnName.Length;
break;
}
// truncate if over the max length
if (dataWidth > MAX_COL_WIDTH)
{
dataWidth = MAX_COL_WIDTH;
}
// always be at least as wide as the colum name
return (column.ColumnName.Length > (dataWidth)
? column.ColumnName.Length + 1
: dataWidth);
}
}
}