maandag 11 oktober 2010

Catching console application output and sending it to SharePoint

I recently had to create a wrapper around an existing console application to catch its output. This output had to be sent to a SharePoint list. The console application itself was used to sync files created by SharePoint and stored on a file share to a UNIX system.

1. Use the following code to start your ‘syncing’ console application and catch the output

   1: // Run RSync and capture output



   2: Process p = new Process();



   3:  



   4: // Redirect the output stream of the child process.



   5: p.StartInfo.UseShellExecute = false;



   6: p.StartInfo.RedirectStandardOutput = true;



   7: p.StartInfo.FileName = args[0];



   8: p.Start();



   9:  



  10: // Read the output stream first and then wait.



  11: string output = p.StandardOutput.ReadToEnd();



  12: p.WaitForExit();




2. Write the output to a SharePoint custom list





   1: // Write output to target SharePoint log site. Should be the same as the regular KB site where the 'Logging' list is present



   2: ListsWs.Lists ls = new ListsWs.Lists();



   3: ls.Url = wsUrl + "_vti_bin/lists.asmx";



   4: ls.Credentials = CredentialCache.DefaultCredentials;



   5:  



   6: XmlDocument doc = new XmlDocument();



   7: XmlElement batch_element = doc.CreateElement("Batch");



   8: string item = "<Method ID=\"1\" Cmd=\"New\">" + 



   9:     "<Field Name=\"ID\">New</Field>" + 



  10:     "<Field Name=\"Title\">RSync Wrapper</Field>" + 



  11:     "<Field Name=\"Message\">" + output + "</Field>" + 



  12:     "</Method>";



  13: batch_element.InnerXml = item;



  14: ls.UpdateListItems("Logging", batch_element);


Geen opmerkingen:

Een reactie posten