Set Weekdays

Hi,


I’m having problems with the project calendar.
I need to set the days of the standard calendar, but I can not.
My task begins on 01/01/2015 and ends on 04/01/2015 on Sunday, but to save, the end date is changed to 05.01.2015 Monday. Sunday is not considered.

Is there any solution to change the default calendar?

Thanks.

Hi Anderson,


Thank you for posting your query.


Sunday is considered as an Off day in the calendar by default. If you want to make it working, you have to add it to the calendar as working day and define its times as shown in the following code. Please try it at your end and share your feedback with us.

Code:



//Create a project instance

Project prj = new Project();


//Define Calendar

Aspose.Tasks.Calendar cal = prj.Calendars.GetByUid(1);

WeekDay sunday = cal.WeekDays.ToArray()[6];

sunday.DayWorking = true;

//Sets working time. Only time part of DateTime is important

WorkingTime wt1 = new WorkingTime();

wt1.FromTime = new DateTime(1, 1, 1, 9, 0, 0, 0);

wt1.ToTime = new DateTime(1, 1, 1, 12, 0, 0, 0);

WorkingTime wt2 = new WorkingTime();

wt2.FromTime = new DateTime(1, 1, 1, 13, 0, 0, 0);

wt2.ToTime = new DateTime(1, 1, 1, 17, 0, 0, 0);

sunday.WorkingTimes.Add(wt1);

sunday.WorkingTimes.Add(wt2);

Task task = prj.RootTask.Children.Add(“Task 1”);

task.Set(Tsk.Start, new DateTime(2015, 1, 1));

task.Set(Tsk.Finish, new DateTime(2015, 1, 4));

//Save the Project

prj.Save(“Project.Xml”, Aspose.Tasks.Saving.SaveFileFormat.XML);
Hi Muhammad,
Thanks for the feedback.
The code sent by you works correctly, but if you run again for the same file, the days Saturday and Sunday are again not a work day and in our case we need to run more than once according to the parameters of our system.

Thanks.

Hi Anderson,


If we create a new project like Project prj = new Project(); then by default the Saturday and Sunday will not be a working weekday.

However if we open the resultant file i.e. “Project.xml” which was saved earlier by this code, then we get Sunday as working day.

Please feel free to write us back if you have any other query in this regard.

Hi Muhammad,


Best detailing the problem.
The created file “Project.mpp” is again open in our system and the code below is run, but the file calendar day Saturday and Sunday are again non-working days.

Project project = new Project(textBox2.Text);

Aspose.Tasks.Calendar cal = project.Calendars.GetByUid(1);

WorkingTime wt1 = new WorkingTime();
wt1.FromTime = new DateTime(1, 1, 1, 9, 0, 0, 0);
wt1.ToTime = new DateTime(1, 1, 1, 12, 0, 0, 0);
WorkingTime wt2 = new WorkingTime();
wt2.FromTime = new DateTime(1, 1, 1, 13, 0, 0, 0);
wt2.ToTime = new DateTime(1, 1, 1, 18, 0, 0, 0);

WeekDay saturday = cal.WeekDays.ToArray()[6];
saturday.DayWorking = true;
saturday.WorkingTimes.Add(wt1);
saturday.WorkingTimes.Add(wt2);

WeekDay sunday = cal.WeekDays.ToArray()[0];
sunday.DayWorking = true;
sunday.WorkingTimes.Add(wt1);
sunday.WorkingTimes.Add(wt2);

project.Save(“Project.mpp”, Aspose.Tasks.Saving.SaveFileFormat.MPP)

Thanks.

Hi Anderson,

We are sorry for any inconvenience you are facing.

I have re-tested this issue at my end with a new Microsoft Project 2010 (MPP) file as input and the following code sample, but was unable to reproduce the issue. Please check the following code sample where I am calling the MakeWeekendAsWorking() method first from the Main() method and then calling the CheckWorkingCalendarReload() after that. The output is as attached here with this post. Could you please try it at your end and share your feedback with us.

Code:

public static void CheckWorkingCalendarReload()
{
    Project project = new Project("Project.mpp");
    Calendar cal = project.Calendars.GetByName("Standard");
    Console.WriteLine("\n******** After loading the MPP file *********");
    foreach (WeekDay day in cal.WeekDays)
    Console.WriteLine(day.DayType+ "=" + day.DayWorking);
    project.Save("Project1.Xml", Aspose.Tasks.Saving.SaveFileFormat.XML);
}

public static void MakeWeekendAsWorking()
{
    Project project = new Project("New project 2010.mpp");
    Calendar cal = project.Calendars.GetByName("Standard");
    Console.WriteLine("\n******** Before setting Saturday/Sunday to Working *********");

    foreach (WeekDay day in cal.WeekDays)
        Console.WriteLine(day.DayType + "=" + day.DayWorking);

    WeekDay saturday = cal.WeekDays.ToArray<WeekDay>()[6];
    saturday.DayWorking = true;
    //Sets working time. Only time part of DateTime is important
    WorkingTime wt1 = new WorkingTime();
    wt1.FromTime = new DateTime(1, 1, 1, 9, 0, 0, 0);
    wt1.ToTime = new DateTime(1, 1, 1, 12, 0, 0, 0);

    WorkingTime wt2 = new WorkingTime();
    wt2.FromTime = new DateTime(1, 1, 1, 13, 0, 0, 0);
    wt2.ToTime = new DateTime(1, 1, 1, 17, 0, 0, 0);

    saturday.WorkingTimes.Add(wt1);
    saturday.WorkingTimes.Add(wt2);

    WeekDay sunday = cal.WeekDays.ToArray<WeekDay>()[0];
    sunday.DayWorking = true;
    //Sets working time. Only time part of DateTime is important
    sunday.WorkingTimes.Add(wt1);
    sunday.WorkingTimes.Add(wt2);
    Console.WriteLine("\n******** After setting Saturday/Sunday to Working *********");
    foreach (WeekDay day in cal.WeekDays)
        Console.WriteLine(day.DayType + "=" + day.DayWorking);
    project.Save("Project.Xml", Aspose.Tasks.Saving.SaveFileFormat.XML);
    project.Save("Project.mpp", Aspose.Tasks.Saving.SaveFileFormat.MPP);
}

Hi Muhammad,


I auditioned you indicated, in the calendar class really the properties are correct as shown by you, but in MS Project schedule assumes these characteristics, as can be seen in the attached pictures.

File creation: CreationFile.png
Changing the file: AfterUpdateFile.png

Thanks.

Hi Anderson,

Can you please check the attached files at your end in Microsoft Project and share your feedback with us? These have been generated through the code sample above and show Saturday/Sunday as working. I am using Microsoft Project 2010 at my end. In case the problem persists, please share screenshots as above with us for our further investigation.

Hi Kashif,


I use MS Project 2013.
I think I could solve the problem.
I realized that the Collection “WorkingTimes” on file creation, is empty for the days Saturday and Sunday, but the change in the same file is completed and again I add other ranges, which was not correct.
I changed my code as follows, and it worked.

===========================================
WorkingTime wt1 = new WorkingTime();
wt1.FromTime = new DateTime(1, 1, 1, 9, 0, 0, 0);
wt1.ToTime = new DateTime(1, 1, 1, 12, 0, 0, 0);
WorkingTime wt2 = new WorkingTime();
wt2.FromTime = new DateTime(1, 1, 1, 13, 0, 0, 0);
wt2.ToTime = new DateTime(1, 1, 1, 18, 0, 0, 0);

WeekDay sunday = cal.WeekDays.ToArray()[0];
sunday.DayWorking = true;

WeekDay saturday = cal.WeekDays.ToArray()[6];
saturday.DayWorking = true;

if (saturday.WorkingTimes.Count > 0)
{
WorkingTime wta = saturday.WorkingTimes.ToArray()[0];
wta = wt1;
WorkingTime wtb = saturday.WorkingTimes.ToArray()[1];
wtb = wt2;
}
else
{
saturday.WorkingTimes.Add(wt1);
saturday.WorkingTimes.Add(wt2);
}

if (sunday.WorkingTimes.Count > 0)
{
WorkingTime wta = sunday.WorkingTimes.ToArray()[0];
wta = wt1;
WorkingTime wtb = sunday.WorkingTimes.ToArray()[1];
wtb = wt2;
}
else
{
sunday.WorkingTimes.Add(wt1);
sunday.WorkingTimes.Add(wt2);
}
===========================================

Is correct this behavior?

Thanks.

Hi Anderson,


Thank you for providing feedback.

You are right that each day should have appropriate working times collection and you have made proper changes in the code.

Please feel free to right us back, if you have any other query in this regard.