Using LinqToXml to pre-populate SharePoint list with entries

This post is both about how to use LinqToXml to parse values from xml and how to leverage that in SharePoint to add a couple of values to a list.
What I wanted to is insert a couple of settings that I´ll use later in my application code. I store those values in a xml file in the 12 hive (features) and then I´ll use that file to insert values into SharePoint. Let´s see how:

The Feature

The feature includes the values I wan´t to add in a xml file (settings.xml) that looks like this:


<?xml version="1.0" encoding="utf-8" ?>
        <Value>data source=.\SQLEXPRESS;Integrated Security=SSPI;……</Value>



I´m also adding the settings.xml file to the DLL Resources to be able to very easily get the resource stream in code. Otherwise I would have to use some like:

assembly.GetManifestResourceStream( “YourNamespace.fileName.xml”);

Just drag and drop it in the editor and VS will do the rest for you. Now I can write


…to accomplish the same thing. A string that holds the contents of the file.

The Feature Receiver

I´m using a bit of LinqToXml to parse the values from the embedded resource to an anonymous type that I use to better handle the inserts, like this:


XElement xml = XElement.Parse(Resources.Settings);

var settings = (from x in xml.Descendants("Setting")
                select new
                    Key = new 
                        Name = x.Element("Key").Name.LocalName, 
                        Value = x.Element("Key").Value 
                    Value = new 
                        Name = x.Element("Value").Name.LocalName, 
                        Value = x.Element("Value").Value 


After this is done, I´m going to have a list of types that resemble a key/value pair. The Name will hold the name of the field in SharePoint and the Value will hold the value to insert into that field.
Next, the inserts:


Dispose(properties, items =>
        foreach (var setting in settings)
            var item = items.Add();
            item[setting.Key.Name] = setting.Key.Value;
            item[setting.Value.Name] = setting.Value.Value;


I´m using a method, called Dispose (that takes an Action delegate), to help me get the RootWeb and the SettingsList.Items. The lambda expression ( => ) is just a neat way to write an inline anonymous delegate that I send to Dipose as the action.
That method looks like this if you´re interested:


private void Dispose(SPFeatureReceiverProperties properties, Action<SPListItemCollection> action)
    using (SPWeb web = (properties.Feature.Parent as SPSite).OpenWeb())
        var settings = web.Lists["Settings"].Items;

 In this method, I´m disposing the SPWeb, fetching the listitems and then I call the action (the delegate/method) that was sent into Dispose which of course is the stuff between the {} after the lambda =>, that is an anynomous delegate.


The Result

Two new values inserted.



, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: