Tuesday, May 22, 2012

Binding objectdatasource to SPGridView for Sorting

Share/Save/Bookmark

Today I have a simple requirement, to achieve filtering and sorting on a simple grid.
Asp.Net GridView do not have an option to filter by default but where as SPGridView provides as part of the control.
I have simple method, that will form the data into DataTable which will be set as datasource to SPGridView as shown in below code.



<SharePoint:SPGridView runat="server" ID="reportsList" AutoGenerateColumns="false"PageSize="1" AllowPaging="false"
AllowSorting="true"  AllowFiltering="false" FilterDataFields="Col1,Col2,Col3,Col4" >
    <Columns>
        <asp:TemplateField HeaderText="Col1" SortExpression="Col1">
            <ItemTemplate>
                <asp:HyperLink ID="Col1Link" runat="server" Text='<%# Eval("Col1") %>'NavigateUrl= '<%# Eval("Col5") %>'   />
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField HeaderText="Col2" SortExpression="Col2">
            <ItemTemplate>
                <asp:HyperLink ID="TabLink" runat="server" Text='<%# Eval("Col2") %>'NavigateUrl= '<%# Eval("Col6") %>'  />
            </ItemTemplate>
        </asp:TemplateField>
                                   
        <SharePoint:SPBoundField HeaderText="Col3" SortExpression="Col3" DataField="Col3" />
        <SharePoint:SPBoundField HeaderText="Col4" SortExpression="Col4" DataField="Col4" />
    </Columns>
    <EmptyDataTemplate>   No data.</EmptyDataTemplate>
</SharePoint:SPGridView>


above markup is also an example for binding hyperlink fields in spgridview.

protected override void CreateChildControls()
        {
            base.CreateChildControls();
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                  
                    // bind the datasource to grid
                    reportsList.DataSource = GetMyDataTable();
                    reportsList.DataBind();
                  
                }
            }
           
        }
So easy and simple to see the grid populated with data on the page, but when I choose to sort a column I get the below error .
The GridView 'GridViewID' fired event Sorting which wasn't handled
Googling around I found that property, DataSourceID need to set instead of setting DataSource. Not sure why.
So now I have to rewrite the code and bind some type of datasource(objectdatasource/SPdatasources/…/).
As we know, ObjectDataSource is simple and easy I started with it and then lately realized that I have to create objects, I mean new type that will be used by ObjectDataSource as suggested by all other blogs.
But I do not want to write extra code but directly bind the existing datatable as objectdatasource for which blogs suggest to create type and write a method that will convert it to datatable which seem to be round way for me.
So I tried the below code and able to achieve sorting without a new Type/object definition. Still i could not get filtering work..at least it did not throw any error in log or on page.


//populate the datasource
                    ObjectDataSource  reportsDataSource = new ObjectDataSource();
                    reportsDataSource.ID = "reportsDataSource";
                    reportsDataSource.SelectMethod = "GetMyDataTable";
                    reportsDataSource.TypeName = this.GetType().AssemblyQualifiedName;
                    this.Controls.Add(reportsDataSource);
                    
                    // bind the datasource to grid
                    reportsList.DataSourceID = "MyDataSource";//  = 
                    reportsList.DataBind();


 Subscribe

Sunday, May 6, 2012

Read SharePoint Service using JQuery

Share/Save/Bookmark


I had spent a day to know that soap response can be read at the event complete but not success.
In success event soapresponse(data.responseXML) is always empty. I'm not sure if its a sepcial case with soap.

Below function of Jquery retrieves the SocialTag count of a specific URL from all users.
Complete event triggers all the time after the service call resulting in success or error. So i used a flag based on which i can get to know if the service call is success or error.

function GetLikeCountFromService(domId, url) {

var tagName = "I like it";
var queryStatus = true;


var soapEnv =
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>\
<soap:Body> \
<GetTagTermsOnUrl xmlns='http://microsoft.com/webservices/SharePointPortalServer/SocialDataService'>\
<url>" + url + "</url>\
<maximumItemsToReturn>'100</maximumItemsToReturn> \
</GetTagTermsOnUrl> \
</soap:Body> \
</soap:Envelope>";

$.ajax({
url: '/_vti_bin/socialdataservice.asmx',
type: 'POST',
dataType: "xml",
data: soapEnv,
complete: function (data) {

//process only if the query is success
if (queryStatus) {
var likeCount = 0;
$(data.responseXML).find("SocialTermDetail").each(function () {
var term = $(this).find("Term").find("Name").text();
//alert($(this).find("Term").find("Name").text());
if (term == tagName) {
likeCount = $(this).find("Count").text();
// set the like count
$(domId).text(likeCount);
return;
}
});
,

error: function (all, textStatus, errorThrown) { queryStatus = false; alert(errorThrown); },
contentType: "text/xml; charset=\"utf-8\""
});
}



JQuery made life easy with flexible API. I have implemented another function similar to it that will like a URL, in sharepoint terms tagging a url with "I Like It". As a combination this will work as similar to the Social Like feature in Facebook.

Subscribe