tirsdag den 2. september 2014

Project Server 2010 - PSI - Calendar Exceptions (AddCalendarExceptionsRow, RemoveCalendarExceptionsRow)

In the recent week we were creating an integration with vacation information from an external system into Project Server (2010).
We wanted to replicate all the calendar exceptions from the external system into Project Server.

This task looked quite simple until we got started using the PSI interface. We encountered a couple of problems and strange documentation. In our scenario we could not update the Project Server to the latest service pack so i do not know if these errors already are fixed.

In the following I will demonstrate how to delete all calendar exceptions on a resource and add only one new exception. This program was only created to outline how the PSI interface worked with calendar exceptions (creating one random exception does not make a lot of sense).




//The resource uid on the resource we want to change.
Guid resUID = new Guid("10b4d15c-4b62-417b-9b4d-924000c14cac");

WS_Resources.Resource wsresource = new WS_Resources.Resource();
wsresource.Url = "http://MyPWA/PWA/" + "/_vti_bin/psi/resource.asmx";
wsresource.Credentials = new System.Net.NetworkCredential("Administrator", "pass@word1", "CONTOSO");
WS_Resources.ResourceDataSet resDS = wsresource.ReadResources(""/*no filter*/, false);

WS_Resources.ResourceDataSet.CalendarExceptionsRow[] calExceptions = null;

//Not needed, only part of test
foreach (WS_Resources.ResourceDataSet.ResourcesRow rrow in resDS.Resources)
{
    if (rrow.RES_UID == resUID)
    {
        //Find exceptions so we can call RemoveCalendarExceptionsRow on each row
        calExceptions = rrow.GetCalendarExceptionsRows();
        break;
    }
}
foreach (WS_Resources.ResourceDataSet.CalendarExceptionsRow row in calExceptions)
{
    if (row.RES_UID == resUID)
    {
        //You would think this will do the trick, however this actually caused
        // an Unhandled Comunication Error when we called UpdateResources().
        //resDS.CalendarExceptions.RemoveCalendarExceptionsRow(row);
        //row.Delete();
        break;
    }

}
           
foreach (WS_Resources.ResourceDataSet.CalendarExceptionsRow calexrow in resDS.CalendarExceptions)
{
    if (calexrow.RES_UID == resUID)
    {
        //This is what works
        calexrow.Delete();
    }
}

WS_Resources.ResourceDataSet.CalendarExceptionsRow newCE = resDS.CalendarExceptions.NewCalendarExceptionsRow();
newCE.RES_UID = resUID;
newCE.Start = new DateTime(2013, 08, 12);
newCE.Finish = new DateTime(2013, 08, 16);
newCE.Name = "Vacation";
newCE.RecurrenceFrequency = 1;
newCE.RecurrenceType = (int)PSLibrary.CalendarConstants.CalendarRecurrenceType.Daily;
resDS.CalendarExceptions.AddCalendarExceptionsRow(newCE);
try
{
    wsresource.CheckOutResources(new Guid[] { resUID });
    wsresource.UpdateResources(resDS, false, false);
    wsresource.CheckInResources(new Guid[] { resUID }, false);
}
catch (Exception Ex) { wsresource.CheckInResources(new Guid[] { resUID }, true); throw Ex; }
finally {  }

Ingen kommentarer:

Send en kommentar