Showing posts with label CAML. Show all posts
Showing posts with label CAML. Show all posts

Thursday, December 6, 2012

SPDataSource

Example:
1.  <SharePoint:SPDataSource runat="server"
2.  DataSourceMode="List"
3.  SelectCommand="<View></View>"
4.  id="dataviewdatasource1" >
5.  <SelectParameters>
6.  <WebPartPages:DataFormParameter Name="ListID"
7.  ParameterKey="ListID"
8.  PropertyName="ParameterValues"
9.  DefaultValue="00000000-0000-0000-0000-000000000000" />
10. <asp:Parameter DefaultValue="{sitecollectionroot}" Name="WebUrl"></asp:Parameter>
11. </SelectParameters>
12. </SharePoint:SPDataSource>

DataSourceMode

Data Source Mode is a type of WSS data to query. The valid modes are:
  • List - Query items in a single list. Multiple rows of list data are returned. Each row represents a list item. This mode is similar to using an instance of the SPQuery class against a list. This mode can get lookup fields. This is the default mode.
  • ListOfLists - Query property values of all lists in a Web site. Multiple rows of data are returned. Each row represents a list.
  • CrossList - Query items in multiple lists in multiple Web sites in the same site collection. Multiple rows of list data are returned. Each row represents a list item. This mode is similar to using an instance of the SPSiteDataQuery class against lists in a site collection. This mode cannot get lookup fields. used for recursive query.
  • Webs - Query property values of all sub sites of a specified Web site. Multiple rows of data are returned. Each row represents a Web site.
  • ListItem - Query field values in a list item. A single row of list data is returned. The row represents a single list item.

SelectCommand

  • Select Command is an actual query for that data type.
  • Some modes don’t require a query at all.
  • In List mode, the SelectCommand roughly maps to the SPQuery object and accepts Collaborative Application Markup Language (CAML) fragments to specify things like the set of return fields, filtering, and sort order.
  • Briefly, this command will draw the wanted fields we want to draw from the data source to the DVWP. For example, if we want to draw the field “Title”, the code will be: SelectCommand="<View><ViewFields><FieldRef Name=’Title’/></ViewFields></View>"
  • But, if we want to create a query, the code will be written in CAML.SelectParameter
  • Select Parameter is each command expects a number of different parameters.
  • These parameters can refer to the parameter binding’s collection in the Data View, as well as take most ASP.Net types of parameters like Control and QueryString.

Where from? <asp:Parameter DefaultValue="…" Name="WebUrl"></asp:Parameter>

  • {sitecollectionroot} – will get the data from the root site: <asp:Parameter DefaultValue="{sitecollectionroot}" Name="WebUrl"></asp:Parameter>
  • If we want a specify sub-site: <asp:Parameter DefaultValue="/sub-site" Name="WebUrl"></asp:Parameter>

Parameteres

List and ListItem modes require the “DataFormParameter” list ID or list name, for example (CrossList for example need that parameter but not using it):
<WebPartPages:DataFormParameter Name="ListID" ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="00000000-0000-0000-0000-000000000000" />
<WebPartPages:DataFormParameter Name=" ListName" ParameterKey="ListName" PropertyName="ParameterValues" DefaultValue="<List Name>" />
  • ListID – GUID that corresponds to a List’s ListID.
  • ListName – Display name for a List.
  • ListItemId – ID for a single item in ListItem mode
  • WebUrl – Url to the web. When not specified, SPDataSource uses the current web to resolve the previous parameters. You can also use this parameter to access lists in other Web sites in the same Site Collection assuming you have permissions to that list.

- Thanks to Eray Chou' post.



Wednesday, March 14, 2012

CAML

CAML (Collaborative Application Markup Language) is an XML based markup language used with the family of Microsoft SharePoint technologies. Each markup will start with <View> tag. The classes are:
<View>
 <Query>
  <Where> - Used to specify a filter.
   <And> - Group filters in a query for view. Contain only two filters, if we want to add another we need to wrap both of the element with another <And> element.
   <Or> - Group filters in a query. Contain only two filters, if we want to add another we need to wrap both of the element with another <Or> element.
    <BeginsWith> - Searches for a string at the start of a column that holds Text or Note field type values.
    <Contains> - Searches for a string anywhere within a column that holds Text or Note field type values.
    <DateRangesOverlap> - Used in queries to compare the dates in a recurring event with a specified Date Time value, to determine whether they overlap.
    <Eq> - Arithmetic operator that means "equal to" and is used within a query.
    <Geq> - Arithmetic operator that means "greater than or equal to".
    <Gt> - Arithmetic operator that means "greater than".
    <In> - Specifies whether the value of a list item for the field specified by the FieldRef element is equal to one of the values specified by the Values element.
    <Includes> - If the specified field is a Lookup field that allows multiple values, specifies that the Value element is included in the list item for the field that is specified by the FieldRef element.
    <IsNotNull> - return items that are not empty (Null).
    <IsNull> - return items that are empty (Null).
    <Leq> - Arithmetic operator that means "less than or equal to”.
    <Lt> - Arithmetic operator that means "less than".
    <Neq> -  Arithmetic operator that means "not equal to".
    <NotIncludes> - If the specified field is a Lookup field that allows multiple values, specifies that the Value element is excluded from the list item for the field that is specified by the FieldRef element.

1.       There are two elements for ordering:
<OrderBy> - Determines the sort order for a query. Ordering by defining the elements  “Override” (Optional Boolean) and “UseIndexForOrderBy” (Optional Boolean). For example:
<OrderBy  Override = "TRUE" | "FALSE"  UseIndexForOrderBy = "TRUE" | "FALSE">
  <FieldRef
    Ascending = "TRUE" | "FALSE"
    Name = "Text" />
    ...
</OrderBy>
<GroupBy> - Contains a Group By section for grouping the data returned through a query in a list view .Grouping by defining the element “Collapse”, Optional Boolean. TRUE, for the Group By section in the list view to be collapsed by default. For example:
<GroupBy
  Collapse = "TRUE" | "FALSE">
  <FieldRef Name = "FieldName"/>
</GroupBy>
If we want to group by content type:
<Where>
 <Eq>
  <FieldRef Name='ContentType'/>
  <Value Type='Text'>{ContentTypeName}</Value>
 </Eq>
</Where>
2.       For example, if we want to create a list view that will show us the Title field and the Name field, sort by ID field, and filtered by (Field1 >= 1500) or (field2 <= 500), it will look like this:
<View>
 <Query>
  <OrderBy>
   <FieldRef Name='ID'/>
  </OrderBy>
  <Where>
   <Or>
    <Geq>
     <FieldRef Name='Field1'/>
     <Value Type='Number'>1500</Value>
    </Geq>
    <Leq>
     <FieldRef Name='Field2'/>
     <Value Type='Number'>500</Value>
    </Leq>
   </Or>
  </Where>
 </Query>
 <ViewFields>
  <FieldRef Name='Title'/>
  <FieldRef Name='Name'/>
 </ViewFields>
 <RowLimit>10</RowLimit>
</View>

3.       As I mentioned before about the limit of the <And> and <Or> tags. If we want three filters we will add another <And> / <Or> tag. For example, this is the regular script:
<And>
 <Leq>
 </Leq>
 <Geq>
 <Geq>
</And>
But if we want three, it will look like this:
<And>
 <And>
  <Leq>
  </Leq>
  <Geq>
  <Geq>
 </And>
 <And>
  <Contains>
  </Contains>
 </And>
</And>