Connect with me
Search
Twitter Feed
Navigation
« Why is programming for Word so different from Excel and PowerPoint? | Main | Getting the physical folder path of a virtual path or directory in WCF »
Friday
Apr092010

One of the only rare instances where VB.NET > C#

Working with SharePoint and CAML can be a pain, but in VB.NET it's actually quite beautiful using the XML Literals feature.

Here's a query to get all web page documents in a SharePoint list that have been modified since some date.

Dim query = _
<Query>
    <Where>
        <And>
            <Gt>
                <FieldRef Name="Modified"/>
                <Value IncludeTimeValue="TRUE" Type="DateTime"><%= lastSyncDate.GetValueOrDefault(DateTime.MinValue).ToString("yyyy-MM-ddTHH:mm:ssZ") %></Value>
            </Gt>
            <And>
                <Eq>
                    <FieldRef Name="FSObjType"/>
                    <Value Type="Lookup">0</Value>
                </Eq>
                <Eq>
                    <FieldRef Name="DocIcon"/>
                    <Value Type="Lookup">aspx</Value>
                </Eq>
            </And>
        </And>
    </Where>
</Query>
 

The same query in C# (using Linq to XML) looks like this:

var query =
    new XElement("Query",
        new XElement("Where",
            new XElement("And",
                new XElement("Gt",
                    new XElement("FieldRef",
                        new XAttribute("Name", "Modified")
                    ),
                    new XElement("Value",
                        new XAttribute("IncludeTimeValue", "TRUE"),
                        new XAttribute("Type", "DateTime"),
                        DateTime.Now.ToString()
                    )
                ),
                new XElement("And",
                    new XElement("Eq",
                        new XElement("FieldRef",
                            new XAttribute("Name", "FSObjType")
                        ),
                        new XElement("Value",
                            new XAttribute("Type", "Lookup"),
                            0
                        )
                    ),
                    new XElement("Eq",
                        new XElement("FieldRef",
                            new XAttribute("Name", "DocIcon")
                        ),
                        new XElement("Value",
                            new XAttribute("Type", "Lookup"),
                            "aspx"
                        )
                    )
                )
            )
        )
    );
 

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>