[ROYALINE’s] SharePoint Dynamics

SharePoint Development & Techniques

Using an XML “settings” file to store values for your SharePoint projects

Posted by Freelance-Puma on August 11, 2011


I recently created a SharePoint timer job solution that relied on the data in a list as to what the function of the job was to do. The solution requirement was to have a timer job set to run at 8:00am every Thursday that would read SharePoint list data and create a customized email of that data for the week.

Since I work in an environment with multiple SharePoint farms, I had to rely on a ‘settings’ file that I could easily change without having to recompile and redeploy my WSP solutions. I will show you how to read the nodes and values of an XML file located in a SharePoint list that your SharePoint solutions can reference, thus eliminating the need to recompile your code for each SharePoint farm. Let’s get started…

First, let’s assume you have a simple XML file:

<?xml version="1.0" encoding="utf-8" ?>
<WSPConfiguration>
	<SharePointFarm Environment="Production">
		<Site>
			<Url>http://www.royaline.com/articles</Url>
			<List Name="My Custom List">
				<Fields>
					<Field>EmailTo</Field>
					<Field>EmailSubject></Field>
					<Field>EmailBody</Field>
				</Fields>
			</List>
			
		</Site>
	</SharePointFarm>
	<SharePointFarm Environment="Staging">
		<Site>
			<Url>http://staging.royaline.com/articles</Url>
			<List Name="My Custom List">
				<Fields>
					<Field>EmailTo</Field>
					<Field>EmailSubject></Field>
					<Field>EmailBody</Field>
				</Fields>
			</List>
			
		</Site>
	</SharePointFarm>
<WSPConfiguration>

Remember, the concept of using an XML file as your settings file will let you change the XML without having to recompile your code. If I want (or need) to move my WSP solution to another SharePoint farm, or even have my code point to another SharePoint site or list, I can easily change this XML file as needed.

Since I am the Site Collection Administrator and have access to everything in my site collection, I decided to create a “WSP Settings” document library at my root site collection. I upload all my XML settings files to a project specific subdirectory so I can easily reference it within my Timer job.

Now, when my timer job runs every thursday morning, it will do the following:

  1. Open the XML Settings file
  2. Read the XML file into memory
  3. Read the values of the XML nodes to build and send an email

To read the XML settings file, add the following code to your project:

private void readXMLSettings()
{
	string SETTINGS_XML = "http://www.royaline.com/Lists/WSP%20Settings/MyWSPProjectSettings.xml";

	//Create a URI and WebRequest
	Uri uri = new Uri(SETTINGS_XML);
	WebRequest req = WebRequest.Create(uri);
	
	//Pass in the default credentials
	req.UseDefaultCredentials = true;
	
	//Get a web response and create the stream object
	WebResponse response = req.GetResponse();
	Stream stream = response.GetResponseStream();

	//Create an XML document and load the stream into memory
	XmlDocument xDoc = new XmlDocument();
	xDoc.Load(stream);

	//Don't forget to add the 'System.Xml.XPath' namespace.
	XPathNavigator xNav = xDoc.CreateNavigator();

	//Get the values of each node (or attribute) in the XML settings file
	string productionURL = xNav.SelectSingleNode("/WSPConfiguration/SharePointFarm[@Environment='Production']/Url").Value.ToString();
	string stagingURL = xNav.SelectSingleNode("/WSPConfiguration/SharePointFarm[@Environment='Staging']/Url").Value.ToString();
	
	
	//do work
}

While this isn’t a complete example, you can see how I made reference to an XML file in a SharePoint list and read the XML attributes and values into my project. Using XML files in a protected SharePoint list is a good way of eliminating the need to recompile your code to reflect changes to your location or lists.

END

Leave a comment