ElementExtensions

 

The Resource class in the Live Framework API provides an ElementExtensions property allowing user-provided elements to be accessed in a DataEntryResource. ElementExtensions is declared as follows:

public List<ResourceMarkupExtension> ElementExtensions { get; }

ElementExtensions provides access to user-defined elements that are children of the <entry> element in the Atom feed representation of a data entry.

An element named bird with text "Emu" can be added as a child of the entry element in the Atom feed as follows:

XmlQualifiedName xmlQualifiedName = new XmlQualifiedName("bird");
String elementContent = "Emu";
ResourceMarkupExtension rme = new ResourceMarkupExtension(xmlQualifiedName, elementContent);
dataEntry.Resource.ElementExtensions.Add(rme);

This creates the following element:

<bird m:NonXmlContent="" xmlns:m="http://user.windows.net" xmlns="">Emu</bird>

The DataContractSerializer can also be used in to serialize class data into ElementExtensions as follows:

[DataContract]
public class Bird 
{
   [DataMember] 
   public String Species  { get; set; } 

   [DataMember]
   public Boolean Flightless  { get; set; } 

Bird emu = new Bird() { Species = "Emu", Flightless = true }; 
SyndicationElementExtension emuSyndicationExtension = new SyndicationElementExtension(emu); 
ResourceMarkupExtension emuResourceExtension = new ResourceMarkupExtension(emuSyndicationExtension); 
dataEntry.Resource.ElementExtensions.Add(emuResourceExtension);
dataEntry.Update();

This creates the following element:

<Bird xmlns="http://schemas.datacontract.org/2004/07/Cumulus"  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">  
      <Flightless>true</Flightless> 
      <Species>Emu</Species> 
</Bird>

Where Cumulus is the namespace of the Bird class.

This can be accessed as follows:

List<ResourceMarkupExtension> elementExtensions = dataEntry.Resource.ElementExtensions;
foreach (ResourceMarkupExtension elementExtension in elementExtensions)
{
    DataContractSerializer ds = new DataContractSerializer(typeof(Bird));
    Bird bird = (Bird)ds.ReadObject(elementExtension.GetReader());
    String content = elementExtension.Content;
}

ElementExtensions and AttributeExtensions are discussed on this thread on the MSDN forums.

Technorati Tags:

About Neil Mackenzie

Cloud Solutions Architect. Microsoft
This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to ElementExtensions

  1. Vikas says:

    your blog post has been tagged athttp://delicious.com/LiveFrameworkand linked to Live Framework Forum thread:http://social.msdn.microsoft.com/Forums/en-US/liveframework/thread/828d9a48-239a-4af8-8239-35931e514d37

  2. Unknown says:

    Is it possible to query a DataFeed and have it return DataEntries based on the DataEntries ElementExtension values?I know this isn’t a legitimate query; I just want to try and show the logic of my question. Something like:Select DataEntries From MyDataFeed Where dataEntry.Resource.ElementExtensions.Bird = "Emu"I guess my real question is, how do I query/return specific DataEntries based on my custom data that I’ve added via: * Attribute Extensions* Element Extensions* UserDataFinding a specific DataEntry and then looping through its ElementExtentions seems limited & time consuming to me. I’m having a hard time figuring out how to store Custom Data with many fields in the Mesh and then being able to query those fields in a meaningful way. Storing it in Element Extensions, Attribute Extensions, or UserData is straightforward. But querying and returning many DataEntries based on data stored in these areas seems rather difficult to me.Thanks for any help!

  3. Neil says:

    An earlier commenter asks "Is it possible to query a DataFeed and have it return DataEntries based on the DataEntries ElementExtension values?"I believe the answer to be no and that it is not possible currently to query the content of attribute extensions, element extensions and user data. I believe there have been indications on the MSDN Live Framework forum that Microsoft is aware of this as an issue – although I’m not sure how much support Microsoft could ever give to queries of user data.Sorry for not responding earlier.

  4. Daniel says:

    Being able to query by those extensions is a must. How can you do that?

  5. Neil says:

    Daniel -You cannot query extensions at this time.I created a simple element extension on a mesh object and ran a filter query using it. The query returned all the mesh objects without taking account of the filter. Furthermore, the Fiddler output contained the following:LiveFX-Query-Execution-Failure: TrueLiveFX-Query-Execution-Failure-Description: Unknown Property Or FieldIf query extensions are a "must" you should submit a suggestion to Microsoft Connect.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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