Thursday, July 9, 2009

Render SharePoint List Items in ObjectList Mobile Controls

Generally, we use repeater control or datalist control to bind the SharePoint List Items.
When comes to Mobile Controls we use ObjectList Controls which works as repeater control.

Below is the sample code shows the procedure to bind the SharePoint List Items to ObjectList Control.

//Apply the styles for the Mobile Controls

<mobile:stylesheet id="mobileStyleSheet" runat="server">

       <mobile:style size="Small" name="Arial"></mobile:style>

   </mobile:stylesheet>

//Bind the SharePoint ListItem

<mobile:objectlist stylereference="ObjectList" id="olMobile" onprerender="olMobile_PreRender" runat="server">

          <devicespecific id="ds" runat="server">

               <choice>

                   <headertemplate>

                             Title

                   </headertemplate>

                   <itemtemplate>

                             <a href="javascript:void(0);"><%#Eval("Title") %></a>

                             <p><mobile:label id="lbDescription" runat="server"></mobile:label>

                             <mobile:link id="lnkUrl" runat="server">read more...</mobile:link></p>

                   </itemtemplate>

                   <footertemplate>

                          

                   </footertemplate>

               </choice>

          </devicespecific>

     </mobile:objectlist>

C# Code:

protected void Page_Load(object sender, EventArgs e)
{
using (SPSite spSite =new SPSite("http://servername:portnumber"))
{
SPWeb spWeb = spSite.OpenWeb();
spWeb.AllowUnsafeUpdates = true;
SPList spList = spWeb.Lists["ListName"];
SPQuery spQuery = new SPQuery();
spQuery.Query = "";
SPListItemCollection listitems= spList.GetItems(spQuery);
olMobile.DataSource = listitems.GetDataTable();
olMobile.DataBind();
spWeb.Dispose();
spWeb.AllowUnsafeUpdates = false;

}

}

protected void olMobile_PreRender(object sender, EventArgs e)
{
foreach (ObjectListItem item in olMobile.Items)
{
System.Web.UI.MobileControls.Link lnkUrl = (System.Web.UI.MobileControls.Link)item.FindControl("lnkUrl");
System.Web.UI.MobileControls.Label lbDescription = (System.Web.UI.MobileControls.Label)item.FindControl("lbDescription");

DataRowView drv = (DataRowView)item.DataItem;

lnkUrl.NavigateUrl = drv["Url"].ToString();
lbDescription.Text = drv["Description"].ToString();
}
}

2 comments:

Anonymous said...

Hi Raj, I excuted your code in visual studio 2010.but getting error " Object List does not exist in the current context error".

Please clarify.

digital certificate said...

You've made some fantastic points, I think all of them come under the importance of attention to detail, if you get that right you're certainly off to a good start.