Header

Sunday, 10 June 2012

CAML


As Small syntax error or order of tag in spquery can result any web part (or any thing in which spquery is used) doesn’t work, I’m explaining SPQuery with example, hope which may help you.

CAML (Collaborative Application Markup Language)
SPQuery is the SharePoint object which is used to perform a query operation against SharePoint data.SPList.getItems(SPQuery) is the step, will return SPListItemCollection which satisfies the query.SPQuery has one data member ‘Query’, which need to set before passing SPQuery object to SPList.


Some Properties
Properties
Description
RowLimit
Gets or sets a limit for the number of items returned in the query per page.
<RowLimit>10</RowLimit>
---
Can be used as: query.rowLimit = 10;
ViewFields
Gets or sets the fields that are displayed in the query.
<ViewFields>
        <FieldRef Name='Title'/>
        <FieldRef Name='Name'/>
</ViewFields>
---
Can be used as:
 SPQuery query = new SPQuery();
 query.ViewFields = "<FieldRef Name='Field1'/>" +
                      "<FieldRef Name='Field2'/>";  



How to Implement SPQuery?
using (SPWeb web = SPContext.Current.Site.RootWeb)
{
    SPList mylist = web.Lists["Tasks"];
    SPQuery query = new SPQuery();
    query.Query = "<Where><Eq><FieldRef Name='Status'/>" +
                   "<Value Type='Text'>Completed</Value></Eq></Where>";
    SPListItemCollection items = mylist.GetItems(query);
    foreach (SPListItem item in items)
    {
        Response.Write(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
    }
}

Operators:
Comparison Operators, Logical Joins and Order/ Group Operators, plays an important role to make this syntax.

Comparison Operators
Comparison Operators
General Meaning
Eq
=
Gt
>
Lt
<
Geq
>=
Leq
<=
Neq
<>
Contains
Like
IsNull
Null
IsNotNull
NotNull
BeginsWith
Beginning with word
DateRangesOverlap
compare the dates in a recurring event with a specified DateTime value, to determine whether they overlap




Now, to use Or between Geq and Leq conditions, we put it inside <Or> tag.
I.e. condition: where (Field1 >= 1500) or (field2 <= 500) can be written like this:
   <Where>
  <Or>
     <Geq>
         <FieldRef Name='Field1'/>
         <Value Type='Number'>1500</Value>
     </Geq>
     <Leq>
        <FieldRef Name='Field2'/><Value Type='Number'>500</Value>
     </Leq>
   </Or>
  </Where>


The full Code snippets will look like:
<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: Contains, And, BeginsWith
The following example uses the Contains element that is assigned to the Query property to return the titles of items where the Conference column value begins with "Morning" and contains "discussion session".

<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>

Example: DateRangesOverlap
<Where>

     <DateRangesOverlap>

          <FieldRef Name="EventDate"></FieldRef>

          <FieldRef Name="EndDate"></FieldRef>

          <FieldRef Name="RecurrenceID"></FieldRef>

            <Value Type="DateTime">

              <Now/>

            </Value>

     </DateRangesOverlap>

</Where>
Logical Joins:
Logical Joins
Comments
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
Example: And
<Where>

   <And>

     <Neq>

        <FieldRef Name="Status"></FieldRef>

        <Value Type="Text">Completed</Value>

     </Neq>

     <IsNull>

        <FieldRef Name="Sent"></FieldRef>

     </IsNull>

   </And>

</Where>
Order/Group Operators:
Order/Group Operators
Comments
OrderBy
Determines the sort order for a query.
The OrderBy element contains a group
of FieldRef elements
I.e.
 <OrderBy>
<FieldRef Name="Title" Ascending="TRUE">
</FieldRef>
</OrderBy>
GroupBy
Contains a Group By section for grouping the data returned through a query in a list view

Example: OrderBy
<OrderBy>

       <FieldRef Name="Modified" Ascending="FALSE"></FieldRef>

</OrderBy>

<Where>

  <Or>

    <Neq>

      <FieldRef Name="Status"></FieldRef>

      <Value Type="Text">Completed</Value>

    </Neq>

    <IsNull>

      <FieldRef Name="Status"></FieldRef>

    </IsNull>

  </Or>

</Where>
Example: GroupBy
<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>
Important:
This is very important thing to keep in mind, because SPQuery will be assigned as a string so it wont show compile time error though it may have tag syntax error, not in order, escape sequence error, etc.
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>

Thanks




Monday, 28 May 2012

How to Call JavaScript function inside any event of server control like button click

It is simple just need to use some property of page, like i am using in my example. With this example i am also trying to show u how can we write Button click on ASPX page that called inline code.


<html>
<head runat="server">
    <title></title>
    <script type="text/javascript" >

        function hello() {
            alert("hello");
             }

    </script>
</head>
<body>

<script language="c#" runat="server" >

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "Jatin Don";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "javascript", "hello()", true);
        
    }
 </script>

    <form id="form1" runat="server">
    <div>

    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"/>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>

Wednesday, 23 May 2012

How to show 3 items in one row in repeater


It is easy to show two items in a single row in any Repeater but when we need to show more then two item in single row we face problem.
But everything have a solution and the solution is here
check this code it will help you to comeout from this problem.....


If anything is wrong let me know i will come back to ur problem.
And drop your Feedback tooooooooooooo :D



<table>
 <asp:Repeater ID="rptTest" runat="server">
  <ItemTemplate>
   <%# IIf(((Container.ItemIndex) Mod 3) = 0, "<tr>", "")%>
  <td>
   <asp:Label ID="getval" runat="server" Text='<%#Eval("Name")>'></asp:Label>
  </td>
   <%# IIf((Container.ItemIndex + 3) Mod 3 = 2,"</tr>", "")%>
  </ItemTemplate>
 </asp:Repeater>
</table>

How to identify Browser using Javascript

Hi Friends,

We will see how we can get the client browser information.

Let see an example, like if we want to know is this browser Chrome or not then we can

In JavaScript we have object which will let us know the information of client browser.

//navigator.userAgent


 if( navigator.userAgent.toLowerCase().indexOf('chrome')>-1)
{
alert("it is chrome");
} 
else
{
alert("it is not Chrome");
}

In the above we are checking the name of Browser in the same way you can find the name.






Thursday, 17 May 2012

JavaScript to read SharePoint list items

<script type="text/javascript" style="font-size: 14px">


 $(function(){
SP.SOD.executeOrDelayUntilScriptLoaded(execClientOM, 'SP.js');

})
function execClientOM() {
alert("exe");
    context = SP.ClientContext.get_current();
alert(context);
    var list = context.get_web().get_lists().getByTitle("Programmes");
    var camlQuery = SP.CamlQuery.createAllItemsQuery();
    this.listItems = list.getItems(camlQuery);

    context.load(listItems);

    context.executeQueryAsync(ReadListItemSucceeded,
                              ReadListItemFailed);
}

function ReadListItemSucceeded(sender, args) {
    var itemsString = '';
    var enumerator = listItems.getEnumerator();

    while (enumerator.moveNext()) {
        var listItem = enumerator.get_current();
        itemsString += listItem.get_item('Title') + '\n';
    }

    alert(itemsString);
}

function ReadListItemFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' +
          args.get_stackTrace());
} </script>

Monday, 14 May 2012

How to Check query String is exist or not

Imports System.Collections.Specialized

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim n As NameValueCollection = Request.QueryString
' See if any query string exists
 If n.HasKeys() Then
            Dim k As String = n.GetKey(0)
            Dim v As String = n.[Get](0)
            Response.Write(v)
            Response.Write(k)
 End If
End Sub



.............................................
I hope this code will help u :D
Thanks And Regards,
Shailendra Kumar Singh

Wednesday, 18 April 2012

SPGrid View Styling


If we want to put our own Styling in SPGrid View then we need to change Corev4.css, and it is not good :(

So what should we do ?

Simple just put internal style sheet, but again what should we need to change. Bcz when i had started, it was too
struglling for me. But u dont worry :)
For you i have solutions.

Suppose you need to change Heading color,Background of heading then use this.

TH .ms-vb A{
color:#4b4b4b !important;
}

above style is for the heading of SPGridView text.


.ms-listviewtable .ms-vh2{
padding:0px !important;
background:#cdcdcd;
}

above style is for the background of heading


TD Table.ms-listviewtable
{
border-collapse:separate !important;
}
TH .ms-vb
{
    padding-top:5px !important;
}
.ms-vh2 Table.ms-selectedtitle
{
    background:#cdcdcd !important;
}

above code is for background setting of heading

TR.ms-viewheadertr > TH.ms-vh2:hover
{
  background:#cdcdcd !important;
}
TR.ms-viewheadertr > TH.ms-vh:hover
{
  background:#cdcdcd !important;
}



above code is for background setting of heading Hover


I will update next part sooon