June 13, 2013

Master page configuration based on device

  • Create new empty SharePoint project solution
  • Add new application page in project.
  • Add new c# class file and inherit httpmodule class. Add following code in this class.
     
        ----------------------------------------------------
     
        public void Init(HttpApplication context)
                {
                    context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
                }
         
                void context_PreRequestHandlerExecute(object sender, EventArgs e)
                {
                    Page page = HttpContext.Current.CurrentHandler as Page;
                    if (page != null)
                    {
                        // register handler for PreInit event
                        page.PreInit += new EventHandler(page_PreInit);
                    }
                }
         
                void page_PreInit(object sender, EventArgs e)
                {
                    Page page = sender as Page;
         
                    if (page != null)
                    { 
                        SPSite currentSite = (SPSite)SPContext.Current.Site;
                        SPWeb currentWeb = currentSite.RootWeb;
                        if (page != null)
                        {
                            string strUserAgent = page.Request.UserAgent.ToString().ToLower();
                            Console.Write(strUserAgent);
                            if (strUserAgent != null)
                            {
                                if (page.Request.Browser.IsMobileDevice == true ||
                                    strUserAgent.Contains("iphone") || strUserAgent.Contains("ipod") ||
                                    strUserAgent.Contains("symbian") || strUserAgent.Contains("android") ||  strUserAgent.Contains("htc") ||
                                    strUserAgent.Contains("windows ce") || strUserAgent.Contains("blackberry") ||
                                    strUserAgent.Contains("palm") || (strUserAgent.Contains("mobile") && !strUserAgent.Contains("ipad")) ||
                                    strUserAgent.Contains("opera mini"))
                                {
         
                                    currentWeb.MasterUrl = "/_catalogs/masterpage/Iphone-Device.master";
                                    currentWeb.CustomMasterUrl = "/_catalogs/masterpage/Iphone-Device.master";
                                }
                                else if (strUserAgent.Contains("mobile") && strUserAgent.Contains("ipad"))
                                {
                                    currentWeb.MasterUrl = "/_catalogs/masterpage/Ipad-Device.master";
                                    currentWeb.CustomMasterUrl = "/_catalogs/masterpage/Ipad-Device.master";
         
                                }
                                else
                                {
                                    page.MasterPageFile = "/_catalogs/masterpage/Desktop-Device.master";
                                   
                                }
                            }
                            else
                            {
                                currentWeb.MasterUrl = "/_catalogs/masterpage/v4.master";
                                currentWeb.CustomMasterUrl = "/_catalogs/masterpage/v4.master";
                            }
                            currentWeb.AllowUnsafeUpdates = true;
                            currentWeb.Update();
                            currentWeb.AllowUnsafeUpdates = false;
                        }
                    }
                }
                public void Dispose() { /* empty implementation */ }

  • Create new mater pages as per requirement and upload that master pages in web application.
  • Deploy this solution in web application.
  • Browse the application page and check the functionality in all devices.

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment