Wednesday, July 15, 2009

Get QueryString Data using javascript

Assume the url is "http://localhost:portnumber/site/pages/page.aspx?Id=1

javascript function:

function getQueryProjectNum(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++)
{
var pair = vars[i].split("=");
if (pair[0] == variable)
{
return pair[1];
}
}
}

var Id= getQueryProjectNum("Id");

Friday, July 10, 2009

C# Code to identify the Mobile Device

Mobile Device:

Request.Browser["IsMobileDevice"] == "true";

BlackBerry:


Request.Browser["BlackBerry"] == "true" ;

iphone:

Request.UserAgent.ToLower().Contains("iphone");

Sample c# code:

if (Request.Headers["User-Agent"] != null && (Request.Browser["IsMobileDevice"] == "true" || Request.Browser["BlackBerry"] == "true" || Request.UserAgent.ToUpper().Contains("MIDP") || Request.UserAgent.ToUpper().Contains("CLDC")) || Request.UserAgent.ToLower().Contains("iphone"))
{
Response.Redirect("www.google.com");
}

Thursday, July 9, 2009

Render sites in mobile device with required view

1) By default, all the sites are designed with the width 980 pixels.
2) If we render the page with this width in the mobile, the user sees your page with miniscule, illegible letters and must zoom in to see the interesting parts, this is unacceptable.
3) Luckily, there is an option for us.
Just we need to use a small "meta" tag as shown below.

<meta name="viewport" content="width=device-width,user-scalable=yes" />

width = device-width (automatically adjust according the device width or else you can give your required width)
initial-scale = represents the Zoom level at which the user initially wants to see the page.
user-scalable = if the content exceeds the page, it represents wheather to show the scroll bar or not.

Stylesheet:

If you are providing any style sheet, we need to add the "media" property as shown below.

<link rel="stylesheet" href="StyleSheet.css" media="screen" type="text/css">




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();
}
}

Friday, July 3, 2009

Site Content type Gallery is Empty

After Creation of Site Collection we need to restart the iis (iisreset /noforce).
Unless and until if you didn't restart the iis you cant see the site content types in the site content type gallery.
So, make sure that the iis is resert as soon as site collection is created.