我有一个自定义计时器作业,它应该从XML文件中读取一些数据并将其保存在word文件中。我在一个简单的web部件中测试了这个代码,它工作得非常好。然而,它不能与自定义定时器作业一起工作。下面是代码-
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
}
下面是创建XML文件的代码。我在功能级别的事件接收器中添加了这段代码。我将这个XML文件保存在inetpub的虚拟目录中,我的站点集合就部署在这个目录中。路径- 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();
}
}
这些是我用来从XML文件读取的方法。这些在webpart中完全可以工作。但是,当在计时器作业中使用时,它会给出一个异常"对象引用未为对象的实例设置"。
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;
}
这只是我用来读取的方法之一。这是读取列表的列名。
正确生成XML文件。我哪里出错了?
将XML文件保存在C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions目录……大多数owtimer没有访问
的权限Server.MapPath (~/AutomatedReport/ConfigFile.xml)
查看https://spg.codeplex.com以获得更好的配置存储选项。