Aug13

Pre-Populate a SharePoint List Instance

 Categories: CAML, Development, SharePoint

So, you want to provide a look-up in a SharePoint Site Definition and you'd quite like to provide your users with some options? (i.e. items in the source list)

Firstly you're probably interested in this post from Josh Gaffy about adding a lookup column declaratively using CAML.

Next you'll be wanting to use this snippet to declare your source list and the data it will contain.

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   3:       <ListInstance FeatureId="89CA0ED1-8E9B-48fa-8C4D-CB406544D662"
   4:                 TemplateType="100"
   5:                 Title="Option List"
   6:                         OnQuickLaunch="TRUE"
   7:                 Description="Choices"
   8:                 Url="Options" >
   9:             <Data>
  10:                   <Rows>
  11:                         <Row>
  12:                               <Field Name="Title">Foo</Field>
  13:                         </Row>
  14:                         <Row>
  15:                               <Field Name="Title">Bar</Field>
  16:                         </Row>
  17:                         <Row>
  18:                               <Field Name="Title">Fred</Field>
  19:                         </Row>
  20:                         <Row>
  21:                               <Field Name="Title">Mildred</Field>
  22:                         </Row>
  23:                         <Row>
  24:                               <Field Name="Title">Karl</Field>
  25:                         </Row>
  26:                         <Row>
  27:                               <Field Name="Title">Lenny</Field>
  28:                         </Row>
  29:                   </Rows>
  30:             </Data>
  31:       </ListInstance>
  32: </Elements>

Hope that someone finds this useful.

 
 
Mar4

Composing multiple criteria CAML Queries

 Categories: CAML, SharePoint

OK, so I've been wiriting a pile of CAML queries recently.
 
Now there is the fantastic U2U CAML Query Builder tool which you can use to write your CAML for you, however I'm from the school of though that I like to know how to do it myself even if I do use the tools, I guess that is where my habit of pulling things apart to see how they work stems from.
 
Edit: It turns out that the CAML query builder tool doesn't allow more than 2 criteria.
 
Anyway. the rules for composite criteria CAML are pretty simple. Essentially you can apply a logical operator to any two criteria and criteria may also be composed of results of logical operations.
 
So, what does this mean in CAML? Let's say we want to find items for a given project and student that have been sent.:
<Query>
    <Where>
        <And>
            <And>
                <Eq>
                    <FieldRef Name="StudentId" />
                    <Value Type="Text">{0}</Value>
                </Eq>
                <Eq>
                    <FieldRef Name="ActivityId" />
                    <Value Type="Text">{1}</Value>
                </Eq>
            </And>
            <Eq>
                <FieldRef Name="HasBeenSent" />
                <Value Type="Boolean">TRUE</Value>
            </Eq>
        </And>
    </Where>
</Query>
 
Of course you would replace {0} and {1} with the values that you were seaching for.
Now let's say that we want the items that either have a Pending status or have been sent for a given student:
<Query>
    <Where>
        <And>
            <Or>
                <Eq>
                    <FieldRef Name="HasBeenSent" />
                    <Value Type="Boolean">TRUE</Value>
                </Eq>
                <Eq>
                    <FieldRef Name="Status" />
                    <Value Type="Text">Pending</Value>
                </Eq>
            </Or>
            <Eq>
                    <FieldRef Name="StudentId" />
                    <Value Type="Text">{0}</Value>
            </Eq>
        </And>
    </Where>
</Query>
 
 
Feb23

CAML Query with dynamic date evaluation

 Categories: CAML, SharePoint

Wow, it's been over a weke since ODC2008 and I havn't posted, how slack of me. It's been a busy week talking about a lot of the cool things that I saw at ODC and cutting a lot of code.
 
Anyway I noticed that there is almost no documentation around how to do date based queries in CAML, particularly when you want to use a date that is evaluated at runtime.
 
The CAML query below show how to find iten that have an ImageCreateDate in the last seven days.
 
<Query>
    <Where>
        <Geq>
            <FieldRef Name="ImageCreateDate" />
            <Value Type="DateTime">
                <Today OffsetDays="-7" />
            </Value>
        </Geq>
    </Where>
</Query>
 
There you go, easy date math in CAML!