Resource.CreateResources 方法

创建一个或多个企业工时、 材料或成本资源。

命名空间:  WebSvcResource
程序集:  ProjectServerServices(位于 ProjectServerServices.dll 中)

语法

声明
<SoapDocumentMethodAttribute("https://schemas.microsoft.com/office/project/server/webservices/Resource/CreateResources", RequestNamespace := "https://schemas.microsoft.com/office/project/server/webservices/Resource/",  _
    ResponseNamespace := "https://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
用法
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("https://schemas.microsoft.com/office/project/server/webservices/Resource/CreateResources", RequestNamespace = "https://schemas.microsoft.com/office/project/server/webservices/Resource/", 
    ResponseNamespace = "https://schemas.microsoft.com/office/project/server/webservices/Resource/", 
    Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
public ResourceDataSet CreateResources(
    ResourceDataSet rds,
    bool validateOnly,
    bool autoCheckIn
)

参数

  • validateOnly
    类型:System.Boolean

    如果true,仅验证数据,并不会创建新资源。

  • autoCheckIn
    类型:System.Boolean

    如果true,自动签入的资源数据。

返回值

类型:WebSvcResource.ResourceDataSet

备注

如果您在 null ResourceDataSet作为Rds参数中传递, CreateResources不会引发异常,但不执行任何操作。

备注

要添加工作或成本资源可用性日期,指定的日期,不是时间。例如, DateTime dt = DateTime(2011, 06, 26)或DateTime dt = DateTime(2011, 06, 26, 0, 0, 0)适用,因为两个值都相同日期和时间。DateTime dt = DateTime(2011, 06, 26, 9, 0, 0)错误GeneralInvalidColumnValue (错误代码 20000) 的原因是 SOAP 异常当您尝试创建资源。

Project Server 权限

权限

说明

ManageUsersAndGroups

使用户能够管理所有企业用户、 资源和组。全局权限。

NewResource

允许用户创建新资源。全局权限。

示例

基于 WCF 的CreateResourceTest示例执行以下操作:

  1. 分析资源名称和百分比可用性命令的参数。

  2. 创建一个包含基本资源和可用性的数据的ResourceDataSet对象。

  3. 将初始ResourceDataSet内容的 XML 文件。

  4. 调用CreateResources方法中,并通过调用ReadResource方法,然后更新ResourceDataSet对象。

  5. 将更新的ResourceDataSet内容的 XML 文件。比较两个 XML 文件的设置的值显示内部创建 Project Server 的其他资源数据。

For information about using the code sample in a Microsoft Visual Studio 2010 project and creating an app.config file for configuration of the WCF endpoint, see Project 2013 中基于 WCF 的代码示例的先决条件.

using System;
using System.Text;
using System.ServiceModel;
using System.Xml;
using PSLibrary = Microsoft.Office.Project.Server.Library;

namespace Microsoft.SDK.Project.Samples.CreateResourceTest
{
    class Program
    {
        private const string ENDPOINT_RESOURCE = "basicHttp_Resource";
        // Change the output directory for your computer.
        private const string OUTPUT_FILES = @"C:\Project\Samples\Output\";
        private const string FILE_BEFORE = "ResourceDataSet_BeforeCreate.xml";
        private const string FILE_AFTER = "ResourceDataSet_AfterCreate.xml";

        private static SvcResource.ResourceClient resourceClient;

        private static string outFileResourceDs;
        private static string resourceName = string.Empty;
        private static double resAvailUnits = 0.0;

        static void Main(string[] args)
        {
            if (args.Length == 4)
            {
                if (args[0].ToLower() == "-name")
                {
                    resourceName = args[1];
                }
                if (args[2].ToLower() == "-units")
                {
                    // Units available, in percent.
                    resAvailUnits = Convert.ToDouble(args[3]);
                }
            }
            else
            {
                Console.WriteLine(@"Usage: CreateResourceTest -name ""Resource Name"" -units 100.0");
                Console.Write("\nPress any key to exit... ");
                Console.ReadKey(true);
                Environment.Exit(-1);
            }

            try
            {
                ConfigClientEndpoints(ENDPOINT_RESOURCE);
                Console.WriteLine("Creating ResourceDataSet...");

                SvcResource.ResourceDataSet resourceDs = new SvcResource.ResourceDataSet();
                resourceDs = PopulateData(resourceDs, resourceName.Trim(), resAvailUnits);

                outFileResourceDs = OUTPUT_FILES + FILE_BEFORE;
                Console.WriteLine(
                    "\nXML output of the ResourceDataSet before CreateResource:\n\t{0}", outFileResourceDs);
                resourceDs.WriteXml(outFileResourceDs);

                Console.WriteLine("Calling CreateResources...");
                resourceClient.CreateResources(resourceDs, false, true);

                resourceDs = resourceClient.ReadResource(resourceDs.Resources[0].RES_UID);

                outFileResourceDs = OUTPUT_FILES + FILE_AFTER;
                Console.WriteLine(
                    "\nXML output of the ResourceDataSet after CreateResource:\n\t{0}", outFileResourceDs);
                resourceDs.WriteXml(outFileResourceDs);
            }
            catch (FaultException fault)
            {
                // Use the WCF FaultException, because the ASMX SoapException does not 
                // exist in a WCF-based application.
                WriteFaultOutput(fault);
            }
            catch (EndpointNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("\nInnerException: \n" + ex.InnerException.Message);
            }
            catch (ServerTooBusyException ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("\nInnerException: \n" + ex.InnerException.Message);
                Console.WriteLine("\nCheck that PWA is running. Can you log on?"
                    + "\nIs the endpoint address correct in app.config?");
            }
            Console.Write("\nPress any key to exit... ");
            Console.ReadKey(true);
        }

        
        // Populate the minimum data necessary in the ResourceDataSet, for the Resource 
        // table and for the ResourceAvailabilities table.
        private static SvcResource.ResourceDataSet PopulateData(
            SvcResource.ResourceDataSet resDs, string resName, double unitsAvail)
        {
            SvcResource.ResourceDataSet.ResourcesRow resRow = resDs.Resources.NewResourcesRow();
            SvcResource.ResourceDataSet.ResourceAvailabilitiesRow resAvailRow = 
                resDs.ResourceAvailabilities.NewResourceAvailabilitiesRow();

            resRow.RES_NAME = resName;
            resRow.RES_UID = Guid.NewGuid();
            resRow.RES_TYPE = (int)PSLibrary.Resource.Type.WorkResource;
            resRow.RES_INITIALS = resName.Substring(0, 1) 
                + (resName.IndexOf(" ") > 0
                ? resName.Substring(resName.IndexOf(" ") + 1, 1)
                : string.Empty);
            resDs.Resources.AddResourcesRow(resRow);

            resAvailRow.RES_UID = resRow.RES_UID;
            resAvailRow.SetRES_AVAIL_FROMNull();
            resAvailRow.SetRES_AVAIL_TONull();
            resAvailRow.RES_AVAIL_UNITS = unitsAvail;
            resDs.ResourceAvailabilities.AddResourceAvailabilitiesRow(resAvailRow);
            return resDs;
        }


        // Extract a PSClientError object from the WCF FaultException object, and
        // then display the exception details and each error in the PSClientError stack.
        private static void WriteFaultOutput(FaultException fault)
        {
            string errAttributeName;
            string errAttribute;
            string errOut;
            string errMess = "".PadRight(30, '=') + "\r\n"
                + "Error details: " + "\r\n";

            PSLibrary.PSClientError error = Helpers.GetPSClientError(fault, out errOut);
            errMess += errOut;

            if (error != null)
            {
                PSLibrary.PSErrorInfo[] errors = error.GetAllErrors();
                PSLibrary.PSErrorInfo thisError;

                for (int i = 0; i < errors.Length; i++)
                {
                    thisError = errors[i];
                    errMess += "\r\n".PadRight(30, '=') + "\r\nPSClientError output:\r\n";
                    errMess += thisError.ErrId.ToString() + "\n";

                    for (int j = 0; j < thisError.ErrorAttributes.Length; j++)
                    {
                        errAttributeName = thisError.ErrorAttributeNames()[j];
                        errAttribute = thisError.ErrorAttributes[j];
                        errMess += "\r\n\t" + errAttributeName
                            + ": " + errAttribute;
                    }
                }
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(errMess);
            Console.ResetColor();
        }

        // Use the endpoints defined in app.config to configure the client.
        public static void ConfigClientEndpoints(string endpt)
        {
            if (endpt == ENDPOINT_RESOURCE)
                resourceClient = new SvcResource.ResourceClient(endpt);
        }
    }

    // Helper methods: WaitForQueue and GetPSClientError.
    class Helpers
    {
        // Wait for the queue jobs to complete.
        public static bool WaitForQueue(SvcQueueSystem.QueueMsgType jobType,
            SvcQueueSystem.QueueSystemClient queueSystemClient,
            DateTime startTime)
        {
            const int MAX_WAIT = 50;    // Maximum wait time.
            int numJobs = 1;            // Number of jobs in the queue.
            bool completed = false;     // Queue job completed.
            SvcQueueSystem.QueueStatusDataSet queueStatusDs =
                new SvcQueueSystem.QueueStatusDataSet();

            int timeout = 0;            // Number of seconds waited.
            Console.Write("Waiting for job: {0} ", jobType.ToString());

            SvcQueueSystem.QueueMsgType[] messageTypes = { jobType };
            SvcQueueSystem.JobState[] jobStates = { SvcQueueSystem.JobState.Success };

            while (timeout < MAX_WAIT)
            {
                System.Threading.Thread.Sleep(1000);    // Sleep one second.

                queueStatusDs = queueSystemClient.ReadMyJobStatus(
                    messageTypes,
                    jobStates,
                    startTime,
                    DateTime.Now,
                    numJobs,
                    true,
                    SvcQueueSystem.SortColumn.QueuePosition,
                    SvcQueueSystem.SortOrder.LastOrder);

                timeout++;
                Console.Write(".");
            }
            Console.WriteLine();

            if (queueStatusDs.Status.Count == numJobs)
                completed = true;
            return completed;
        }

        /// <summary>
        /// Extract a PSClientError object from the ServiceModel.FaultException,
        /// for use in output of the GetPSClientError stack of errors.
        /// </summary>
        /// <param name="e"></param>
        /// <param name="errOut">Shows that FaultException has more information 
        /// about the errors than PSClientError has. FaultException can also contain 
        /// other types of errors, such as failure to connect to the server.</param>
        /// <returns>PSClientError object, for enumerating errors.</returns>
        public static PSLibrary.PSClientError GetPSClientError(FaultException e,
                                                               out string errOut)
        {
            const string PREFIX = "GetPSClientError() returns null: ";
            errOut = string.Empty;
            PSLibrary.PSClientError psClientError = null;

            if (e == null)
            {
                errOut = PREFIX + "Null parameter (FaultException e) passed in.";
                psClientError = null;
            }
            else
            {
                // Get a ServiceModel.MessageFault object.
                var messageFault = e.CreateMessageFault();

                if (messageFault.HasDetail)
                {
                    using (var xmlReader = messageFault.GetReaderAtDetailContents())
                    {
                        var xml = new XmlDocument();
                        xml.Load(xmlReader);

                        var serverExecutionFault = xml["ServerExecutionFault"];
                        if (serverExecutionFault != null)
                        {
                            var exceptionDetails = serverExecutionFault["ExceptionDetails"];
                            if (exceptionDetails != null)
                            {
                                try
                                {
                                    errOut = exceptionDetails.InnerXml + "\r\n";
                                    psClientError =
                                        new PSLibrary.PSClientError(exceptionDetails.InnerXml);
                                }
                                catch (InvalidOperationException ex)
                                {
                                    errOut = PREFIX + "Unable to convert fault exception info ";
                                    errOut += "a valid Project Server error message. Message: \n\t";
                                    errOut += ex.Message;
                                    psClientError = null;
                                }
                            }
                            else
                            {
                                errOut = PREFIX + "The FaultException e is a ServerExecutionFault, "
                                    + "but does not have ExceptionDetails.";
                            }
                        }
                        else
                        {
                            errOut = PREFIX + "The FaultException e is not a ServerExecutionFault.";
                        }
                    }
                }
                else // No detail in the MessageFault.
                {
                    errOut = PREFIX + "The FaultException e does not have any detail.";
                }
            }
            errOut += "\r\n" + e.ToString() + "\r\n";
            return psClientError;
        }
    }
}

当您运行命令CreateResourceTest –name "Test Res1" –units 30时,应用程序将写入 ResourceDataSet_BeforeCreate.xml 文件与以下内容:

<?xml version="1.0" standalone="yes"?>
<ResourceDataSet xmlns="https://schemas.microsoft.com/office/project/server/webservices/ResourceDataSet/">
  <Resources>
    <RES_UID>cf45c439-5320-438b-bc30-22a564f64fca</RES_UID>
    <RES_ID>-1</RES_ID>
    <RES_TYPE>2</RES_TYPE>
    <RES_HAS_NOTES>false</RES_HAS_NOTES>
    <RES_CAN_LEVEL>true</RES_CAN_LEVEL>
    <RES_NAME>Test Res1</RES_NAME>
    <RES_INITIALS>TR</RES_INITIALS>
    <RES_IS_TEAM>false</RES_IS_TEAM>
    <RES_EXCHANGE_SYNC>false</RES_EXCHANGE_SYNC>
  </Resources>
  <ResourceAvailabilities>
    <RES_UID>cf45c439-5320-438b-bc30-22a564f64fca</RES_UID>
    <RES_AVAIL_UNITS>30</RES_AVAIL_UNITS>
  </ResourceAvailabilities>
</ResourceDataSet>

在创建资源,并使用ReadResource更新ResourceDataSet之后, ResourceDataSet_AfterCreate.xml 文件包含以下项目:

<?xml version="1.0" standalone="yes"?>
<ResourceDataSet xmlns="https://schemas.microsoft.com/office/project/server/webservices/ResourceDataSet/">
  <Resources>
    <RES_UID>6004cee6-e74a-4389-a5e0-d6925a79e5b8</RES_UID>
    <RES_ID>13</RES_ID>
    <RES_TYPE>2</RES_TYPE>
    <RES_HAS_NOTES>false</RES_HAS_NOTES>
    <RES_CAN_LEVEL>true</RES_CAN_LEVEL>
    <RES_ACCRUE_AT>3</RES_ACCRUE_AT>
    <RES_BOOKING_TYPE>0</RES_BOOKING_TYPE>
    <RES_NAME>Test Res47</RES_NAME>
    <RES_INITIALS>TR</RES_INITIALS>
    <RES_IS_WINDOWS_USER>false</RES_IS_WINDOWS_USER>
    <RES_TIMESHEET_MGR_UID>6004cee6-e74a-4389-a5e0-d6925a79e5b8</RES_TIMESHEET_MGR_UID>
    <RES_DEF_ASSN_OWNER>6004cee6-e74a-4389-a5e0-d6925a79e5b8</RES_DEF_ASSN_OWNER>
    <RES_IS_TEAM>false</RES_IS_TEAM>
    <RES_EXCHANGE_SYNC>false</RES_EXCHANGE_SYNC>
    <CREATED_DATE>2010-12-08T08:12:50.377-08:00</CREATED_DATE>
    <MOD_DATE>2010-12-08T08:12:50.38-08:00</MOD_DATE>
    <RES_STD_RATE_FMT>2</RES_STD_RATE_FMT>
    <RES_OVT_RATE_FMT>2</RES_OVT_RATE_FMT>
    <RES_MAX_UNITS>3000.000000</RES_MAX_UNITS>
    <RES_PREVENT_ADSYNC>false</RES_PREVENT_ADSYNC>
    <BaseCalendarUniqueId>b6635b2e-e747-4771-a78b-24f7509629d0</BaseCalendarUniqueId>
  </Resources>
  <ResourceAvailabilities>
    <RES_UID>6004cee6-e74a-4389-a5e0-d6925a79e5b8</RES_UID>
    <RES_AVAIL_UNITS>30</RES_AVAIL_UNITS>
  </ResourceAvailabilities>
</ResourceDataSet>

比较两个文件显示,例如,Project Server 内部RES_AVAIL_UNITS属性值设置一个值,对应的RES_MAX_UNITS属性。

下面的示例创建两个工作资源、 材料资源和对成本资源。它不会检查前现有资源,并报告错误,如果已存在的资源。此示例还添加ResourceAvailabilitiesDataTable工时和成本资源,并指定RES_AVAIL_FROM和RES_AVAIL_UNITS值。

您可以使用在DeleteResources示例删除资源,以或手动删除资源,请转到您Project Web App服务器设置,然后选择删除企业对象(例如,https://ServerName/pwa/_layouts/pwa/admin/admin.aspx)。

Please see Project 2013 中基于 ASMX 的代码示例的先决条件 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";

            SvcResource.ResourceDataSet resourceDs;

            // Set up the Web service objects
            SvcResource.Resource resourceSvc = new SvcResource.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 SvcResource.ResourceDataSet();
            // NOTE: Resource names must be unique. Open Server Settings - Delete Enterprise Objects   
            //       in Project Web App 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++)
            {
               SvcResource.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(2011, 06, 26) or DateTime(2011, 06, 26, 0, 0, 0) is OK.
               // DateTime(2011, 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.
               {
                  SvcResource.ResourceDataSet.ResourceAvailabilitiesRow availRow =
                     resourceDs.ResourceAvailabilities.NewResourceAvailabilitiesRow();
                  availRow.RES_UID = resourceRow.RES_UID;

                  DateTime dt = new DateTime(2011, 06, 26);
                  dt = DateTime.SpecifyKind(dt, DateTimeKind.Local);  // Optional.
                  availRow.RES_AVAIL_FROM = dt;
                  availRow.RES_AVAIL_TO = new DateTime(2011, 7, 1);
                  availRow.RES_AVAIL_UNITS = 100.0;

                  SvcResource.ResourceDataSet.ResourcesRow resRow = 
                     (SvcResource.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(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 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));
         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);
      }
   }
}

另请参阅

引用

Resource 类

Resource 成员

WebSvcResource 命名空间