I have a custom timer job which should read some data from an XML file and save it in a word file. I tested this code in a simple web part and it works perfectly fine. However it is not working with custom timer job. Here is the code -
public override void Execute(Guid targetInstanceId)
{
#if (DEBUG)
System.Diagnostics.Trace.Assert(false);
#endif
// get a reference to the current site collection's content database
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
//Define a string builder object
StringBuilder stringBuilder = new System.Text.StringBuilder();
//double revenue = 0;
//creating object of configuration class
//Reading page size from xml configuration file
int intPageLimit = weeklyConfig.readPageSize();
//Reading column name from xml configuration file
List<string> strList = weeklyConfig.readColumnName();
//Reading list name from xml configuration file
string strListName = weeklyConfig.readListName();
//Reading Library name from xml
string strLibraryName = weeklyConfig.readLibraryName();
stringBuilder.Append(intPageLimit);
foreach (string str in strList)
{
stringBuilder.Append(str);
}
stringBuilder.Append(strListName);
stringBuilder.Append(strLibraryName);
SPFolder customerDocLib = null;
customerDocLib = contentDb.Sites[0].RootWeb.Folders["Reports"];
byte[] byteArray = Encoding.ASCII.GetBytes(stringBuilder.ToString());
MemoryStream stream = new MemoryStream(byteArray);
string fileName = "TestReport_" + DateTime.Now.ToString("MMddyyyyhhmmss") + ".doc";//Creating .doc file
customerDocLib.Files.Add(fileName, stream);//writing memory stream to doc file and saving it into SharePoint document library
}
Here is the code which creates the XML file. I have added this code in an Event Receiver at feature level. I'm saving this XML file in inetpub's virtual directory where my site collection is deployed. Path - C:/inetpub/wwwroot/wss/VirtualDirectories/38118/Configuration/ConfigFile.xml
public static void CreateConfigFile(String path)
{
//Use an xml Writer
using (XmlWriter writer = XmlWriter.Create(path + "/ConfigFile.xml"))
{
//Start the Xml Document
writer.WriteStartDocument();
//Create Root element
writer.WriteStartElement("Root");
//Write Element in the root element
writer.WriteElementString("PageSize", "33");
writer.WriteElementString("Column", "Title");
writer.WriteElementString("Column", "SalesPerson");
writer.WriteElementString("Column", "SalesTarget");
writer.WriteElementString("Column", "Achieved");
writer.WriteElementString("Column", "UnitPrice");
writer.WriteElementString("Column", "TotalRevenue");
writer.WriteElementString("ListName", "SalesList");
writer.WriteElementString("LibraryName", "Reports");
//End the root element
writer.WriteEndElement();
//End the Xml Document
writer.WriteEndDocument();
}
}
These are the methods I'm using to read from the XML file. These work totally fine with a webpart. But it gives an exception "Object reference not set for an instance of an object" when used in a timer job.
public class configuration : LayoutsPageBase
{
public List<string> readColumnName()
{
XmlReader reader = XmlReader.Create(Server.MapPath("~/AutomatedReport/ConfigFile.xml"));
List<string> listColumnName = new List<string>();
string element = "";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
element = reader.Name;
}
else if (reader.NodeType == XmlNodeType.Text)
{
switch (element)
{
case "Column": listColumnName.Add(reader.Value.ToString());
break;
}
}
}
return listColumnName;
}
This is only one of the methods I'm using to read. This is reading the column names of a list.
The XML file is generated properly. Where am I going wrong here?
Save your XML file in
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14
directory.....
Mostly OWSTimer don't have an access to
Server.MapPath("~/AutomatedReport/ConfigFile.xml")