Work Order Linked Documents

Hi,

I am trying to display the documents that are linked to a workorder and have come unstuck.

I know that I have to use Workorder.Docs

Then I come very unstuck ! How do I use it ??

Hi Martin, do you mean a linked document or a Wiki page?
If you mean an assigned document those are contained in a child collection attached to the parent object.
You can get the url for one from the Docs property of the workorder object.

For example if a workorder had one assigned document and you fetched the workorder under a variable called w then it might look like this:

string url=w.Docs[0].URL;

Collections are zero based so 0 is the first one.

You could also iterate the collection like this:
foreach(AssignedDoc d in w.Docs)
{
string url=d.URL;
}

AssignedDoc is documented here: http://api.ayanova.com/html/AllMembers_T_GZTW_AyaNova_BLL_AssignedDoc.htm

Hi,

it is for a linked document. Found the wiki stuff on another thread, which started me down this route ! Teh example you gave was perfect.

Hopefully one last question.

To add a document do you use AssignedDocs add ?? Trying to get this to work with no success.

Thanks for your time and help.

Hi Martin, below is a working example I just put together.
Yes you do use AssignedDocs.Add but not directly, it’s a child collection so it’s always accessed via the parent.
The add method is documented here: http://api.ayanova.com/html/M_GZTW_AyaNova_BLL_AssignedDocs_Add.htm
And here is some working code from a sample database (which has workorder 40):

                //AssignedDoc is a member of AssignedDocs which is a child collection
                //it can only be accessed via the parent object, for example:
                //a workorder
                Workorder w = Workorder.GetItem(Workorder.GetIDFromNumber("40", WorkorderTypes.Service));

                //This next part is a little tricky since you need to specify the correct object types and this
               // is a service workorder so there are two different ones, other types of objects such as client are easier than workorders as it's the same type twice

                //You need to specify two RootObjectTypes and the ID of the parent object in order to add a new record to the
                //docs collection.
                //When in doubt, use the AyaNova UI to add a document to the object of your choice, then
                //through code retrieve the document and examine the two RootObjectTypes that AyaNova used if you want to replicate it

                //In the case of a service work order it's like this:
                AssignedDoc doc = w.Docs.Add(RootObjectTypes.Workorder, w.ID, RootObjectTypes.WorkorderService);

                //These are the only two properties you need to set
                doc.Description = "My linked document description";
                doc.URL = @"c:\documents\mydocument.pdf";//don't forget to escape backslashes or use the @ literal specifier 

                //Now save the parent which will automatically save all child collections including this one
                w.Save();

Hi john
I was laso trying this but i got some problem.I think problem is in this below line please review this
//In the case of a service work order it’s like this:
AssignedDoc doc = w.Docs.Add(RootObjectTypes.Workorder, w.ID, RootObjectTypes.WorkorderService);
I this correct.It is written by you

I tested the sample code before posting it so it should be fine Ethan. What problem are you seeing?

Hi Ethan,

do not know whether this helps. Below is my complete code. Any text boxes used are just to display info. I use a generic form so a lot of teh using statements are not actually needed for the code to run. What it does is move files associated with the workordeer from directEntryFolder to folders customerFolder and sourceFolder. It also determines what year it is, since we store stuff by calender year, and adds that to the path of the two destination folders.

Once it has moved teh files it then adds them to the worder document list. If they already exist then they will not be added. Probably more elegant ways of achieving the end goal but I am a noob at API and .net stuff so if anyone can make it better please do !

========================
// The aims of this code are:
// Smooth Tech Clear Process
// =========================
// Put todays date in Date cleared
// Move all files associated with a job in direct entry to appriopriate folders
// Link files to job
// Change Status to Comm Clear

using System.IO;
using GZTW.AyaNova.BLL;
using CSLA;

using System;
using System.Diagnostics;
using System.Net.Mail;
using System.Runtime.InteropServices;

using System.Net;
using System.Windows.Forms;
using System.Drawing;

using Microsoft.VisualBasic;

private void Detail1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {

// ==================================================
// declare where the various Tech Clear folders are:
// ==================================================
string directEntryFolder=“J:\Team Instrument Service\Calibration Reports - from direct entry”;

string customerFolder=“J:\Team Instrument Service\Calibration Reports - approved and archived”;

string sourceFolder=“J:\Team Instrument Service\AyaNova Source CR and Misc Attachments”;

// ==================================================
// We also need the year
// ==================================================
string thisYear = DateTime.Now.ToString(“yyyy”);
label3.Text="This Year is : " + thisYear;

customerFolder=customerFolder + “\” + thisYear + “\”;
sourceFolder = sourceFolder + “\” + thisYear + “\”;

string tempFolder; // will use this later when moving files

// ==================================================
// Get the workorder Number
// ==================================================
Guid gWOItemID=new Guid(DetailReport1.GetCurrentColumnValue(“ID”).ToString());

//now that we have the Item, now can fetch the workorder from the woitem id
Workorder w = Workorder.GetWorkorderByRelativeNoMRU(RootObjectTypes.WorkorderItem, gWOItemID);

// re write summary !!!
//w.Summary=“Martin was here”;

//now get the correct workorder item from this workorder
WorkorderItem wi = w.WorkorderItems[gWOItemID];

// =======================================================================
// Get list of files associated with teh work order in direct entry folder
// =======================================================================
string jobToFind= DetailReport.GetCurrentColumnValue(“LT_O_Workorder”).ToString();
jobToFind = jobToFind + “.”;

string[] filePaths = Directory.GetFiles(@directEntryFolder, jobToFind);
label1.Text = “Files Associated With Job”;
label7.Text = “Error List:”;

// ====================================================
// Now to move the files associated with the workorder
// ====================================================
foreach (string value in filePaths)
{
label1.Text=label1.Text + Environment.NewLine + value;

// is the file a cal report or service report.
int isCalReport = Path.GetFileName(value).IndexOf("_CR.pdf");
int isServiceReport = Path.GetFileName(value).IndexOf("_SR.pdf");

// Assume that the field is destined for the source folder
tempFolder =sourceFolder;

if(isCalReport>1 || isServiceReport>1){

tempFolder = customerFolder;
}

// Check to see if the file exists in target folder
if (File.Exists(tempFolder + Path.GetFileName(value)))
{	// Whoops file exists
    label7.Text=label7.Text + Environment.NewLine + "File Exists " + tempFolder + Path.GetFileName(value);

}
else
{
// copy file. If file already exists do not overwrite. Theoretically file does not exist in target so this is boots and braces
System.IO.File.Copy(value, tempFolder + Path.GetFileName(value), false);
File.Delete(value);
}

// now to append the file

int docexists=1;

foreach(AssignedDoc d in w.Docs)
{
string url=d.URL;

// string uniqueid=d.ID.ToString();
//label10.Text=label10.Text + Environment.NewLine + uniqueid;

if (Path.GetFileName(url)==Path.GetFileName(value))

{
// document already linked
docexists=0;
}

}

if (docexists == 1)
{

//AssignedDoc is a member of AssignedDocs which is a child collection
//it can only be accessed via the parent object, for example:
//a workorder

//This next part is a little tricky since you need to specify the correct object types and this
// is a service workorder so there are two different ones, other types of objects such as client are easier than workorders as it’s the same type twice

//You need to specify two RootObjectTypes and the ID of the parent object in order to add a new record to the
//docs collection.
//When in doubt, use the AyaNova UI to add a document to the object of your choice, then
//through code retrieve the document and examine the two RootObjectTypes that AyaNova used if you want to replicate it

//In the case of a service work order it’s like this:
AssignedDoc doc = w.Docs.Add(RootObjectTypes.Workorder, w.ID, RootObjectTypes.WorkorderService);

// string mDoc=w.Docs.Contains(@“J:\Team Instrument Service\Analysis\Weekly Reports\deleteme.txt”).ToString();

//These are the only two properties you need to set
doc.Description = Path.GetFileName(value);
doc.URL = tempFolder + Path.GetFileName(value); // don’t forget to escape backslashes or use the @ literal specifier

}

}

// Stes workorder to comms clear.
w.WorkorderService.WorkorderStatusID=Guid.Parse(“4599d727-a6c7-4adb-ad1b-ee2117fb9050”);

w.Save();
}