ObjectLinkProvider.CreateWebObjectLinks method
Cria vínculos entre objetos da web, com base nas definições de objeto e o link.
Namespace: WebSvcObjectLinkProvider
Assembly: ProjectServerServices (in ProjectServerServices.dll)
Syntax
'Declaração
<SoapDocumentMethodAttribute("https://schemas.microsoft.com/office/project/server/webservices/ObjectLinkProvider/CreateWebObjectLinks", RequestNamespace := "https://schemas.microsoft.com/office/project/server/webservices/ObjectLinkProvider/", _
ResponseNamespace := "https://schemas.microsoft.com/office/project/server/webservices/ObjectLinkProvider/", _
Use := SoapBindingUse.Literal, ParameterStyle := SoapParameterStyle.Wrapped)> _
Public Function CreateWebObjectLinks ( _
primaryDataSet As ObjectLinkProviderDataSet, _
linkDataSet As ObjectLinkProviderDataSet, _
linkTypes As WebObjectLinkType() _
) As Integer
'Uso
Dim instance As ObjectLinkProvider
Dim primaryDataSet As ObjectLinkProviderDataSet
Dim linkDataSet As ObjectLinkProviderDataSet
Dim linkTypes As WebObjectLinkType()
Dim returnValue As Integer
returnValue = instance.CreateWebObjectLinks(primaryDataSet, _
linkDataSet, linkTypes)
[SoapDocumentMethodAttribute("https://schemas.microsoft.com/office/project/server/webservices/ObjectLinkProvider/CreateWebObjectLinks", RequestNamespace = "https://schemas.microsoft.com/office/project/server/webservices/ObjectLinkProvider/",
ResponseNamespace = "https://schemas.microsoft.com/office/project/server/webservices/ObjectLinkProvider/",
Use = SoapBindingUse.Literal, ParameterStyle = SoapParameterStyle.Wrapped)]
public int CreateWebObjectLinks(
ObjectLinkProviderDataSet primaryDataSet,
ObjectLinkProviderDataSet linkDataSet,
WebObjectLinkType[] linkTypes
)
Parâmetros
primaryDataSet
Type: WebSvcObjectLinkProvider.ObjectLinkProviderDataSetContém os detalhes da tarefa que deve ser vinculada.
linkDataSet
Type: WebSvcObjectLinkProvider.ObjectLinkProviderDataSetContém os detalhes dos itens de lista que a tarefa deve ser vinculado a.
linkTypes
Type: []Contém uma matriz de tipos de link.
Valor retornado
Type: System.Int32
Examples
O exemplo usa o namespace SvcObjectLinkProvider no assembly de proxy ProjectServerServices.dll. O método ConfigureClientEndPoints usa um arquivo App. config para definir a vinculação, comportamento e ponto de extremidade do WCF. Para obter informações sobre como criar um assembly de proxy PSI e um arquivo App. config, consulte Prerequisites for WCF-Based Code Samples.
Dica
Este exemplo demonstra o uso do método CreateWebObjectLinks ; não é uma solução completa.
Antes de executar o aplicativo de exemplo, execute as seguintes etapas:
Use o Project Professional 2013 para criar e publicar um projeto pelo menos uma tarefa.
Use Project Web App para abrir o site do projeto.
Crie pelo menos uma questão ou risco na lista do SharePoint questão ou risco do site do projeto.
Altere os valores do construtor de acordo com seu exemplo.
Adicionar uma referência da web para https://localhost/pwa/_vti_bin/Lists.asmxe nomeie-o WebSvcLists.
Este exemplo executa as seguintes tarefas:
Verifica se o projeto, a tarefa e o site do projeto existe e obtém as GUIDs correspondentes.
Item (nesse caso, um problema) da lista de links de uma tarefa e uma lista do SharePoint.
Vincula um objeto genérico e uma tarefa.
Obtém a lista de itens do SharePoint que estão vinculadas a uma tarefa e grava o conteúdo do conjunto de dados de ObjectLinkProviderdsLinkedObjects para um arquivo XML.
Obtém uma lista de itens genéricos que estão vinculadas a uma tarefa.
Exclui os vínculos para objetos da web.
Para obter mais informações sobre como funciona o código de exemplo, consulte procedimentos de 1 a 6 no como: usar o OLP para vincular um objeto a uma tarefa. O código completo do exemplo é a seguinte:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ServiceModel;
using System.Data;
using System.Xml;
using System.Web.Services;
using PSLibrary = Microsoft.Office.Project.Server.Library;
using WebSvcLists = Microsoft.SDK.Project.Samples.OLP.WebSvcLists;
namespace Microsoft.SDK.Project.Samples.OLP
{
class OLP
{
private static SvcProject.ProjectClient projectClient;
private static SvcWssInterop.WssInteropClient wssInteropClient;
private static SvcObjectLinkProvider.ObjectLinkProviderClient olpClient;
private static string outFilePathOLPDataSet;
private static string outFilePathListItemsDS;
private static string outFilePathGenericDS;
private static string projectName; // Name of the project.
private static string taskName; // Name of the task.
private static string listName; // Name of the SharePoint list.
private static string listItemTitle; // Title of the SharePoint list item.
private static PSLibrary.WebObjectType webObjectType; // Type of web object (issue, risk, and so on.)
private static string linkedItems; // List of linked items to be displayed.
private static bool deleteLinks = false; // Delete links after they are created.
private const string OUTPUT_FILES = @"C:\Projects\Samples\Output\";
private const string ENDPOINT_P = "basicHttp_Project";
private const string ENDPOINT_WSS = "basicHttp_WssInterop";
private const string ENDPOINT_OLP = "basicHttp_ObjectLinkProvider";
public OLP()
{
// Constructor to assign values to the variables.
// Change these for your example.
projectName = "My Swell Project";
taskName = "T2";
listName = "Issues";
listItemTitle = "Issue1";
}
static void Main(string[] args)
{
if (args.Length > 0 && args[0].ToLower() == "-delete")
{
deleteLinks = true;
}
// Configure the end points.
bool configResult = false;
configResult = ConfigClientEndpoints(ENDPOINT_P);
configResult = ConfigClientEndpoints(ENDPOINT_WSS);
configResult = ConfigClientEndpoints(ENDPOINT_OLP);
OLP param = new OLP();
if (!configResult) throw new ApplicationException();
// If output directory does not exist,create it.
if (!Directory.Exists(OUTPUT_FILES))
{
Directory.CreateDirectory(OUTPUT_FILES);
}
// Set the output file path.
outFilePathOLPDataSet = OUTPUT_FILES + "OLPDataSet.xml";
outFilePathListItemsDS = OUTPUT_FILES + "OLPListItemsDS.xml";
outFilePathGenericDS = OUTPUT_FILES + "OLPGenericDS.xml";
// Lists is a SharePoint web service.
WebSvcLists.Lists wssLists = new WebSvcLists.Lists();
wssLists.Credentials = System.Net.CredentialCache.DefaultCredentials;
Guid taskUid = new Guid();
Guid projectUid = new Guid();
String worksiteUrl = String.Empty;
int itemTPID = -1;
Guid listUid = Guid.Empty;
#region Verify input values.
try
{
Console.WriteLine("Reading the list of projects...");
// Check whether the project exists and get the project GUID.
SvcProject.ProjectDataSet projectList = projectClient.ReadProjectList();
foreach (DataRow projectRow in projectList.Project)
{
if ((string)projectRow[projectList.Project.PROJ_NAMEColumn] == projectName)
{
projectUid = (Guid)projectRow[projectList.Project.PROJ_UIDColumn];
break;
}
}
if (projectUid == Guid.Empty)
{
Console.WriteLine("Project not found: " + projectName + "\n");
throw new ApplicationException();
}
// Get a task UID for the specified task.
Console.WriteLine("Reading the data for project: " + projectName);
SvcProject.ProjectDataSet dsProject =
projectClient.ReadProject(projectUid, SvcProject.DataStoreEnum.PublishedStore);
foreach (DataRow objRow in dsProject.Task)
{
if ((string)objRow[dsProject.Task.TASK_NAMEColumn] == taskName)
{
taskUid = (Guid)objRow[dsProject.Task.TASK_UIDColumn];
break;
}
}
if (taskUid == Guid.Empty)
{
Console.WriteLine("Task not found: " + taskName + "\n");
throw new ApplicationException();
}
// Set the worksite URL for the SharePoint Lists web service.
SvcWssInterop.ProjectWSSInfoDataSet dsProjectWssInfo = new SvcWssInterop.ProjectWSSInfoDataSet();
dsProjectWssInfo = wssInteropClient.ReadWssData(projectUid);
if (dsProjectWssInfo.ProjWssInfo.Count > 0)
{
worksiteUrl = dsProjectWssInfo.ProjWssInfo[0].PROJECT_WORKSPACE_URL.ToString();
wssLists.Url = worksiteUrl + "/_vti_bin/lists.asmx";
}
else
{
Console.WriteLine("Worksite not found for project: " + projectName + "\n");
throw new ApplicationException();
}
// Get the list GUID in the project worksite from the Lists web service.
Console.WriteLine("Reading the SharePoint list data...");
listUid = GetListUid(wssLists, listName);
if (listUid == Guid.Empty)
{
Console.WriteLine("SharePoint list not found: " + listName);
Console.Read();
}
// Get the TP_ID for the specified SharePoint list item.
itemTPID = GetItemTPID(wssLists, listItemTitle);
if (itemTPID < 0)
{
Console.WriteLine(string.Format("List {0} does not contain item: {1}",
listName, listItemTitle));
}
}
catch (FaultException fault)
{
// Use the WCF FaultException, because the ASMX SoapException does not
// exist in a WCF-based application.
WriteFaultOutput(fault);
}
catch (CommunicationException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(
"\n***System.ServiceModel.CommunicationException:");
Console.WriteLine(e.ToString());
Console.ResetColor();
}
catch (ApplicationException)
{
ExitApp();
}
#endregion
try
{
#region Link task and SharePoint list item
// Get the task web object, if there are any existing links to the task.
Console.WriteLine("Finding linked objects for the task...");
Guid taskWebObjectUid = Guid.Empty;
SvcObjectLinkProvider.ObjectLinkProviderDataSet dsLinkedObjects =
new SvcObjectLinkProvider.ObjectLinkProviderDataSet();
dsLinkedObjects = olpClient.ReadTaskWebObject(taskUid);
int numTaskWebObjects = dsLinkedObjects.WebObjects.Count;
if (numTaskWebObjects > 0)
taskWebObjectUid = dsLinkedObjects.WebObjects[0].WOBJ_UID;
else
{
Console.WriteLine(
"There are no linked web objects for task: " + taskName);
}
// Create a web object for the specified task.
Console.WriteLine("Creating a web object for the task...");
SvcObjectLinkProvider.ObjectLinkProviderDataSet dsTask =
new SvcObjectLinkProvider.ObjectLinkProviderDataSet();
SvcObjectLinkProvider.ObjectLinkProviderDataSet.WebObjectsRow taskRow =
dsTask.WebObjects.NewWebObjectsRow();
// Provide information to the Web object row for the task.
// If a task web object does not exist, AddWebObjects creates
// a new web object and updates WOBJ_UID in taskRow.
taskRow.WOBJ_UID = taskWebObjectUid;
taskRow.WOBJ_TASK_UID = taskUid;
taskRow.WOBJ_PROJ_UID = projectUid;
taskRow.WOBJ_TYPE = (int)PSLibrary.WebObjectDatabaseType.Task;
dsTask.WebObjects.AddWebObjectsRow(taskRow);
// Create a web object for the list item, with the TP_ID of the list item.
Console.WriteLine("Creating a web object for the list item...");
SvcObjectLinkProvider.ObjectLinkProviderDataSet dsListItems =
new SvcObjectLinkProvider.ObjectLinkProviderDataSet();
SvcObjectLinkProvider.ObjectLinkProviderDataSet.WebObjectsRow listItemRow =
dsListItems.WebObjects.NewWebObjectsRow();
// Provide information to the web object row for the list item.
// If a list item web object does not exist, AddWebObjects creates
// a new web object and updates WOBJ_UID in listItemRow.
listItemRow.WOBJ_UID = Guid.NewGuid();
listItemRow.WOBJ_TP_ID = itemTPID;
listItemRow.WOBJ_LIST_NAME = listUid;
listItemRow.WOBJ_PROJ_UID = projectUid;
switch (listName)
{
case "Issues":
listItemRow.WOBJ_TYPE =
(int)PSLibrary.WebObjectDatabaseType.Issue;
webObjectType = PSLibrary.WebObjectType.Issue;
linkedItems = "\nIssues found for task: " + taskName;
break;
case "Risks":
listItemRow.WOBJ_TYPE =
(int)PSLibrary.WebObjectDatabaseType.Risk;
webObjectType = PSLibrary.WebObjectType.Risk;
linkedItems = "\nRisks found for task: " + taskName;
break;
case "Documents":
listItemRow.WOBJ_TYPE =
(int)PSLibrary.WebObjectDatabaseType.Document;
webObjectType = PSLibrary.WebObjectType.Document;
linkedItems = "\nDocuments found for task: " + taskName;
break;
case "Commitments": // Commitments are now called Deliverables.
listItemRow.WOBJ_TYPE =
(int)PSLibrary.WebObjectDatabaseType.Commitment;
webObjectType = PSLibrary.WebObjectType.Commitment;
linkedItems = "\nDeliverables found for task: " + taskName;
break;
default:
string errMess = "\n" + listName +
" is not a default SharePoint list type for task links.";
Console.WriteLine(errMess);
break;
}
dsListItems.WebObjects.AddWebObjectsRow(listItemRow);
// You can link the task to multiple list items by adding multiple rows
// to dsListItems and adding ObjectLinkType values to the array of link types.
Console.WriteLine("Linking the web objects...");
SvcObjectLinkProvider.WebObjectLinkType generalLinkType =
SvcObjectLinkProvider.WebObjectLinkType.General;
SvcObjectLinkProvider.WebObjectLinkType[] wssLinkTypeArray = { generalLinkType };
olpClient.CreateWebObjectLinks(dsTask, dsListItems, wssLinkTypeArray);
// Write the list items dataset to an XML file.
dsListItems.WriteXml(outFilePathListItemsDS);
Console.WriteLine("\nSee XML output of the list items Dataset at {0}",
outFilePathListItemsDS);
Console.Write("\nPress any key to continue: ");
Console.ReadKey(true);
#endregion
#region Link task and a generic object
// Link a generic external item to the task.
// WOBJ_TP_ID is an arbitrary constant in this case,
// because the GUID is the important part for the link.
Console.WriteLine("\nLinking a task and a generic object...");
int externalTPID = 1;
// The external item must have a GUID; folowing is a sample GUID.
Guid externalUid = Guid.NewGuid(); // new Guid("12345678-1234-1234-1234-123456789012");
SvcObjectLinkProvider.ObjectLinkProviderDataSet dsGeneric =
new SvcObjectLinkProvider.ObjectLinkProviderDataSet();
SvcObjectLinkProvider.ObjectLinkProviderDataSet.WebObjectsRow genericRow =
dsGeneric.WebObjects.NewWebObjectsRow();
genericRow.WOBJ_UID = Guid.NewGuid();
// Instead of a SharePoint list GUID, you can substitute any GUID here.
// If the object does not have a GUID, or if you need to use the GUID and
// another ID, you can use the WOBJ_TP_ID field.
genericRow.WOBJ_LIST_NAME = externalUid;
genericRow.WOBJ_TP_ID = externalTPID;
genericRow.WOBJ_PROJ_UID = projectUid;
genericRow.WOBJ_TYPE =
(int)PSLibrary.WebObjectDatabaseType.GenericSharePointListItem;
dsGeneric.WebObjects.AddWebObjectsRow(genericRow);
// Create a new link type array for the generic link.
SvcObjectLinkProvider.WebObjectLinkType[] genericLinkTypeArray = { generalLinkType };
olpClient.CreateWebObjectLinks(dsTask, dsGeneric, genericLinkTypeArray);
// Write the generic dataset to an XML file.
dsGeneric.WriteXml(outFilePathGenericDS);
Console.WriteLine("See XML output of the Generic Dataset at {0}",
outFilePathGenericDS);
Console.Write("\nPress any key to continue: ");
Console.ReadKey(true);
#endregion
#region Get the list of SharePoint items linked to the task
// Get the OLP DataSet for all SharePoint items of specified type
// that are linked to the task.
Console.WriteLine("\nGetting the list of SharePoint items linked to the task...");
dsLinkedObjects = olpClient.ReadTaskLinkedWebObjects(taskUid,
(int)webObjectType);
// Write the OLP DataSet to an XML file.
dsLinkedObjects.WriteXml(outFilePathOLPDataSet);
Console.WriteLine("See XML output of OLP Dataset at {0}",
outFilePathOLPDataSet);
Console.Write("\nPress any key to continue: ");
Console.ReadKey(true);
int itemsFound = 0;
foreach (SvcObjectLinkProvider.ObjectLinkProviderDataSet.WebObjectsRow objRow
in dsLinkedObjects.WebObjects)
{
if (objRow.WOBJ_TASK_UID != taskUid)
{
// Add the internal OLP GUID, List GUID, and TP_ID.
linkedItems += string.Format(
"\n\n\tWebObjectUid:\t{0}\n\tList UID:\t{1}\n\tTP_ID:\t\t{2}",
objRow.WOBJ_UID.ToString(),
objRow.WOBJ_LIST_NAME,
objRow.WOBJ_TP_ID.ToString());
itemsFound++;
}
}
if (itemsFound == 0)
{
linkedItems = "No " + listName.ToLower() + " found, for task: " + taskName;
}
else
{
Console.WriteLine(linkedItems, listName);
}
#endregion
#region Get list of generic items linked to the task
// Get the OLP DataSet for all generic items linked to the task.
Console.WriteLine("\nGetting the list of generic items linked to the task...");
dsLinkedObjects = olpClient.ReadTaskLinkedWebObjects(taskUid,
(int)PSLibrary.WebObjectType.GenericSharePointListItem);
linkedItems = "Generic items found:";
itemsFound = 0;
foreach (SvcObjectLinkProvider.ObjectLinkProviderDataSet.WebObjectsRow objRow in
dsLinkedObjects.WebObjects)
{
if (objRow.WOBJ_TASK_UID != taskUid)
{
linkedItems += string.Format(
"\n\n\tWebObjectUid:\t{0}\n\tExternal UID:\t{1}\n\tFake TP_ID:\t{2}",
objRow.WOBJ_UID.ToString(),
objRow.WOBJ_LIST_NAME,
objRow.WOBJ_TP_ID.ToString());
itemsFound++;
}
}
if (itemsFound == 0)
linkedItems = "No generic items found for task: " + taskName;
Console.WriteLine(linkedItems, "Generic Linked Items");
#endregion
#region Delete the links
if (deleteLinks)
{
Console.Write("\nPress any key to delete the links: ");
Console.ReadKey(true);
int[] deletedItems = { 0, 0 };
// Delete the link from the task to the SharePoint list item.
deletedItems[0] = DeleteTaskLinks(olpClient, taskUid, listUid, itemTPID);
// Delete the link from the task to the generic item.
deletedItems[1] = DeleteTaskLinks(olpClient, taskUid, externalUid, externalTPID);
string deletedResults = "For task: " + taskName + "\n\n";
deletedResults += "Deleted SharePoint list items: ";
deletedResults +=
string.Format("{0}\n\nDeleted generic items: {1}",
deletedItems[0].ToString(), deletedItems[1].ToString());
Console.WriteLine(deletedResults, "Deleted Links");
}
else
{
Console.WriteLine("\nThe new links are not deleted.");
}
#endregion
}
catch (FaultException fault)
{
// Use the WCF FaultException, because the ASMX SoapException does not
// exist in a WCF-based application.
WriteFaultOutput(fault);
}
catch (CommunicationException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(
"\n***System.ServiceModel.CommunicationException:");
Console.WriteLine(e.ToString());
Console.ResetColor();
}
catch (ApplicationException)
{
}
ExitApp();
}
#region Private methods
private static Guid GetListUid(WebSvcLists.Lists wssLists, string listName)
{
const string idAttribute = "ID";
const string defaultViewUrl = "DefaultViewUrl";
string listNodeName = "/" + listName + "/";
Guid listUid = Guid.Empty;
XmlNode ndLists = wssLists.GetListCollection();
// Get the GUID for the specified SharePoint list.
foreach (XmlNode ndList in ndLists.ChildNodes)
{
if (ndList.Attributes[defaultViewUrl].Value.Contains(listNodeName))
{
listUid = new Guid(ndList.Attributes[idAttribute].Value);
break;
}
}
return listUid;
}
private static int GetItemTPID(WebSvcLists.Lists wssLists, string listItemTitle)
{
int itemTPID = -1;
XmlDocument xmlDoc = new XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
// Query for the list item title.
string queryFormat =
"<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>";
ndQuery.InnerXml = string.Format(queryFormat, listItemTitle);
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
"<DateInUtc>TRUE</DateInUtc>";
// Get the Title and ID fields in the returned data.
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
ndViewFields.InnerXml = "<FieldRef Name='Title' /><FieldRef Name='ID'/>";
string viewName = string.Empty;
string webId = string.Empty;
string rowLimit = string.Empty;
// GetListItems returns <listitems> element with the child <rs:data ItemCount="1">,
// which contains the following <z:row> child element:
// <z:row ows_Title="Test Issue 1" ows_ID="1" ...[additional metadata] />
// The TP_ID is in the ows_ID attribute, so we can jump to that row.
try
{
XmlNode ndListItems = wssLists.GetListItems(listName, viewName, ndQuery, ndViewFields,
rowLimit, ndQueryOptions, webId);
if (ndListItems.ChildNodes.Count > 1
&& ndListItems.ChildNodes[1].ChildNodes.Count > 1)
{
string tpidValue = "-1";
tpidValue = ndListItems.ChildNodes[1].ChildNodes[1].Attributes["ows_ID"].Value;
itemTPID = Convert.ToInt16(tpidValue);
}
}
catch (CommunicationException e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(
"\n***System.ServiceModel.CommunicationException:");
Console.WriteLine(e.ToString());
Console.ResetColor();
}
return itemTPID;
}
private static int DeleteTaskLinks(SvcObjectLinkProvider.ObjectLinkProvider olp,
Guid taskUid, Guid objectUid, int itemTPID)
{
int deleteResult;
SvcObjectLinkProvider.ObjectLinkProviderDataSet dsDeleteTask =
olp.ReadTaskWebObject(taskUid);
Guid delTaskUid = dsDeleteTask.WebObjects[0].WOBJ_UID;
SvcObjectLinkProvider.ObjectLinkProviderDataSet dsDeleteItem =
olp.ReadSharePointWebObject(itemTPID, objectUid);
Guid delItemUid = dsDeleteItem.WebObjects[0].WOBJ_UID;
deleteResult = olp.DeleteWebObjectLink(delTaskUid, delItemUid);
return deleteResult;
}
// 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();
}
private static void ExitApp()
{
Console.Write("\nPress any key to exit: ");
Console.ReadKey(true);
Environment.Exit(0);
}
#endregion
// Configure the PSI client endpoints.
public static bool ConfigClientEndpoints(string endpt)
{
bool result = true;
switch (endpt)
{
case ENDPOINT_WSS:
wssInteropClient = new SvcWssInterop.WssInteropClient(endpt);
break;
case ENDPOINT_P:
projectClient = new SvcProject.ProjectClient(endpt);
break;
case ENDPOINT_OLP:
olpClient = new SvcObjectLinkProvider.ObjectLinkProviderClient(endpt);
break;
default:
result = false;
Console.WriteLine("Invalid endpoint: {0}", endpt);
break;
}
return result;
}
}
class Helpers
{
/// <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;
}
}
}