Export (0) Print
Expand All

Resource.SetResourceAuthorization Method

Sets authorization values for one or more resources.

Namespace:  [Resource Web service]
Service reference: http://ServerName:32843/[Project Service Application GUID]/PSI/Resource.svc
Web service reference: http://ServerName/ProjectServerName/_vti_bin/PSI/Resource.asmx?wsdl

[SoapDocumentMethodAttribute("http://schemas.microsoft.com/office/project/server/webservices/Resource/SetResourceAuthorization", 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 void SetResourceAuthorization(
	ResourceAuthorizationDataSet users
)

Parameters

users
Type: [Resource Web service].ResourceAuthorizationDataSet
A ResourceDataSet containing the authorization data.

SetResourceAuthorization sets information about a resource such as the user logon account with the e-mail address.

Project Server Permissions

Permission

Description

ManageUsersAndGroups

Allows a user to manage resources, users and groups. Category permission.

This example updates the permssions of the current user and reads the permissions for the current user.

This sample creates a custom permission. Before you can run this sample twice, you need to remove the permission it creates. To manually delete resources, go to the Resource Center section of your Project Web App site, select your account from the select, and choose Edit Details from the Actions menu. Go to the Global Permissions section and clear the check box next to the New Project permission and click on Save.

Please see Prerequisites for ASMX-Based Code Samples for critical information on running this code sample.

using System;
using System.Collections.Generic;
using System.Text;
using PSLibrary=Microsoft.Office.Project.Server.Library;
using System.Net;
using System.Data;
using System.Web.Services.Protocols;
using System.Windows.Forms;

namespace Microsoft.SDK.Project.Samples.SetResourceAuthorization
{
   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";


            // Set up the Web service objects
            ResourceWebSvc.Resource resourceSvc = new ResourceWebSvc.Resource();

            resourceSvc.Url = PROJECT_SERVER_URI + RESOURCE_SERVICE_PATH;
            resourceSvc.Credentials = CredentialCache.DefaultCredentials;

            // Update your own permissions
            Guid resourceUid = resourceSvc.GetCurrentUserUid();
            // Read the current permissions
            ResourceWebSvc.ResourceAuthorizationDataSet authDS = resourceSvc.ReadResourceAuthorization(resourceUid);
            Console.WriteLine("Before: ");
            WriteTablesToConsole(authDS.Tables);

            // add the custom permission
            ResourceWebSvc.ResourceAuthorizationDataSet.GlobalPermissionsRow globalPermissionsRow = authDS.GlobalPermissions.NewGlobalPermissionsRow();
            globalPermissionsRow.RES_UID = resourceUid;
            globalPermissionsRow.WSEC_ALLOW = true;
            globalPermissionsRow.WSEC_DENY  = false;
            globalPermissionsRow.WSEC_FEA_ACT_UID = PSLibrary.PSSecurityGlobalPermission.NewProject;
            authDS.GlobalPermissions.AddGlobalPermissionsRow(globalPermissionsRow);

            resourceSvc.SetResourceAuthorization(authDS);
            // Read the permissions again to see the results
            authDS = resourceSvc.ReadResourceAuthorization(resourceUid);
            Console.WriteLine("After: ");
            WriteTablesToConsole(authDS.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();
         }
      }
      // Writes all table contents to the console in a
      // relatively readable way
      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);
      }
   }
}

Community Additions

ADD
Show:
© 2014 Microsoft