Labels

Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. Show all posts

Thursday, September 27, 2012

Displaying Powerview report using a sharepoint silverlight webpart

  1. Edit your page and click on "Add a Web Part".
  2. From the "Media and Content" category, select "Silverlight Web Part" and click the "Add" button.
  3. You will now need to enter a URL to the Silverlight XAP file. I found this location by opening the Power View editor and looking in the HTML source to see where it was loaded. In any case, it is something like this (your mileage may vary):
  4. You will see that SharePoint tries to load the Silverlight component, but fails. Not to worry, there are more settings to fill in before it actually works!
  5. First of all, you should see the Web Part's properties on the right hand side of your screen. If not, select the little drop down in the title bar of the Web Part and click on "Edit Web Part".
  6. I gave my Web Part a fixed width and height, but you can set it as you like.
  7. Chrome Type can be set to None to give a nice integrated look and feel.
  8. The most import settings are to be found under the heading "Other Settings".
  9. Click the (empty) textbox "Custom Initialization Parameters" and click on the button with the ellipsis (...).
  10. Here are my settings:
    • ItemPath=<LINK TO RDLX file>,
      ReportServerUri=http://<SHAREPOINT_SERVER>/_vti_bin/reportserver/,
      ViewMode=Presentation,
      ServerTraceLevel=1,
      AuthenticationMode=Windows,
      ReportSection=ReportSection,
      Fit=True,
      PreviewBar=False,
      BackgroundColor=White,
      Border=True,
      AllowEditViewMode=False,
      AllowFullScreenViewMode=False
    • The tricky bit is of course the url to the ItemPath, but the easiest way to find this is by opening the Power View editor, open "View Source" and search for the text: param name="InitParams"
    • Right after this text is the value parameter. Its contents pretty much gives you the ItemPath you need. Be sure to translate the encoded characters such as %3A to :, %2F to /, %20 to a space, etc.
  11. Finally, hit OK or Apply to make sure that it actually works!

If you would rather start with Page 2 (the second view) of your Power View report, simply change the value of the parameter ReportSection. For instance, ReportSection=ReportSection2 will show you the second view.

Friday, October 14, 2011

CAML - Basics

The Collaborative Application Markup Language (CAML) is an XML-based query language that helps you querying, building and customizing Web sites based on Windows SharePoint Services.

Example:
<Query>  
   <Where>
    <And>
        <And>
           <Eq>
             <FieldRef Name='LastName' />
             <Value Type='Text'>Janssens</Value>
           </Eq>
           <Geq>
             <FieldRef Name='Age' />
             <Value Type='Number'>21</Value>
           </Geq>
        </And>
        <Lt>
          <FieldRef Name='Age' />
          <Value Type='Number'>60</Value>
        </Lt>
    <And>
   </Where>
</Query> 

How to Build CAML?
It is an XML-based query language. Its root element is Query. Within the Query element two other elements are possible but not mandatory: the OrderBy element and the Where element.
The OrderBy element is the simplest one. It is used to sort the returning list items. You have to specify the field(s) on which you want to sort the items and the sort direction. The syntax looks as follows:

<OrderBy>
   <FieldRef Name='LastName' Ascending='False' />
</OrderBy>

If you omit the Ascending attribute, your resulting rows will be sorted in ascending order. If you want to order in descending order you have to specify Ascending=’False’.

The GroupBy clause is used for grouping:

<GroupBy>
      <FieldRef Name="Modified"/>
</GroupBy> 

<Where>
   <Or>
      <Neq>
          <FieldRef Name="Status"></FieldRef>
          <Value Type="Text">Completed</Value>
      </Neq> 
      <IsNull>
          <FieldRef Name="Status"></FieldRef>
      </IsNull> 
   </Or>
</Where>

The Where clause is used to specify one or more filter criteria. In its most simple form you specify an operator, a field name for which you want to specify a criterion, and a value.
<Where>
   <Eq>
     <FieldRef Name='LastName' />
      <Value Type='Text'>Janssens</Value>
  </Eq>
</Where>

Fields
The FieldRef element can be any field of the list on which you want to execute the CAML query. If you use the Name attribute you need to specify the internal name of the field. But you can also use the ID attribute to specify the Guid of the field.
Value
The Value element specifies the value part of the criterion. The attribute Type is optional and specifies the data type of the field you want to specify the criterion for. If omitted the data type is considered as being Text. In all other cases you have to specify the Type attribute.
If the field type is a Lookup you need to specify the text value. For example you have an Employees list and the Country field is a lookup field referring to the Countries list. In that case an employee living in Belgium will have f.e. following value: #15; Belgium. If you have to query for the employees living in Belgium you will have to write your query as follows:
<Where>
    <Eq>
     <FieldRef Name='Country' />
     <Value Type='Lookup'>Belgium</Value>
  </Eq>
</Where>

You can find this not good coding practice because the name of the country can change in time. In that case you can also query on the id of the country specifying the LookupId attribute in the FieldRef element:
<Where>
  <Eq>
     <FieldRef Name='Country' LookupId='True' />
     <Value Type='Lookup'>15</Value>
  </Eq>
</Where> 

Escape sequence:
The ScriptEncode method escapes characters that would otherwise conflict with script.
Original
Replacement
"
\"
\
\\
+
\u002b
> 
\u003e
< 
\u003c
'
\u0027










 Example:
<FieldRef Name=\"Checkbox\"></FieldRef> <Value Type=\"bit\">1</Value> 

Few of the commonly used operators list: 

Operator
Description
Eq
Equals (=)
Neq
Not Equals ( <>)
Gt
Greater than (>)
Geq
Greater or Equal to (>=)
Lt
Less than (<)
Leq
Less than or Equal to (<=)
IsNull / IsNotNull
Null  / Not Null
BeginsWith
Begins With Word
Contains
Like
And
Used within the ‘Where’ element to group filters in a query for a view
Or
Used within the ‘Where’ element to group filters in a query for a view
DateRangesOverlap
compare the dates in a recurring event with a specified DateTime value, to determine whether they overlap

Example for View:
<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>

Example: DateRangesOverlap

<where> 
     <DateRangesOverlap>
          <FieldRef Name="EventDate"></FieldRef>
          <FieldRef Name="EndDate"></FieldRef>
          <FieldRef Name="RecurrenceID"></FieldRef>
            <Value Type="DateTime">
              <Now/>  
            </Value>
</DateRangesOverlap>
</Where>

Example: Begins With / Contains 

<Where>
   <And>
        <BeginsWith>
               <FieldRef Name="Conference"/>
               <Value Type="Note">Morning</Value>
        </BeginsWith>
       <Contains>
               <FieldRef Name="Conference" />
               <Value Type="Note">discussion session</Value>
       </Contains>
   </And>
</Where>

Building Queries for working with DateTime Values
Filtering on DateTime fields also requires some extra attention. First of all when querying for a specific date, you have to use the SharePoint datetime notation:
<Where>
   <Ge>
        <FieldRef Name="StartDate" />
        <Value Type="DateTime">2008-08-10T10:00:00Z</Value>
   </Ge>
</Where>

But in this case the date is hard coded and the time part will not be taken into account. This query will return all list items with a start date as of 10 August 2008, also those starting before 10 o’clock. If you want your query to take into account the time part, you have to use a special attribute IncludeTimeValue that you can set on the FieldRef element:

<Where>
   <Ge>
        <FieldRef Name="StartDate" IncludeTimeValue="TRUE" />
       <Value Type="DateTime">2008-08-10T10:00:00Z</Value>
   </Ge>
</Where>

This query will return all list items with a start date as of 10 August 10 o’clock.
As already said, this way the date is hard coded. If you want your query a bit more dynamic, you can always use the element Today.
<Where>
   <Ge>
        <FieldRef Name="StartDate" />
        <Value Type="DateTime"><Today /></Value>
   </Ge>
</Where>

Today will not take a time part into account. You can also add or subtract a number of days from today’s date. In that case you have to add the Offset attribute to the Today element. The Offset attribute accepts a positive value for adding days and a negative value for subtracting days.

 <Where>
   <Ge>
        <FieldRef Name="StartDate" />
        <Value Type="DateTime"><Today Offset="10" /></Value>
   </Ge>
</Where>

Retrieving List Items with CAML using the SharePoint Object Model
If you need to retrieve items from a list when developing web parts, application pages or custom field types you can best use the SPQueryobject from the SharePoint object model. This object is located in the Microsoft.SharePoint namespace of the Microsoft.SharePoint.dll located in the Global Assembly Cache.
Instantiate the object as follows:
SPQuery qry = new SPQuery();

The most important property is the Query property, which needs to be set to your CAML query:

string camlquery = "<OrderBy><FieldRef Name='Country' /></OrderBy><Where>"
    + "<Eq><FieldRef Name='LastName' /><Value Type='Text'>Smith</Value></Eq>"
    + </Where>";
qry.Query = camlquery;

At this point you can execute the query on your list:
SPListItemCollection listItemsCollection = list.GetItems(qry);

A small remark with the GetItems method of the SPList instance: this method returns a collection of type SPListItemCollection. It is possible that it is easier working with a DataTable. In that case you can execute the query as follows:
DataTable listItemsTable = list.GetItems(qry).GetDataTable();

The query will not only return all list items that have their last name set to Smith, but also all columns of each list item. In cases you work with large lists it can be important to retrieve a subset of list items containing only the columns you need. In that case you will have to set the ViewFields property of the SPQuery object. You can do this by specifying all columns you want to see returned:
qry.ViewFields = "<FieldRef Name='FirstName' /><FieldRef Name='LastName' />";

This will return the first name and the last name of the retrieved employees, but also the system fields like the ID and the Created date.
The major disadvantage of the SPQuery object is that you can query only one list. If you want to query more than one list you will have to use the SPSiteDataQuery.
We can also retrieve the List Items from List.asmx (Web Service).