Showing posts with label Master Page. Show all posts
Showing posts with label Master Page. Show all posts

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.

November 2, 2009

Set master page from current site context in ASPX pages Runtime

Sometimes you create ASPX page and host it into SharePoint site, I mean you copy and paste it in _layouts folder.

If you want your ASPX Page is running within the SharePoint context (within a site) and you want to set current master page to your ASPX page, here are the steps that can be followed seamlessly.
What you need to do is, set the master page during the OnPreInit method of the ASP.NET page lifecycle

//Here is code for set master page in ASPX page

public partial class ABC: System.Web.UI.Page
{
SPWeb webspweb;

Protected override void OnPreInit (EventArgs e)

{

//call the base class method first

base. OnPreInit (e);

//applied runtime master page to the current page

webspweb = SPControl.GetContextWeb (Context);

this.MasterPageFile = webspweb.CustomMasterUrl;

}

}

The main think to keep in mind is the OnPreInit method where we applied the master page from the current site (SPWeb).

LAYOUTS applications are always called in the context of a site: http://…/sitename/_layouts/MyApp/Default.aspx

Happy Coding J

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

By: Piyush